tags:

views:

71

answers:

2

I have a GET form with checkboxes. When i select several boxes and submit the form it produces a link with several variables with the same name. How do i get each value of those variables in the view?

class SearchJobForm(ModelForm):
    query = forms.CharField()
    types = forms.ModelChoiceField(queryset=JobType.objects.all(), widget=forms.CheckboxSelectMultiple())
    class Meta:
        model = Job
A: 

Give every checkbox a different name attribute. How do you generate your checkboxes?

markmywords
+3  A: 

request.GET is a QueryDict instance that has a getlist method. If you call

request.GET.getlist('mykey')

you'll get back a list with all the values, e.g. if the querystring is mykey=1&mykey=2, you'll get ['1', '2'] from getlist.

If you use the MultipleChoiceField, Django handles this automatically for you.

piquadrat