【发布时间】:2017-12-26 09:29:07
【问题描述】:
以下是我的 Django 表单
class Country(forms.Form):
name = forms.CharField()
country = forms.ChoiceField(widget=forms.Select(attrs={'id':'country'}))
以下是发送表单到 HTML 页面之前的代码
form = Country()
choices = [('a', 'India'), ('b', 'United States of America')]
form.fields['country'].choices = choices
form.fields['country'].initial = 'b'
return render(request,"Test.html",{"form":form})
表单在前端正确渲染,并设置了初始值。 当用户单击提交按钮时。它正在抛出异常。
以下是我在用户点击提交按钮时编写的代码,
f = Country(request.POST)
print (f)
print("Country Selected: " + f.cleaned_data['country'])
当我在用户提交后打印表单时,我得到了如下所示的表单。
<tr><th><label for="id_name">Name:</label></th><td><input type="text" name="name" value="ggg" id="id_name" required /></td></tr>
<tr><th><label for="country">Country:</label></th><td><ul class="errorlist"><li>Select a valid choice. a is not one of the available choices.</li></ul><select name="country" id="country">
</select></td></tr>
请帮我解决这个问题。 谢谢!
【问题讨论】:
-
发布异常
-
异常:'Country' 对象没有属性'cleaned_data'
标签: django django-forms