Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Require Checkbox to be ticked to Submit Form

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.

like image 875
Daniel Rosenthal Avatar asked Jun 07 '13 15:06

Daniel Rosenthal


1 Answers

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.

like image 178
Mark Avatar answered Oct 04 '22 18:10

Mark