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. I got very good results with this, thanks for sharing.

    By Yasir Atabani in How to speed up tests with Django and PostgreSQL

  2. Hi Amad, for any question regarding OpenWISP, use one of the support channels: http://openwisp.org/support.html

    By Federico Capoano in How to install OpenWISP

  3. Sir please guid , i have install the ansible-openwisp2 , now how to add the access points . What is the next procedure . Please help.

    By Ahmad in How to install OpenWISP

  4. Hi Ronak, for any question regarding OpenWISP, use one of the support channels: http://openwisp.org/support.html

    By Federico Capoano in netjsonconfig: convert NetJSON to OpenWRT UCI

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

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