Django RadioSelect | CheckboxSelectMultiple | SelectDateWidget Examples

In trying to use django built-in forms, I had the most trouble finding good examples of django radioselect, checkboxselectmultiple and selectdatewidget.

Here’s my attempt to help others looking into using Django RadioSelect, CheckboxSelectMultiple, or the SelectDateWidget.

Django RadioSelect Example


from django.forms.widgets import RadioSelect

RADIO_CHOICES = [['1','Radio 1'],['2','Radio 2']]

class SimpleForm(forms.Form):
radio = forms.ChoiceField( widget=RadioSelect(), choices=RADIO_CHOICES)

Django CheckboxSelectMultiple Example


from django.forms.widgets import CheckboxSelectMultiple

CHECKBOX_CHOICES = (('1','The first choice'),('2','The Second Choice'))

class SimpleForm(forms.Form):
checkboxes = forms.MultipleChoiceField( required=False, widget=CheckboxSelectMultiple(), choices=CHECKBOX_CHOICES)

Django SelectDateWidget Example


from django.forms.extras.widgets import SelectDateWidget

YEAR_CHOICES = ('2016','2015')
class SimpleForm(forms.Form):
date = forms.DateField(widget=SelectDateWidget(None,YEAR_CHOICES) )

Django Docs

Update 1/3/2016: The official documentation on these have improved but is still buried within a huge post. Here is the link to the latest stable docs: https://docs.djangoproject.com/en/stable/ref/forms/widgets/

I Love Django Forms

These are all excellent features and the more I use Django, the more I like it. Chalk up Forms as another part of django that blows away any other web framework I’ve worked with.