I have been using this site as an example of how to make a dynamic form in Django. In his view he uses
我一直在使用这个站点作为如何在Django中创建动态表单的示例。在他看来他使用
if request.method == 'POST':
form = UserCreationForm(request.POST)
to pass the data into the form and in the form constructor he uses
将数据传递到表单和他使用的表单构造函数中
extra = kwargs.pop('extra')
to access the POST data. I tried to do something similar with my view:
访问POST数据。我尝试用我的观点做类似的事情:
def custom_report(request):
if request.method=='POST':
form=CustomQueryConstraintForm(request.POST)
else:
form=CustomQueryConstraintForm()
return render(request, 'frontend/custom_report.html', {'form':form})
In my form constructor I printed args
and kwargs
and found that kwargs
is empty and args
is a tuple containing the QueryDict
that in turn contains the POST data. If I try instead to use form=CustomQueryConstraintForm(**request.POST)
, each element in kwargs
is a list containing the value of the field as its only element. Am I doing something wrong here? If not, is there a more elegant way of accessing the data than args[0][element_name][0]
?
在我的表单构造函数中,我打印了args和kwargs,发现kwargs是空的,args是一个包含QueryDict的元组,而QueryDict又包含POST数据。如果我尝试使用form = CustomQueryConstraintForm(** request.POST),kwargs中的每个元素都是一个包含字段值作为唯一元素的列表。我在这里做错了吗?如果没有,是否有比args [0] [element_name] [0]更优雅的方式来访问数据?
3 个解决方案
#1
7
That is expected behavior for forms: the POST data you pass into the form is the first argument, args[0] and not a keyword argument. What are you looking for?
这是表单的预期行为:传递给表单的POST数据是第一个参数args [0]而不是关键字参数。你在找什么?
data = args[0]
print data['my_field']
and in the form constructor he uses extra = kwargs.pop('extra') to access the POST data.
并在表单构造函数中,他使用extra = kwargs.pop('extra')来访问POST数据。
kwargs.pop('extra')
is not getting POST data. It is a list of questions associated with that given user -- some scenario given by the author that the "marketing department" handed you.
kwargs.pop('extra')没有获取POST数据。这是与给定用户相关的问题列表 - 作者给出了“营销部门”递给您的一些情况。
In any case, if you need to access the post data at any point in a form, I find self.data
the cleanest which is set in forms.__init__
.
无论如何,如果你需要在表单中的任何一点访问帖子数据,我发现self.data是表格中设置的最干净的.__ init__。
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.data['my_field']
#2
0
If I understand correctltly, after a POST request you are trying to redisplay the same form page in which the form fields are filled, right? If yes, this is what you need:
如果我理解正确,在POST请求之后你试图重新显示填写表单字段的相同表单页面,对吗?如果是,这就是您所需要的:
form = CustomQueryConstraintForm(initial=request.POST)
#3
0
You can also access the form data through the cleaned_data
dictionary after using the is_valid()
method. Like this:
您还可以在使用is_valid()方法后通过cleaning_data字典访问表单数据。喜欢这个:
jobForm = JobForm(request.POST)
jobForm.is_valid()
jobForm.cleaned_data
it's a dictionary of the values that have been entered into the form.
它是已输入表单的值的字典。
#1
7
That is expected behavior for forms: the POST data you pass into the form is the first argument, args[0] and not a keyword argument. What are you looking for?
这是表单的预期行为:传递给表单的POST数据是第一个参数args [0]而不是关键字参数。你在找什么?
data = args[0]
print data['my_field']
and in the form constructor he uses extra = kwargs.pop('extra') to access the POST data.
并在表单构造函数中,他使用extra = kwargs.pop('extra')来访问POST数据。
kwargs.pop('extra')
is not getting POST data. It is a list of questions associated with that given user -- some scenario given by the author that the "marketing department" handed you.
kwargs.pop('extra')没有获取POST数据。这是与给定用户相关的问题列表 - 作者给出了“营销部门”递给您的一些情况。
In any case, if you need to access the post data at any point in a form, I find self.data
the cleanest which is set in forms.__init__
.
无论如何,如果你需要在表单中的任何一点访问帖子数据,我发现self.data是表格中设置的最干净的.__ init__。
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.data['my_field']
#2
0
If I understand correctltly, after a POST request you are trying to redisplay the same form page in which the form fields are filled, right? If yes, this is what you need:
如果我理解正确,在POST请求之后你试图重新显示填写表单字段的相同表单页面,对吗?如果是,这就是您所需要的:
form = CustomQueryConstraintForm(initial=request.POST)
#3
0
You can also access the form data through the cleaned_data
dictionary after using the is_valid()
method. Like this:
您还可以在使用is_valid()方法后通过cleaning_data字典访问表单数据。喜欢这个:
jobForm = JobForm(request.POST)
jobForm.is_valid()
jobForm.cleaned_data
it's a dictionary of the values that have been entered into the form.
它是已输入表单的值的字典。