Django: using <select multiple> and POST

Question:

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?

Asked By: neoice

||

Answers:

request.POST.getlist('services')

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 []

Answered By: electromechanick

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

Answered By: ady

you can get the expected list just by using…

request.POST.getlist('fiel_name')
Answered By: Javed

request.POST.getlist(‘services’)

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

Answered By: Projesh Bhoumik
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.