Login

render_to_response wrapper

Author:
Magus
Posted:
February 25, 2007
Language:
Python
Version:
Pre .96
Score:
20 (after 28 ratings)

Simplifies using RequestContext in render_to_response.

Simply call the wrapper with request as the first argument, and the rest of the arguments are as normal for render_to_response.

ex: render_response(request, 'foo_list.html', {'foo': Foo.objects.all()})

1
2
3
4
5
6
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)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

stereoit (on July 18, 2007):

+1 from me as I do not have to pass the RequestContext all the time, specially handy for newbie. I spent some time debugging why the authentication is not persistent across the requests just to find out I forgot add RequestContext.

with previous comment I stil lhave to use:

return direct_to_template(request, 'template' , {'context_instance':RequestContext(request), 'form':form})

right?

#

stereoit (on July 18, 2007):

fix to my previous comment, I didn't catch that the RequestContext is in the return statement of direct_to_template. So indeed it looks like this snippet is already in django in some way ;)

#

blcline7156 (on April 11, 2009):

-1 whiteinge has it right, this function adds nothing but confusion to future code, especially since it is so close to the render_to_response function name and is barely less verbose than render_to_response('template', {}, RequestContext(request)).

Use either direct_to_template() or render_to_response('template', {}, RequestContext(request)).

#

Please login first before commenting.