Ramblings

April 25, 2008

A Django HttpRequest QueryDict is not your normal Python dict – i.e. getting ids from a friend request-form

Filed under: dev, django, facebook, python, snippet, tip — michaelangela @ 5:34 pm

Doesn’t work:

ids=request.GET['ids[]']

Does work:

ids=request.GET.getlist('ids[]')

A typical Facebook friend invite sends the ids to be invited to your site as an array in 'ids[]'. Normally, you can pull values out with just request.GET['key']. But since the value of this particular key is an array and not just a string, you can’t (at least I wasn’t able to) just do ids=request.GET['ids[]']. That returns the first value of the array as a string. Not very helpful.

But, this is a QueryDict object, not a standard {} dict. As such, Django provides the getlist(key) method.

Django | Request and response objects | Django Documentation

getlist(key) — Returns the data with the requested key, as a Python
list. Returns an empty list if the key doesn’t exist. It’s guaranteed to
return a list of some sort.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: