Django: How to Retrieve Query String Parameters

6comments

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.

  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.

Categories

Let's be social

Popular posts

Latest Comments

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

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

  2. 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

  3. Send any question to the interop-dev mailing list or open an issue on github.

    By Federico in Network Topology Visualizer: django-netjsongraph

  4. I have a question about Network Topology Collector, can you brief me pls?

    By Nasrin Akter in Network Topology Visualizer: django-netjsongraph

  5. Thanks !

    By Alois in How to speed up tests with Django and PostgreSQL

Popular Tags

battlemesh censorship creativity criptography django event 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 wireless-community