Home > Software engineering >  Handle select multiple on django
Handle select multiple on django

Time:10-31

I have this on template

<select name="document" id="document" multiple="">
        <option value="2">test 1</option>
        <option value="3">test 2</option>
</select>

When I select two options when I accept the request in views.py I got correctly 2 options selected

self.request.POST // It prints 'document': ['2', '3']

But when I try to select the field like this

self.request.POST['document'] // It prints only '3'

How can I select 2 options?

Thanks

CodePudding user response:

You can use .getlist(…) [Django-doc] to obtain all the values associated with a given key.

You thus can here work with:

request.POST.getlist('document')
  • Related