【发布时间】:2022-02-02 02:53:33
【问题描述】:
我有一个模板,它通过一个查询集并为每个项目创建一个复选框,这似乎是一个验证问题。我只能在检查每个复选框时才提交此文件,但我似乎无法弄清楚出了什么问题。
我的模板:
<form method="POST">
{% csrf_token %}
<fieldset>
{% for choice in choices %}
{{ choice.description }}
<input type="checkbox" value="{{choice.section}}" name="sections" required="">
{% endfor %}
<button type="submit">Submit</button>
</fieldset>
</form>
我的表单.py
class AddSectionForm(forms.Form):
sections = forms.MultipleChoiceField(
required=False, widget=forms.CheckboxSelectMultiple())
编辑
呃,我是个白痴,这是 html 复选框对象中的required=""!
【问题讨论】:
-
请将解决方案作为答案发布在下面,而不是编辑您的问题。
-
原来这只是部分答案,我通过验证解决了问题,但这仍然无法通过 form.is_valid()
-
@Chonker: 你的
sections没有choices=...... -
能发下相关观点吗?
标签: django validation checkbox