Django: using <select multiple> and POST

2019-01-21 01:40发布

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条回答
乱世女痞
2楼-- · 2019-01-21 02:08

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

查看更多
太酷不给撩
3楼-- · 2019-01-21 02:09

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

request.POST.getlist('fiel_name')
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-21 02: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 []

查看更多
聊天终结者
5楼-- · 2019-01-21 02:27
request.POST.getlist('services')
查看更多
6楼-- · 2019-01-21 02:27

request.POST.getlist('services')

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

查看更多
登录 后发表回答