28

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

Bigdinrock
  • 525
  • 2
  • 10
  • 14

3 Answers3

57

forms.py

class SomeForm(forms.Form):
    CHOICES = (('a','a'),
               ('b','b'),
               ('c','c'),
               ('d','d'),)
    picked = forms.MultipleChoiceField(choices=CHOICES, widget=forms.CheckboxSelectMultiple())

views.py

def some_view(request):
    if request.method == 'POST':
        form = SomeForm(request.POST)
        if form.is_valid():
            picked = form.cleaned_data.get('picked')
            # do something with your results
    else:
        form = SomeForm

    return render_to_response('some_template.html', {'form':form },
        context_instance=RequestContext(request))

some_template.html

<form method='post'>
    {{ form.as_p }}
    <input type='submit' value='submit'>
</form>

results:

checkboxselectmultiple

explanation:

choices:

The first element in each tuple is the actual value to be stored. The second element is the human-readable name for the option.

getting selected boxes:

form.cleaned_data.get('picked') will result in a list of the 'actual values'. For example, if I replaced the # do something with your results with print picked you see:

[u'a', u'c']

in your console

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
dting
  • 38,604
  • 10
  • 95
  • 114
  • Thanks a lot..it really cleared up things for me. I still have one more question. When I try to handle the results will I try to handle each element of the list as u'a' or just the string 'a'. – Bigdinrock Apr 21 '11 at 17:52
  • 1
    How take list of human readable names? I need take list of second option of tuple. – Nurzhan Nogerbek Apr 24 '17 at 03:33
  • initial_values = ['a', 'c'] picked = forms.MultipleChoiceField(choices=CHOICES, widget=forms.CheckboxSelectMultiple(), initial=initial_values) – Russo Jul 30 '18 at 16:17
  • 1
    What does the rendered HTML look like? – Josh Grinberg Jan 12 '20 at 16:17
29

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)
Trect
  • 2,759
  • 2
  • 30
  • 35
Paulo
  • 6,982
  • 7
  • 42
  • 56
0

you can check this https://pypi.python.org/pypi/django-multiselectfield/

from multiselectfield import MultiSelectField

# ...

MY_CHOICES = (('item_key1', 'Item title 1.1'),
          ('item_key2', 'Item title 1.2'),
          ('item_key3', 'Item title 1.3'),
          ('item_key4', 'Item title 1.4'),
          ('item_key5', 'Item title 1.5'))

MY_CHOICES2 = ((1, 'Item title 2.1'),
           (2, 'Item title 2.2'),
           (3, 'Item title 2.3'),
           (4, 'Item title 2.4'),
           (5, 'Item title 2.5'))

class MyModel(models.Model):

    # .....

    my_field = MultiSelectField(choices=MY_CHOICES)
    my_field2 = MultiSelectField(choices=MY_CHOICES2,
                             max_choices=3,
                             max_length=3)
Randy Arguelles
  • 225
  • 1
  • 9