storm
storm

Reputation: 65

Can't recover list of items from HTTP POST request

I'm working on a Django app.

In the frontend, I've got a HTML form with some checkboxes that go something like:

<input type="checkbox" name="list" value="item1" />Item one<br/>
<input type="checkbox" name="list" value="item2" />Item two<br/>

Upon submitting, the HTTP POST request as seen from the Django backend looks like (let's say I've checked both items):

POST:<QueryDict: {u'list': [u'item1', u'item2']}>

I'm trying to recover the list:

list = request.POST['list']

For some reason I only seem to get the last item from the list:

['item2']

I also tried with

 request.POST.get('list', '')

with the same result.

Anyone understands what's going on?

Upvotes: 1

Views: 388

Answers (1)

jan zegan
jan zegan

Reputation: 1657

Try:

list = request.POST.getlist('list')

also consider changing the name of variable list to something else, it's a built in type.

Upvotes: 2

Related Questions