Django: Require Checkbox to be ticked to Submit Form

混江龙づ霸主 提交于 2020-01-03 14:54:12

问题


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.



来源:https://stackoverflow.com/questions/16988127/django-require-checkbox-to-be-ticked-to-submit-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!