Django:需要选中复选框以提交表单 [英] Django: Require Checkbox to be ticked to Submit Form
问题描述
我正在Django(使用ModelForm)中创建一个表单。
有很多复选框,我要选中它,以便必须选中其中之一才能提交表单。我不是说任何一个复选框,而是一个 specific 框。我在Django文档中找不到任何内容。任何帮助,将不胜感激。
I'm creating a form in Django (using ModelForm). There are many checkboxes, and I want to make it so that one of these must be selected in order to submit the form. I don't mean any one checkbox, but one specific box. I can't find anything in the Django documentation. Any help would be appreciated.
推荐答案
类似
from django import forms
class MyForm(forms.Form):
check = forms.BooleanField(required = True)
# your other form fields
对于BooleanField, required = True
将检查是否选中了该框。这是因为只有在检查数据后才会提交数据。
For a BooleanField, required = True
will check if the box is checked. This is because data will only be submitted if it is checked.
来源: https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.BooleanField
如果字段为required = True,则验证该值为True(例如,复选框已选中)。
Validates that the value is True (e.g. the check box is checked) if the field has required=True.
这篇关于Django:需要选中复选框以提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!