The name of the template to be rendered. Accepts a Template object, a path to a template or list of template paths.
Example: ['foo.html', 'path/to/bar.html']
The context data to be used when rendering the template. It can be a dictionary or a context object.
Example: {'foo': 123}
The current rendered value of the response content, using the current template and context data.
A boolean indicating whether the response content has been rendered.
Instantiates a SimpleTemplateResponse object with the given template, context, MIME type and HTTP status.
Converts context data into a context instance that can be used for rendering a template. Accepts a dictionary of context data or a context object. Returns a Context instance containing the provided data.
Override this method in order to customize context instantiation.
Resolves the template instance to use for rendering. Accepts a path of a template to use, or a sequence of template paths. Template instances may also be provided. Returns the Template instance to be rendered.
Override this method in order to customize template rendering.
Add a callback that will be invoked after rendering has taken place. This hook can be used to defer certain processing operations (such as caching) until after rendering has occurred.
If the SimpleTemplateResponse has already been rendered, the callback will be invoked immediately.
When called, callbacks will be passed a single argument – the rendered SimpleTemplateResponse instance.
If the callback returns a value that is not None, this will be used as the response instead of the original response object (and will be passed to the next post rendering callback etc.)
Sets response.content to the result obtained by SimpleTemplateResponse.rendered_content, runs all post-rendering callbacks, and returns the resulting response object.
render() will only have an effect the first time it is called. On subsequent calls, it will return the result obtained from the first call.
TemplateResponse is a subclass of SimpleTemplateResponse that uses a RequestContext instead of a Context.
Instantiates an TemplateResponse object with the given template, context, MIME type and HTTP status.
# Set up a rendered TemplateResponse
>>> t = TemplateResponse(request, 'original.html', {})
>>> t.render()
>>> print t.content
Original content
# Re-rendering doesn't change content
>>> t.template_name = 'new.html'
>>> t.render()
>>> print t.content
Original content
# Assigning content does change, no render() call required
>>> t.content = t.rendered_content
>>> print t.content
New content
def my_render_callback(response):
# Do content-sensitive processing
do_post_processing()
def my_view(request):
# Create a response
response = TemplateResponse(request, 'mytemplate.html', {})
# Register the callback
response.add_post_render_callback(my_render_callback)
# Return the response
return response
from django.template.response import TemplateResponse
def blog_index(request):
return TemplateResponse(request, 'entry_list.html', {'entries': Entry.objects.all()})
Aug 21, 2013