Django: How to Retrieve Query String Parameters

3rd October 2010 in Coding Tags: django, programming, python

If you use Django you will surely know that its url routing is quite strict regarding the prettiness of its URLS.

An url that in a php application could be like:

"index.php?option=content&id=7"

With django would translate:

/content/7/

The nice thing is that this pretty django URLs don't cause significant overhead, at least not as by doing the same with Php, MySQL and Apache (example: Joomla).

So how to retrieve query string params with Django?

There are still cases in we need to handle query string parameters, like when dealing with external services that return a URL like:

callback?verification_code=088145235648fs689sa56h&userid=854 

Following this example, create the "callback" URL in your urlconf:

# urls.py

from django.conf.urls.defaults import *

# import your view to pass to the URL
from views import callback

urlpatterns = patterns('',
    url(r'^callback$', view=callback, name='callback'),
	# your other URLS
	#
	#
)

Then create the view that retrieves the parameters:

# views.py

from django.template import Context, RequestContext
from django.shortcuts import render_to_response, get_object_or_404

# your other views
#
#

# callback
def callback(request):

    verification_code = request.GET.get('verification_code')
    userid = request.GET.get('userid')

    context = {
        'verification_code': verification_code,
        'userid': verification_url,
    }

    return render_to_response('callback.html', context, context_instance=RequestContext(request))

And then create the template that shows the result. Be sure to put in one of your TEMPLATE_DIRS.

<!-- callback.html -->

verification_code: {{ verification_code }}<br />
verification_url: {{ userid }}

Very easy indeed and Django takes care of filtering the data to avoid XSS attacks or SQL Injection automatically for you! Isn't that nice?

Retweet

Comments

  1. 1.

    Kerala said:

    ( on 2nd of May 2012 at 12:58 )

    I just wanted to say that your blog has been really useful.

Leave your comment

Categories

Let's be social

Popular posts

Latest Comments

  1. nice site i really learn a lot more power to your blog <a href="http://staying-healthy.info">healthy living</a>

    By health is wealth in Nice Skip Links Appearing on :focus with CSS

  2. Very strange bug- thanks for the fix!

    By Andrew Chart in IE8 doesn't like 1x1px semi-transparent backgrounds

  3. Completely agree about keeping it simple, sometimes people try a little too hard

    By ryan in 10 Effective Business Card Design Tips

  4. I just wanted to say that your blog has been really useful.

    By Kerala in Django: How to Retrieve Query String Parameters

  5. Thank you this was exactly what I have been searching for.

    By Kerala in Django Tagging Autocomplete Tag-It

Popular Tags

creativity django google-summer-of-code ibiza inspiration jquery nemesisdesign ninux open-source performance photo programming python security staticgenerator upload wireless-community