我正在寻找一个带有复选框的 Django 表单。无论我选中或取消选中该框的天气如何,它都不会在 POST 请求中检测到。这是模板的代码 -
<form action="annotate_page" method="post">{% csrf_token %}
<input id="repeat" type="checkbox" >
<label for="repeat">Repeat Sentence?</label>
<br>
<button type="submit">Next</button><br>
</form>
这是我的 forms.py-
from django import forms
class AnnotateForm(forms.Form):
repeat=forms.BooleanField(required=False)
这是我的观点逻辑-
if request.method=="POST":
form = AnnotateForm(request.POST)
if form.is_valid():
print(request.POST)#prints only csrf_token in Query_dict
print(form.cleaned_data["repeat"])#Always false
无论天气如何,复选框是否被选中,打印语句总是给出 False。
我知道有类似的问题,但它们不能解决我的问题。
<form action="annotate_page" method="post">{% csrf_token %}
<input id="repeat" name="something" type="checkbox" >
<label for="repeat">Repeat Sentence?</label>
<br>
<button type="submit">Next</button><br>
</form>
并且鉴于
if request.method=="POST":
form = AnnotateForm(request.POST)
if form.is_valid():
print(request.POST)#prints only csrf_token in Query_dict
print(form.cleaned_data["something"])#Always false
您需要在输入字段中提供名称,否则不会被捕获
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句