How to print url ‘GET’ variables in Django template

There are two ways to achieve this:

1. By sending the request object as part of the context dictionary that will be read by the template, like this:

def yourView(request):
c = {}
...
c["request"] = request
return render_to_response("yourtemplate.html", c, context_instance=RequestContext(request))

2. By activating the django.core.context_processors.request template context processor, which will automatically send the request variable for you to the template, instead of making you send it manually:

TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
)

Both methods will allow you to print a GET variable. For example, if your url is http://localhost:8000/?foo=1234 , then you can print the variable foo in your template like this:

{{ request.GET.foo }}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: