Reputation: 4651
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
Upvotes: 1
Views: 745
Reputation: 31828
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.
Upvotes: 3
Reputation: 663
Give every checkbox a different name attribute. How do you generate your checkboxes?
Upvotes: 0