Login

simplified render_to_response with RequestContext

Author:
jasongreen
Posted:
December 30, 2009
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

manything need to do with RequestContext, but it's too tedious. use render_to_response("/my.html", {'key':value,},request) instead of render_to_response("/my.html", {'key':value,},new RequestContext(request)) and you can also use render_to_response("/my.html", {'key':value,},new RequestContext(request))

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# coding:utf-8
'''
Created on 2009-12-31

@author: Jason Green
@author-email: [email protected]

use
`return render_to_response("/my.html", {'key':value,},request)`
instead of
`return render_to_response("/my.html", {'key':value,},new RequestContext(request))`
and you can also use
`return render_to_response("/my.html", {'key':value,},new RequestContext(request))`
'''
import django.template.loader as djloader
from django.template.loader import render_to_string as _original_render_to_string
from django.template.context import RequestContext
def render_to_string(template_name, dictionary=None, context_instance=None):
    if isinstance(context_instance,HttpRequest):
        context_instance = RequestContext(context_instance)
    return _original_render_to_string(template_name, dictionary, context_instance)
djloader.render_to_string = render_to_string

More like this

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

Comments

romain-hardouin (on January 7, 2010):

new keyword does not exist in Python...

#

Please login first before commenting.