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 can make more customers for your business without ripping your pocket I Can Contact for more details? David

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

  2. Adult blog with daily updates daily quotes of wisdom what antonyms and synonyms are underwear man http://sissyabuse.blogporn.in/?profile.iyana sequined skirt free porno of girls herbs estrogen hardsteel chastity wife cheat toronto breast implants gallery of contemporary art mens sexo

    By suzannetu2 in OpenWISP at IETF Bangkok

  3. I got very good results with this, thanks for sharing.

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

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

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

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