0
So that the html
send a field of the type select
with several values selected it is necessary to put the notation []
in the name attribute of html
:
<select name="categories[]" multiple="multiple" class="form-input__select">
{% for cat in categories %}
<option value="{{ cat.id }}">{{ cat.id }}</option>
{% endfor %}
</select>
how do I capture this value using Form.forms
Django?
I tried to use the:
categories = forms.MultipleChoiceField(required=False)
but when using the form.cleaned_data
the value of categories[]
empty.
debug before the cleaned_data
:
'categories[]': ['AB', 'AU'], 'brands_are_inclusive': ['true']
debug after the cleaned_data
:
'categories': [], 'brands_are_inclusive': 'true'
Man, as far as I know this trick to use
[]
to denote multiple values is a PHP thing. On other servers just usename="categories"
because sending to the server turns into something like:categories=1&categories=3&categories=5
.– fernandosavio
if I don’t put the
[]
the validation ofMultipliChoiceField
does not capture the field ie, is not a list– RFL