How to pass a GET param that contains multiple items

5,377 views
Skip to first unread message

Margie Roginski

unread,
Jul 10, 2010, 6:40:40 PM7/10/10
to Django users
I have a url in my app that needs to get info from a GET param. For
example, let's say my url is retrieving books by any of a set of
authors, so the url might be this to get books authored by smith,
johnson, or klein:

www.example.com/books/?author=smith+johnson+klein

I notice that when I look at request.GET.get('author') on the server,
the '+' is gone and replaced by space:

<QueryDict: {'u'author': [u'foo bar']}>

Is this django doing this for me or is this some sort of general http
protocal thing?

My main question is just - what's the accepted way to pass in a get
parameter that contains a bunch of times. What if the parameter
itself has spaces? I've seen this '+' used - is that standard or just
personal preference?

Thanks,
Margie

euan.g...@gmail.com

unread,
Jul 11, 2010, 5:00:12 AM7/11/10
to Django users
This is a standard encode/decode situation you are descibing. Django
automatically decodes the GET string the the browser encodes. If you
need spaces, then they wil be encoded and decoded appropriately, so
don't worry about that.

If you want to pass a list in the GET string, do:

url?var=1&var=2&var=3

Django will intepret this in it's multi-value dict implementation that
QueryDict uses. So if you do:

request.GET.getlist('var')

you will get:

['1', '2', '3']

Hope that helps, Euan

Margie Roginski

unread,
Jul 12, 2010, 12:27:42 PM7/12/10
to Django users

Thanks, Euan!

Margie

On Jul 11, 2:00 am, "euan.godd...@googlemail.com"
Reply all
Reply to author
Forward
0 new messages