Django view shortcuts: render_to_response vs. render

If you’re using Django 1.3+, and find your code littered with

from django.shortcuts import render_to_response

def my_view(request):
...
  return render_to_response('my_template.html', my_data,
    context_instance=RequestContext(request))

you can replace that with a call to the render() shortcut, introduced in Django 1.3:


from django.shortcuts import render

def my_view(request):
...
  return render(request, 'my_template.html', my_data)

You should generally prefer render() over render_to_response() – with render_to_response(), if you leave off the context_instance param, Django won’t invoke any of the default template context processors.

See the docs for more details.

Advertisements