82

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?

4
  • What's the value of services that's provided to the template? Commented Mar 6, 2009 at 11:45
  • Do you mean: services = Service.objects.all() return render_to_response('add.html', {'services': services}) ?? (I cannot get these comments to format at all.) Commented Mar 6, 2009 at 11:54
  • 3
    @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. Commented Mar 6, 2009 at 12:30
  • how did u get the value in jquery and pass it to django views?? Commented Oct 15, 2015 at 8:47

5 Answers 5

196
request.POST.getlist('services')
Sign up to request clarification or add additional context in comments.

7 Comments

Jackpot! I think I did need to change services to services[], but getlist r0xors for this. I thought I'd scoured the Django docs but apparently, I was wrong.
You don't need the []. That's a convention limited to PHP.
It might be worth mentioning that this also works for GET data. request.GET.getlist('services')
I needed to change services to services[] for Python as well.
May you expand where this should be? If I replace it in the View, then when debuging I get the list but lost all of the extra fields.
|
17

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 Comments

I have to use [] wihtout any blank between
Yeah, I originally had the brackets with no space between them, then somebody came along and edited my response to include a space, which of course breaks the code because matching a literal "[]" is not the same as "[ ]"
2

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

request.POST.getlist('fiel_name')

Comments

1

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

Comments

1

request.POST.getlist('services')

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.