Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django form multiple choice

Tags:

python

django

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

like image 607
Bigdinrock Avatar asked Apr 21 '11 16:04

Bigdinrock


People also ask

How do I create a multiple choice field in Django?

To add a multiple choice field with Python Django, we can add a ManyToManyField . to create the choices field in the Profile model which is a ManyToManyField . We use Choices as the argument to allow Choices values as the values for choices .

How do I create a drop down list in Django?

To create a dropdown in Python Django model form, we can create a char field with the choices argument set to a tuple of choices in a model class. Then we can set the model class as the model for the form. to add the color field to the MyModel model class.

What is CharField in Django?

CharField is generally used for storing small strings like first name, last name, etc. To store larger text TextField is used. The default form widget for this field is TextInput. CharField has one extra required argument: CharField.max_length. The maximum length (in characters) of the field.


2 Answers

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

like image 115
dting Avatar answered Sep 18 '22 15:09

dting


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) 
like image 31
Paulo Avatar answered Sep 18 '22 15:09

Paulo