learner
learner

Reputation: 323

In django form how to add checkboxes instead of radio button?

I have a form where for one specific field I can have multiple choices. Something like

What would you like to eat this weekend?
Choice 1: Italian 
Choice 2: Mexican
Choice 3: Greek
Choice 4: Indian

I have radio button for all possible choices but because weekend is a multiple meals most of the time people want to select more than just one item. That makes me think it may be a good idea to have checkboxes associated with all possible choices.

How do I go about converting my radio buttons into a checkboxes ??

Please advise. Thanks.

Upvotes: 0

Views: 1051

Answers (2)

nael
nael

Reputation: 1507

You can use the MultipleChoiceField in Django. https://docs.djangoproject.com/en/1.10/ref/forms/fields/#multiplechoicefield

If the choices that the user can select are coming from a model then you can use the ModelMultipleChoiceField and pass a queryset for the choices. check https://docs.djangoproject.com/en/1.10/ref/forms/fields/#modelmultiplechoicefield

Upvotes: 1

mtt2p
mtt2p

Reputation: 1906

Use the django widgets

CHOICES=[('Italian','Italian'),
         ('Mexican','Mexican')]

like = forms.ChoiceField(choices=CHOICES, widget=forms.Select())

Upvotes: 0

Related Questions