What is point of setting cd = form.cleaned_data before passing the input values of a POST/GET submission? What is the point of this and why is it necessary (if it is so)?
设置cd = form的点是什么?在传递POST/GET submission的输入值之前清除_data ?这有什么意义?为什么有必要(如果有的话)?
2 个解决方案
#1
21
It is not necessary to use the .cleaned_data attribute of a form before passing the input values, it will raise an AttributeError if you do it before calling .is_valid() in a bound form or if you try to access it in an unbound form, read more about Form.cleaned_data .
在传递输入值之前,不需要使用窗体的.cleaned_data属性,如果在以绑定形式调用.is_valid()之前进行此操作,或者尝试以非绑定形式访问窗体,则需要阅读更多有关窗体的信息。cleaned_data。
Also, it is usually a good idea to abstract the use of the form's data in a form method in order to encapsulate logic
此外,为了封装逻辑,通常以表单方法抽象表单数据的使用是一个好主意
In your views, the traditional way you should be using forms is like this:
在你看来,使用表单的传统方式是这样的:
if request.method == 'POST':
form = MyForm(request.POST) # Pass the resuest's POST/GET data
if form.is_valid(): # invoke .is_valid
form.process() # look how I don't access .cleaned_data in the view
in your form:
在你的形式:
class MyForm(forms.Form):
my_field = forms.CharField()
def process(self):
# Assumes .cleaned_data exists because this method is always invoked after .is_valid(), otherwise will raise AttributeError
cd = self.cleaned_data
# do something interesting with your data in cd
# At this point, .cleaned_data has been used _after_ passing the POST/GET as form's data
#2
2
Once is_valid() returns True, you can process the form submission safe in the knowledge that it conforms to the validation rules defined by your form. While you could access request.POST directly at this point, it is better to access form.cleaned_data. This data has not only been validated but will also be converted in to the relevant Python types for you.
一旦is_valid()返回True,您就可以安全地处理表单提交,因为您知道它符合表单定义的验证规则。而你可以访问请求。现在直接发布,最好访问form.cleaned_data。这些数据不仅经过验证,而且还将转换为相关的Python类型。
Processing the data from a form
处理表单中的数据
#1
21
It is not necessary to use the .cleaned_data attribute of a form before passing the input values, it will raise an AttributeError if you do it before calling .is_valid() in a bound form or if you try to access it in an unbound form, read more about Form.cleaned_data .
在传递输入值之前,不需要使用窗体的.cleaned_data属性,如果在以绑定形式调用.is_valid()之前进行此操作,或者尝试以非绑定形式访问窗体,则需要阅读更多有关窗体的信息。cleaned_data。
Also, it is usually a good idea to abstract the use of the form's data in a form method in order to encapsulate logic
此外,为了封装逻辑,通常以表单方法抽象表单数据的使用是一个好主意
In your views, the traditional way you should be using forms is like this:
在你看来,使用表单的传统方式是这样的:
if request.method == 'POST':
form = MyForm(request.POST) # Pass the resuest's POST/GET data
if form.is_valid(): # invoke .is_valid
form.process() # look how I don't access .cleaned_data in the view
in your form:
在你的形式:
class MyForm(forms.Form):
my_field = forms.CharField()
def process(self):
# Assumes .cleaned_data exists because this method is always invoked after .is_valid(), otherwise will raise AttributeError
cd = self.cleaned_data
# do something interesting with your data in cd
# At this point, .cleaned_data has been used _after_ passing the POST/GET as form's data
#2
2
Once is_valid() returns True, you can process the form submission safe in the knowledge that it conforms to the validation rules defined by your form. While you could access request.POST directly at this point, it is better to access form.cleaned_data. This data has not only been validated but will also be converted in to the relevant Python types for you.
一旦is_valid()返回True,您就可以安全地处理表单提交,因为您知道它符合表单定义的验证规则。而你可以访问请求。现在直接发布,最好访问form.cleaned_data。这些数据不仅经过验证,而且还将转换为相关的Python类型。
Processing the data from a form
处理表单中的数据