Professional Documents
Culture Documents
Instantiates a
:class:`~django.template.response.SimpleTemplateResponse` object
with the given template, context, content type, and HTTP status.
``template``
The full name of a template, or a sequence of template names.
:class:`~django.template.Template` instances can also be used.
``context``
A dictionary of values to add to the template context. By default,
this is an empty dictionary. :class:`~django.template.Context` objects
are also accepted as ``context`` values.
``status``
The HTTP Status code for the response.
``content_type``
.. versionchanged:: 1.5
Historically, this parameter was only called ``mimetype`` (now
deprecated), but since this is actually the value included in the HTTP
``Content-Type`` header, it can also include the character set
encoding, which makes it more than just a MIME type specification. If
``mimetype`` is specified (not ``None``), that value is used.
Otherwise, ``content_type`` is used. If neither is given,
:setting:`DEFAULT_CONTENT_TYPE` is used.
.. method:: SimpleTemplateResponse.resolve_context(context)
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 :class:`~django.template.Context`
instance containing the provided data.
Override this method in order to customize context instantiation.
.. method:: SimpleTemplateResponse.resolve_template(template)
Resolves the template instance to use for rendering. Accepts a
path of a template to use, or a sequence of template paths.
:class:`~django.template.Template` instances may also be provided.
Returns the :class:`~django.template.Template` instance to be
rendered.
Override this method in order to customize template rendering.
.. method:: SimpleTemplateResponse.add_post_render_callback
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.
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.)
.. method:: SimpleTemplateResponse.render()
Sets ``response.content`` to the result obtained by
:attr:`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 objects
========================
.. class:: TemplateResponse()
TemplateResponse is a subclass of
:class:`~django.template.response.SimpleTemplateResponse` that uses
a :class:`~django.template.RequestContext` instead of
a :class:`~django.template.Context`.
Methods
------.. method:: TemplateResponse.__init__(request, template, context=None, content_t
ype=None, status=None, current_app=None)
Instantiates an ``TemplateResponse`` object with the given
template, context, MIME type and HTTP status.
``request``
An :class:`~django.http.HttpRequest` instance.
``template``
The full name of a template, or a sequence of template names.
:class:`~django.template.Template` instances can also be used.
``context``
A dictionary of values to add to the template context. By default,
this is an empty dictionary. :class:`~django.template.Context` objects
are also accepted as ``context`` values.
``status``
The HTTP Status code for the response.
``content_type``
.. versionchanged:: 1.5
Historically, this parameter was only called ``mimetype`` (now
deprecated), but since this is actually the value included in the HTTP
``Content-Type`` header, it can also include the character set
encoding, which makes it more than just a MIME type specification. If
``mimetype`` is specified (not ``None``), that value is used.