2

I have HTML form like this:

    <p>Models Sizes IDs:</p>
    <input type="checkbox" name="model_size_ids[]" value="1">XS</input>
    <input type="checkbox" name="model_size_ids[]" value="2">S</input>
    <input type="checkbox" name="model_size_ids[]" value="3">M</input>
    <input type="checkbox" name="model_size_ids[]" value="4">L</input>
    <button>Submit</button>

I'm trying to receive an array of checked values on server side in my View:

size_ids = request.data['model_size_ids[]']

But, I can extract only one and the last value. So if I check 2-3 values in checkbox form, I receive only last value in my view. I also tried to name input field without braсkets and the result was the same. Can anybody tell me, how can I solve that? Thanks!

1

2 Answers 2

10

Use the getlist method for geting the list of selected choices

request.POST.getlist('model_size_ids[]')
Sign up to request clarification or add additional context in comments.

1 Comment

Also note that you don't need the brackets ("[ ]") in the name for Django to correctly treat this as a list (this bracket thing is a PHP hack actually, not a part of the HTTP spec)
-1

You don't have to use '[ ]'. You can simply write

request.POST.getlist('model_size_ids')

However, if someone doesn't check one of those buttons, the associated value will be missing in the list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.