Bigdinrock Bigdinrock - 2 days ago 3
Python Question

Django form multiple choice

I am a newbie in Django and I would really appreciate it if you could offer me some guidance. I am trying to create a form that allows a user to tick one or more options. I understood that I must use MultipleChoiceField field with a CheckboxSelectMultiple widget but the Django documentation doesn't offer an example on this topic. I would be grateful if you could offer me an example and explain how do I handle the results. For example if I have a form with the options a b c d, and the user ticks c and d. Also how do I specify the choices(I don't want to use a db, a list of strings is what I have in mind)? Thanks a lot

Answer

hope this helps :D

from django import forms


    class Test(forms.Form):
        OPTIONS = (
            ("a", "A"),
            ("b", "B"),
            )
        name = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
                                         choices=OPTIONS)
Comments