2016-01-25 05:26:11 +08:00
|
|
|
===================================================
|
|
|
|
``TemplateResponse`` and ``SimpleTemplateResponse``
|
|
|
|
===================================================
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
.. module:: django.template.response
|
|
|
|
:synopsis: Classes dealing with lazy-rendered HTTP responses.
|
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
Standard :class:`~django.http.HttpResponse` objects are static structures.
|
|
|
|
They are provided with a block of pre-rendered content at time of
|
|
|
|
construction, and while that content can be modified, it isn't in a form that
|
|
|
|
makes it easy to perform modifications.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
However, it can sometimes be beneficial to allow decorators or
|
|
|
|
middleware to modify a response *after* it has been constructed by the
|
|
|
|
view. For example, you may want to change the template that is used,
|
|
|
|
or put additional data into the context.
|
|
|
|
|
|
|
|
TemplateResponse provides a way to do just that. Unlike basic
|
2010-12-08 08:27:07 +08:00
|
|
|
:class:`~django.http.HttpResponse` objects, TemplateResponse objects retain
|
|
|
|
the details of the template and context that was provided by the view to
|
|
|
|
compute the response. The final output of the response is not computed until
|
2010-12-07 21:57:01 +08:00
|
|
|
it is needed, later in the response process.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``SimpleTemplateResponse`` objects
|
|
|
|
==================================
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
.. class:: SimpleTemplateResponse()
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
----------
|
|
|
|
|
|
|
|
.. attribute:: SimpleTemplateResponse.template_name
|
|
|
|
|
2015-01-10 05:59:00 +08:00
|
|
|
The name of the template to be rendered. Accepts a backend-dependent
|
|
|
|
template object (such as those returned by
|
|
|
|
:func:`~django.template.loader.get_template()`), the name of a template,
|
|
|
|
or a list of template names.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
Example: ``['foo.html', 'path/to/bar.html']``
|
|
|
|
|
|
|
|
.. attribute:: SimpleTemplateResponse.context_data
|
|
|
|
|
2015-01-10 05:59:00 +08:00
|
|
|
The context data to be used when rendering the template. It must be a
|
|
|
|
:class:`dict`.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
Example: ``{'foo': 123}``
|
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
.. attribute:: SimpleTemplateResponse.rendered_content
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
The current rendered value of the response content, using the current
|
|
|
|
template and context data.
|
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
.. attribute:: SimpleTemplateResponse.is_rendered
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
A boolean indicating whether the response content has been rendered.
|
|
|
|
|
|
|
|
Methods
|
|
|
|
-------
|
|
|
|
|
2020-09-15 18:43:37 +08:00
|
|
|
.. method:: SimpleTemplateResponse.__init__(template, context=None, content_type=None, status=None, charset=None, using=None, headers=None)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2015-01-10 05:59:00 +08:00
|
|
|
Instantiates a :class:`~django.template.response.SimpleTemplateResponse`
|
|
|
|
object with the given template, context, content type, HTTP status, and
|
|
|
|
charset.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
``template``
|
2015-01-10 05:59:00 +08:00
|
|
|
A backend-dependent template object (such as those returned by
|
|
|
|
:func:`~django.template.loader.get_template()`), the name of a template,
|
|
|
|
or a list of template names.
|
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
``context``
|
2015-01-10 05:59:00 +08:00
|
|
|
A :class:`dict` of values to add to the template context. By default,
|
|
|
|
this is an empty dictionary.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
``content_type``
|
2013-08-19 20:29:32 +08:00
|
|
|
The value included in the HTTP ``Content-Type`` header, including the
|
|
|
|
MIME type specification and the character set encoding. If
|
|
|
|
``content_type`` is specified, then its value is used. Otherwise,
|
2018-12-28 08:49:03 +08:00
|
|
|
``'text/html'`` is used.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2015-01-10 05:59:00 +08:00
|
|
|
``status``
|
|
|
|
The HTTP status code for the response.
|
|
|
|
|
2013-11-17 01:54:12 +08:00
|
|
|
``charset``
|
|
|
|
The charset in which the response will be encoded. If not given it will
|
|
|
|
be extracted from ``content_type``, and if that is unsuccessful, the
|
|
|
|
:setting:`DEFAULT_CHARSET` setting will be used.
|
|
|
|
|
2015-01-27 04:57:10 +08:00
|
|
|
``using``
|
|
|
|
The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
|
|
|
|
loading the template.
|
2013-11-17 01:54:12 +08:00
|
|
|
|
2020-09-15 18:43:37 +08:00
|
|
|
``headers``
|
|
|
|
A :class:`dict` of HTTP headers to add to the response.
|
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
.. method:: SimpleTemplateResponse.resolve_context(context)
|
|
|
|
|
2015-01-10 05:59:00 +08:00
|
|
|
Preprocesses context data that will be used for rendering a template.
|
|
|
|
Accepts a :class:`dict` of context data. By default, returns the same
|
|
|
|
:class:`dict`.
|
|
|
|
|
|
|
|
Override this method in order to customize the context.
|
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
.. method:: SimpleTemplateResponse.resolve_template(template)
|
|
|
|
|
|
|
|
Resolves the template instance to use for rendering. Accepts a
|
2015-01-10 05:59:00 +08:00
|
|
|
backend-dependent template object (such as those returned by
|
|
|
|
:func:`~django.template.loader.get_template()`), the name of a template,
|
|
|
|
or a list of template names.
|
|
|
|
|
|
|
|
Returns the backend-dependent template object instance to be rendered.
|
|
|
|
|
|
|
|
Override this method in order to customize template loading.
|
|
|
|
|
2014-01-23 05:26:10 +08:00
|
|
|
.. method:: SimpleTemplateResponse.add_post_render_callback()
|
2011-01-24 22:24:35 +08:00
|
|
|
|
|
|
|
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 :class:`~django.template.response.SimpleTemplateResponse`
|
|
|
|
has already been rendered, the callback will be invoked
|
|
|
|
immediately.
|
|
|
|
|
|
|
|
When called, callbacks will be passed a single argument -- the
|
|
|
|
rendered :class:`~django.template.response.SimpleTemplateResponse`
|
|
|
|
instance.
|
|
|
|
|
2013-03-22 17:50:45 +08:00
|
|
|
If the callback returns a value that is not ``None``, this will be
|
2011-05-25 05:28:43 +08:00
|
|
|
used as the response instead of the original response object (and
|
|
|
|
will be passed to the next post rendering callback etc.)
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
.. method:: SimpleTemplateResponse.render()
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
Sets ``response.content`` to the result obtained by
|
2011-10-12 06:21:14 +08:00
|
|
|
:attr:`SimpleTemplateResponse.rendered_content`, runs all post-rendering
|
|
|
|
callbacks, and returns the resulting response object.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
``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.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``TemplateResponse`` objects
|
|
|
|
============================
|
2010-12-08 08:27:07 +08:00
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
.. class:: TemplateResponse()
|
|
|
|
|
2013-09-30 18:44:41 +08:00
|
|
|
``TemplateResponse`` is a subclass of
|
2015-01-10 05:59:00 +08:00
|
|
|
:class:`~django.template.response.SimpleTemplateResponse` that knows about
|
|
|
|
the current :class:`~django.http.HttpRequest`.
|
2010-12-08 08:27:07 +08:00
|
|
|
|
|
|
|
Methods
|
|
|
|
-------
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2020-09-15 18:43:37 +08:00
|
|
|
.. method:: TemplateResponse.__init__(request, template, context=None, content_type=None, status=None, charset=None, using=None, headers=None)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2015-01-10 05:59:00 +08:00
|
|
|
Instantiates a :class:`~django.template.response.TemplateResponse` object
|
|
|
|
with the given request, template, context, content type, HTTP status, and
|
|
|
|
charset.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
``request``
|
|
|
|
An :class:`~django.http.HttpRequest` instance.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
``template``
|
2015-01-10 05:59:00 +08:00
|
|
|
A backend-dependent template object (such as those returned by
|
|
|
|
:func:`~django.template.loader.get_template()`), the name of a template,
|
|
|
|
or a list of template names.
|
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
``context``
|
2015-01-10 05:59:00 +08:00
|
|
|
A :class:`dict` of values to add to the template context. By default,
|
|
|
|
this is an empty dictionary.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
``content_type``
|
2013-08-19 20:29:32 +08:00
|
|
|
The value included in the HTTP ``Content-Type`` header, including the
|
|
|
|
MIME type specification and the character set encoding. If
|
2013-06-29 07:50:36 +08:00
|
|
|
``content_type`` is specified, then its value is used. Otherwise,
|
2018-12-28 08:49:03 +08:00
|
|
|
``'text/html'`` is used.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2015-01-10 05:59:00 +08:00
|
|
|
``status``
|
|
|
|
The HTTP status code for the response.
|
|
|
|
|
2013-11-17 01:54:12 +08:00
|
|
|
``charset``
|
|
|
|
The charset in which the response will be encoded. If not given it will
|
|
|
|
be extracted from ``content_type``, and if that is unsuccessful, the
|
|
|
|
:setting:`DEFAULT_CHARSET` setting will be used.
|
|
|
|
|
2015-01-27 04:57:10 +08:00
|
|
|
``using``
|
|
|
|
The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
|
|
|
|
loading the template.
|
|
|
|
|
2020-09-15 18:43:37 +08:00
|
|
|
``headers``
|
|
|
|
A :class:`dict` of HTTP headers to add to the response.
|
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
The rendering process
|
|
|
|
=====================
|
|
|
|
|
2010-12-08 08:27:07 +08:00
|
|
|
Before a :class:`~django.template.response.TemplateResponse` instance can be
|
|
|
|
returned to the client, it must be rendered. The rendering process takes the
|
|
|
|
intermediate representation of template and context, and turns it into the
|
|
|
|
final byte stream that can be served to the client.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
There are three circumstances under which a ``TemplateResponse`` will be
|
2010-12-07 21:57:01 +08:00
|
|
|
rendered:
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
* When the ``TemplateResponse`` instance is explicitly rendered, using
|
2011-10-14 08:12:01 +08:00
|
|
|
the :meth:`SimpleTemplateResponse.render()` method.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* When the content of the response is explicitly set by assigning
|
2013-01-01 21:12:42 +08:00
|
|
|
``response.content``.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* After passing through template response middleware, but before
|
|
|
|
passing through response middleware.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
A ``TemplateResponse`` can only be rendered once. The first call to
|
|
|
|
:meth:`SimpleTemplateResponse.render` sets the content of the response;
|
|
|
|
subsequent rendering calls do not change the response content.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
However, when ``response.content`` is explicitly assigned, the
|
2010-12-07 21:57:01 +08:00
|
|
|
change is always applied. If you want to force the content to be
|
|
|
|
re-rendered, you can re-evaluate the rendered content, and assign
|
|
|
|
the content of the response manually::
|
|
|
|
|
2010-12-09 08:47:37 +08:00
|
|
|
# Set up a rendered TemplateResponse
|
2013-05-18 20:00:52 +08:00
|
|
|
>>> from django.template.response import TemplateResponse
|
2010-12-07 21:57:01 +08:00
|
|
|
>>> t = TemplateResponse(request, 'original.html', {})
|
|
|
|
>>> t.render()
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(t.content)
|
2010-12-07 21:57:01 +08:00
|
|
|
Original content
|
|
|
|
|
2010-12-09 08:47:37 +08:00
|
|
|
# Re-rendering doesn't change content
|
2010-12-07 21:57:01 +08:00
|
|
|
>>> t.template_name = 'new.html'
|
|
|
|
>>> t.render()
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(t.content)
|
2010-12-07 21:57:01 +08:00
|
|
|
Original content
|
|
|
|
|
|
|
|
# Assigning content does change, no render() call required
|
|
|
|
>>> t.content = t.rendered_content
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(t.content)
|
2010-12-07 21:57:01 +08:00
|
|
|
New content
|
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
Post-render callbacks
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
Some operations -- such as caching -- cannot be performed on an
|
|
|
|
unrendered template. They must be performed on a fully complete and
|
|
|
|
rendered response.
|
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
If you're using middleware, you can do that. Middleware provides
|
2011-01-24 22:24:35 +08:00
|
|
|
multiple opportunities to process a response on exit from a view. If
|
2016-04-18 16:39:42 +08:00
|
|
|
you put behavior in the response middleware, it's guaranteed to execute
|
2011-01-24 22:24:35 +08:00
|
|
|
after template rendering has taken place.
|
|
|
|
|
|
|
|
However, if you're using a decorator, the same opportunities do not
|
|
|
|
exist. Any behavior defined in a decorator is handled immediately.
|
|
|
|
|
|
|
|
To compensate for this (and any other analogous use cases),
|
|
|
|
:class:`TemplateResponse` allows you to register callbacks that will
|
|
|
|
be invoked when rendering has completed. Using this callback, you can
|
|
|
|
defer critical processing until a point where you can guarantee that
|
|
|
|
rendered content will be available.
|
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
To define a post-render callback, define a function that takes
|
2011-01-24 22:24:35 +08:00
|
|
|
a single argument -- response -- and register that function with
|
|
|
|
the template response::
|
|
|
|
|
2013-05-18 20:00:52 +08:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
|
2011-01-24 22:24:35 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
``my_render_callback()`` will be invoked after the ``mytemplate.html``
|
|
|
|
has been rendered, and will be provided the fully rendered
|
|
|
|
:class:`TemplateResponse` instance as an argument.
|
|
|
|
|
|
|
|
If the template has already been rendered, the callback will be
|
|
|
|
invoked immediately.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
Using ``TemplateResponse`` and ``SimpleTemplateResponse``
|
|
|
|
=========================================================
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2015-01-10 05:59:00 +08:00
|
|
|
A :class:`TemplateResponse` object can be used anywhere that a normal
|
|
|
|
:class:`django.http.HttpResponse` can be used. It can also be used as an
|
2015-12-22 23:21:24 +08:00
|
|
|
alternative to calling :func:`~django.shortcuts.render()`.
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
For example, the following view returns a :class:`TemplateResponse` with a
|
|
|
|
template and a context containing a queryset::
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
|
|
|
|
def blog_index(request):
|
|
|
|
return TemplateResponse(request, 'entry_list.html', {'entries': Entry.objects.all()})
|