[英]Accessing POST data in Django form
I have been using this site as an example of how to make a dynamic form in Django.我一直以这个站点为例,说明如何在 Django 中制作动态表单。 In his view he uses
在他看来,他使用
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.访问 POST 数据。 I tried to do something similar with my view:
我试图用我的观点做类似的事情:
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.在我的表单构造函数中,我打印了
args
和kwargs
,发现kwargs
是空的,而args
是一个包含QueryDict
的元组,而 QueryDict 又包含 POST 数据。 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.如果我尝试改用
form=CustomQueryConstraintForm(**request.POST)
,则kwargs
中的每个元素都是一个列表,其中包含该字段的值作为其唯一元素。 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[0][element_name][0]
更优雅的数据访问方式?
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.这是 forms 的预期行为:您传递给表单的 POST 数据是第一个参数 args[0] 而不是关键字参数。 What are you looking for?
你要买什么?
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. kwargs.pop('extra')
没有获取 POST 数据。 It is a list of questions associated with that given user -- some scenario given by the author that the "marketing department" handed you.它是与给定用户相关的问题列表——作者给出的“营销部门”交给你的一些场景。
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
是在forms.__init__
中设置的最干净的。
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.data['my_field']
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?如果我理解正确,在 POST 请求之后,您正试图重新显示填写表单字段的同一表单页面,对吗? If yes, this is what you need:
如果是,这就是您需要的:
form = CustomQueryConstraintForm(initial=request.POST)
You can also access the form data through the cleaned_data
dictionary after using the is_valid()
method.使用
is_valid()
方法后,您还可以通过cleaned_data
字典访问表单数据。 Like this:像这样:
jobForm = JobForm(request.POST)
jobForm.is_valid()
jobForm.cleaned_data
it's a dictionary of the values that have been entered into the form.它是已输入表单的值的字典。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.