Python ';此字段为必填字段;显示在输入旁边,带有GET请求
我无法获取“此字段是必需的”。在实例化表单时,不会在页面上显示。另外,我正在使用Django+引导 forms.pyPython ';此字段为必填字段;显示在输入旁边,带有GET请求,python,django,twitter-bootstrap,django-forms,Python,Django,Twitter Bootstrap,Django Forms,我无法获取“此字段是必需的”。在实例化表单时,不会在页面上显示。另外,我正在使用Django+引导 forms.py class RatingForm(forms.Form): name = forms.CharField( label='', widget=forms.TextInput(attrs={'class': 'form-control'}), ) rating = forms.ChoiceField( label='', widget=fo
class RatingForm(forms.Form):
name = forms.CharField(
label='',
widget=forms.TextInput(attrs={'class': 'form-control'}),
)
rating = forms.ChoiceField(
label='',
widget=forms.RadioSelect(attrs={'class': ''}),
choices=RATING_CHOICES,
)
views.py函数
thing = Thing.objects.get(name=thing_name)
rating_form = RatingForm(request.POST) # get form for adding a Rating to Thing
args={'thing':thing, 'rating_form':rating_form}
args['ratings'] = thing.ratings.all()
if request.method == 'POST':
...
...
...
else:
return redirect('/')
else:
if thing:
# This is the page I'm loading that I'm having problems with
return render(request, 'thing_page.html', args)
else:
return redirect('/')
html
{%csrf\u令牌%}
创建新评级:
{{评级形式|删除标签:“li ul”}
提前感谢您的帮助:)您应该只传递
请求。POST
到表单中以获取POST请求
if request.method == 'POST':
rating_form = RatingForm(request.POST)
...
else:
rating_form = RatingForm()
如果对GET请求使用RatingForm(request.POST)
,则request.POST
为空,因此任何必填字段都会出现错误。相反,对于GET
请求,应该使用RatingForm()
。这将创建一个未绑定的表单,因此不会出现任何验证错误
有关更多信息,请参阅上的文档。您应该只将
请求传递给POST请求的表单。POST
if request.method == 'POST':
rating_form = RatingForm(request.POST)
...
else:
rating_form = RatingForm()
如果对GET请求使用RatingForm(request.POST)
,则request.POST
为空,因此任何必填字段都会出现错误。相反,对于GET
请求,应该使用RatingForm()
。这将创建一个未绑定的表单,因此不会出现任何验证错误
有关更多信息,请参阅上的文档。Ah!太明显了!谢谢阿拉斯代尔:)啊!太明显了!谢谢阿拉斯代尔:)