Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

45 lines (37 sloc) 1.606 kB
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseNotFound
from django.utils import simplejson
from django.template import TemplateDoesNotExist
def render_to(template):
Decorator for Django views that sends returned dict to render_to_response function
with given template and RequestContext as context instance.
If view doesn't return dict then decorator simply returns output.
Additionally view can return two-tuple, which must contain dict as first
element and string with template name as second. This string will
override template name, given as parameter
- template: template name to use
def renderer(func):
def wrapper(request, *args, **kw):
output = func(request, *args, **kw)
if isinstance(output, (list, tuple)):
return render_to_response(output[1], output[0], RequestContext(request))
elif isinstance(output, dict):
return render_to_response(template, output, RequestContext(request))
except TemplateDoesNotExist:
return HttpResponseNotFound()
return output
return wrapper
return renderer
def render_json(func):
def wrapper(request, *args, **kw):
data = func(request, *args, **kw)
if isinstance(data, HttpResponse):
return data
return HttpResponse(
simplejson.dumps(data), mimetype='text/javascript')
return wrapper
Jump to Line
Something went wrong with that request. Please try again.