Primary image for Getting RequestContext in Your Templates

Getting RequestContext in Your Templates

Lately, we’ve been taking over projects from people who began building their first site in Django, but either got in over their head or just found they didn’t have the time to continue. As I review the existing code, the first issue I typically see is using the render_to_response shortcut without including the RequestContext, preventing context processors from being used in the templates. The standard symptom is when people can’t access MEDIA_URL in their templates.

Here are a few ways to add RequestContext to your templates.

Option #1: Adding RequestContext to render_to_response

The Django documentation recommends passing RequestContext as an argument to render_to_response like this:

from django.shortcuts import render_to_response
from django.template import RequestContext

def my_view(request):
# View code here...
return render_to_response('my_template.html',
This works, but as you can see, it adds a fair amount of repetitive code to your views.

Option #2: Wrapper for render_to_response

Don’t repeat yourself. Create a wrapper like this, courtesy of Django Snippet #3

from django.shortcuts import render_to_response
from django.template import RequestContext

def render_response(req, *args, **kwargs):
kwargs['context_instance'] = RequestContext(req)
return render_to_response(*args, **kwargs)

Option #3: Use Generic Views

Django’s generic views include RequestContext by default. Although the docs show them being used in, you can just as easily use them in your views. James Bennet gave some examples of this technique on his blog back in ’06. It looks something like this:

from django.views.generic import list_detail

def my_view(request):
# View code here...
return list_detail.object_list_(request,
Most views can be distilled down to either a single object or list of objects with some extra variables thrown in. When they can’t, simply use direct_to_template.

Option #4: Getting Creative with Decorators

I came across a technique I hadn’t seen before a couple of days ago when poking around django-cashflow. It monkey patches render_to_response using option #1 above, then creates uses a decorator for it. The code looks like this:

def render_to(template_name):
def renderer(func):
def wrapper(request, *args, **kw):
output = func(request, *args, **kw)
if not isinstance(output, dict):
return output
return render_to_response(request, template_name, output)
return wrapper
return renderer

def my_view(request):
# View code here...
return some_dict
Pretty slick.

How About You?

I use generic views for a few of reasons.

  • It is less verbose than option #1
  • Options 2 & 4 are clever, but I prefer to use built-ins whenever possible. One of the reasons I chose Python over Ruby is because it isn’t a TIMTOWTDI language. Wrappers and shortcuts are great, but they can also make it more difficult for someone else to jump into your code and start working. With generic views, you get sane defaults and everything is already fully documented and clear to other Django developers.
  • They provide a standardized naming scheme for your templates. Again allowing other developers to jump right into your code without having to learn your style.

So how about you? What technique do you use and why?

Peter Baumgartner

About the author

Peter Baumgartner

Peter is the founder of Lincoln Loop, having built it up from a small freelance operation in 2007 to what it is today. He is constantly learning and is well-versed in many technical disciplines including devops, …