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:


With django would translate:


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:


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


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:


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?



  1. 1.

    Kerala said:

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

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

  2. 2.

    Flashmurphy said:

    ( on 9th of October 2012 at 17:53 )

    Hey, thanks man. I was trying to figure this out for a pretty ridiculous about of time until I found this page!!! Thanks for posting.

  3. 3.

    goll said:

    ( on 19th of August 2013 at 17:41 )

    It's ultra mega nice :)

  4. 4.

    goll said:

    ( on 19th of August 2013 at 17:45 )

    Where is this 'verification_url' coming from?

  5. 5.

    drum said:

    ( on 24th of July 2014 at 07:46 )

    Helpful blog.
    But if I am using classes then how to get query string as it throws error If i do the same in classes like
    class view():
    def callback(request):

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

  6. 6.

    Willie said:

    ( on 18th of February 2015 at 06:57 )

    You da man!!!

Comments are closed.

Comments have been closed for this post.


Let's be social

Popular posts

Latest Comments

  1. Hi Ronak, for any question regarding OpenWISP, use one of the support channels:

    By Federico Capoano in netjsonconfig: convert NetJSON to OpenWRT UCI

  2. Hi, I have installed openwisp controller using ansible playbook. Now, i am adding the configurations automatically using OPENWRT devices in openwisp file by specifying shared_key so can you suggest me if I want to set limit to add configuration how can i do it?

    By Ronak in netjsonconfig: convert NetJSON to OpenWRT UCI

  3. Hi Julio! I missed your comment a few years ago but I'm glad you are working with OpenWISP, I'll try to reach you in private :-)

    By Federico Capoano in A Turning Point in my Life, Community Networks and OpenWISP

  4. Great news Aymará! Very happy to know this post has inspired you to experiment :-)

    By Federico Capoano in First DjangoGirls Rome wrap-up & afterthoughts

  5. Hi!! I'm a Django Girls coach too. Here, in Argentina, made just what you suggested, splited the workshop in two days. The experiment went just great! Most of the girls achieved to publish the blog from ground 0. It feels great to be helpfull ...

    By Aymará in First DjangoGirls Rome wrap-up & afterthoughts

Popular Tags

battlemesh censorship creativity criptography django event fosdem google-summer-of-code ibiza inspiration javascript jquery linux nemesisdesign netjson ninux nodeshot open-source openwisp openwrt performance photo programming python security staticgenerator talk upload wifi wireless-community