73
votes

I'm using something like this in my template

<select multiple="multiple"  name="services" id="services" size="5">
    {% for service in services %}
        <option value="{{service.id}}">{{service}}</option>
    {% endfor %}
</select>

When I view the POST data in Firebug or the Django debug, I see it only sends one value. Am I doing something wrong or misunderstanding a concept?

5
What's the value of services that's provided to the template? - S.Lott
Do you mean: services = Service.objects.all() return render_to_response('add.html', {'services': services}) ?? (I cannot get these comments to format at all.) - neoice
@neoice: Hint: don't add details to your questions in the comments. Edit your question to add facts. The question always formats correctly. AND the question should stand by itself without a thread of comments. - S.Lott
how did u get the value in jquery and pass it to django views?? - DeadDjangoDjoker

5 Answers

174
votes
request.POST.getlist('services')
14
votes

Just FYI, I had to use:

    list = request.POST.getlist("items[]")

because omitting the [] caused a blank list to be returned instead of the correct values. I'm using jQuery to fetch the values of a multiple select element, and jQuery appears to be adding the []

2
votes

you can get the expected list just by using...

request.POST.getlist('fiel_name')
1
votes

Watch out! getlist method from QueryDict returns an empty list if the key doesn't exist. It does not throw an exception. http://bit.ly/MdgrUH

1
votes

request.POST.getlist('services')

Worked for me. or you can define select box name as a list