Django: using <select multiple> and POST

PythonDjangoHttp

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

request.POST.getlist('services')

Solution 2 - Python

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

Solution 3 - Python

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

request.POST.getlist('fiel_name')

Solution 4 - Python

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

Solution 5 - Python

request.POST.getlist('services')

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionneoiceView Question on Stackoverflow
Solution 1 - PythonIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 2 - PythonelectromechanickView Answer on Stackoverflow
Solution 3 - PythonJavedView Answer on Stackoverflow
Solution 4 - PythonadyView Answer on Stackoverflow
Solution 5 - PythonProjesh BhoumikView Answer on Stackoverflow