request.POST get multiple values
Posted: February 24th, 2010 | Author: Davo | Filed under: Django | Tags: getlist, querydict, request.post | 7 Comments »The QueryDict.getlist() allows to get all the checkbox(or select list) values from the request.POST/GET object.
Let’s assume we have a simple form with the following checkboxes. Each checkbox contains an ID of an artist.
In views.py :
def handle(request):
if request.method == 'POST':
artists = request.POST.getlist('artists') # now artists is a list of [1,2,3]

@Davo
Thanks.
Thats was totally #$%& usefull for me
I have a question – what would be the best way in the case of GET (form with action GET) to convert the list back to a proper URL? urlencode doesn’t work – it converts the list to string ‘[1,2,3]‘ which in turn is’nt recognized properly by getlist anymore.
Hello SpiegS,
What you can do is the following:
>>> from django.http import QueryDict
>>> q = QueryDict(‘artists=1′).copy()
>>> my_artists = [11,22,33,44]
>>> q.setlist(‘artists’, my_artists)
>>> print q
<QueryDict: {u’artists’: [11, 22, 33, 44]}>
>>> print q.urlencode()
artists=11&artists=22&artists=33&artists=44
Hope this helps.
Thx. Didn’t knew this.
you learn something everyday..
thanks a lot
Thank you! Just what i was looking for.
thanks a lot, i was looking for it. saved me some time.