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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Something like
from django import forms
class MyForm(forms.Form):
check = forms.BooleanField(required = True)
# your other form fields
For a BooleanField, required = True
will check if the box is checked. This is because data will only be submitted if it is checked.
Source: https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.BooleanField
Validates that the value is True (e.g. the check box is checked) if the field has required=True.