【发布时间】:2012-03-29 12:51:22
【问题描述】:
根据 Django 教程,您应该使用 clean_data 字典访问表单字段。我想知道为什么我不能直接访问表单的属性?我的表单验证得很好,但是当我尝试访问它时,Django 抱怨该对象没有该属性。我在下面添加了一些代码,希望能帮助诊断问题。
表格:
class CustomForm(forms.Form):
description = forms.CharField(widget = forms.TextInput(attrs = {'placeholder' : 'enter some text'}), label = "My form")
查看:
def process_form(request):
if request.method != 'POST':
raise Http404
myForm = CustomForm(request.POST)
if not myForm.is_valid():
c = RequestContext(request)
return render_to_response('home/index.html', {'form' : myForm }, c)
# debug
print 'Description: ' + myForm.description # this does NOT work
# print 'Description: ' + myForm.cleaned_data['description'] # this does work
我收到以下错误:“CustomForm”对象没有“描述”属性。我是否错过了文档中说我不能这样做的内容?
【问题讨论】:
标签: python django forms validation