Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 65530c6176058f9b54858c3b4f6385e6 > files > 787

python-django-doc-1.8.19-1.mga6.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" lang="">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>TemplateResponse and SimpleTemplateResponse &#8212; Django 1.8.19 documentation</title>
    
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.8.19',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="top" title="Django 1.8.19 documentation" href="../contents.html" />
    <link rel="up" title="API Reference" href="index.html" />
    <link rel="next" title="Unicode data" href="unicode.html" />
    <link rel="prev" title="Upgrading templates to Django 1.8" href="templates/upgrading.html" />



 
<script type="text/javascript" src="../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body role="document">

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 1.8.19 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="templates/upgrading.html" title="Upgrading templates to Django 1.8">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="unicode.html" title="Unicode data">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-template-response">
            
  <div class="section" id="s-module-django.template.response">
<span id="s-templateresponse-and-simpletemplateresponse"></span><span id="module-django.template.response"></span><span id="templateresponse-and-simpletemplateresponse"></span><h1>TemplateResponse and SimpleTemplateResponse<a class="headerlink" href="#module-django.template.response" title="Permalink to this headline">¶</a></h1>
<p>Standard <a class="reference internal" href="request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></code></a> 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&#8217;t in a form that
makes it easy to perform modifications.</p>
<p>However, it can sometimes be beneficial to allow decorators or
middleware to modify a response <em>after</em> 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.</p>
<p>TemplateResponse provides a way to do just that. Unlike basic
<a class="reference internal" href="request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></code></a> 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
it is needed, later in the response process.</p>
<div class="section" id="s-simpletemplateresponse-objects">
<span id="simpletemplateresponse-objects"></span><h2>SimpleTemplateResponse objects<a class="headerlink" href="#simpletemplateresponse-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.template.response.SimpleTemplateResponse">
<em class="property">class </em><code class="descname">SimpleTemplateResponse</code><a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="section" id="s-attributes">
<span id="attributes"></span><h3>Attributes<a class="headerlink" href="#attributes" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.template.response.SimpleTemplateResponse.template_name">
<code class="descclassname">SimpleTemplateResponse.</code><code class="descname">template_name</code><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.template_name" title="Permalink to this definition">¶</a></dt>
<dd><p>The name of the template to be rendered. Accepts a backend-dependent
template object (such as those returned by
<a class="reference internal" href="../topics/templates.html#django.template.loader.get_template" title="django.template.loader.get_template"><code class="xref py py-func docutils literal"><span class="pre">get_template()</span></code></a>), the name of a template,
or a list of template names.</p>
<p>Example: <code class="docutils literal"><span class="pre">['foo.html',</span> <span class="pre">'path/to/bar.html']</span></code></p>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.8: </span><code class="docutils literal"><span class="pre">template_name</span></code> used to accept a <a class="reference internal" href="templates/api.html#django.template.Template" title="django.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a>.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.template.response.SimpleTemplateResponse.context_data">
<code class="descclassname">SimpleTemplateResponse.</code><code class="descname">context_data</code><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.context_data" title="Permalink to this definition">¶</a></dt>
<dd><p>The context data to be used when rendering the template. It must be a
<code class="xref py py-class docutils literal"><span class="pre">dict</span></code>.</p>
<p>Example: <code class="docutils literal"><span class="pre">{'foo':</span> <span class="pre">123}</span></code></p>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.8: </span><code class="docutils literal"><span class="pre">context_data</span></code> used to accept a <a class="reference internal" href="templates/api.html#django.template.Context" title="django.template.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a>.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.template.response.SimpleTemplateResponse.rendered_content">
<code class="descclassname">SimpleTemplateResponse.</code><code class="descname">rendered_content</code><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.rendered_content" title="Permalink to this definition">¶</a></dt>
<dd><p>The current rendered value of the response content, using the current
template and context data.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.template.response.SimpleTemplateResponse.is_rendered">
<code class="descclassname">SimpleTemplateResponse.</code><code class="descname">is_rendered</code><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.is_rendered" title="Permalink to this definition">¶</a></dt>
<dd><p>A boolean indicating whether the response content has been rendered.</p>
</dd></dl>

</div>
<div class="section" id="s-methods">
<span id="methods"></span><h3>Methods<a class="headerlink" href="#methods" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.template.response.SimpleTemplateResponse.__init__">
<code class="descclassname">SimpleTemplateResponse.</code><code class="descname">__init__</code>(<em>template</em>, <em>context=None</em>, <em>content_type=None</em>, <em>status=None</em>, <em>charset=None</em>, <em>using=None</em>)<a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.__init__"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Instantiates a <a class="reference internal" href="#django.template.response.SimpleTemplateResponse" title="django.template.response.SimpleTemplateResponse"><code class="xref py py-class docutils literal"><span class="pre">SimpleTemplateResponse</span></code></a>
object with the given template, context, content type, HTTP status, and
charset.</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">template</span></code></dt>
<dd><p class="first">A backend-dependent template object (such as those returned by
<a class="reference internal" href="../topics/templates.html#django.template.loader.get_template" title="django.template.loader.get_template"><code class="xref py py-func docutils literal"><span class="pre">get_template()</span></code></a>), the name of a template,
or a list of template names.</p>
<div class="last deprecated">
<p><span class="versionmodified">Deprecated since version 1.8: </span><code class="docutils literal"><span class="pre">template</span></code> used to accept a <a class="reference internal" href="templates/api.html#django.template.Template" title="django.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a>.</p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">context</span></code></dt>
<dd><p class="first">A <code class="xref py py-class docutils literal"><span class="pre">dict</span></code> of values to add to the template context. By default,
this is an empty dictionary.</p>
<div class="last deprecated">
<p><span class="versionmodified">Deprecated since version 1.8: </span><code class="docutils literal"><span class="pre">context</span></code> used to accept a <a class="reference internal" href="templates/api.html#django.template.Context" title="django.template.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a>.</p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">content_type</span></code></dt>
<dd>The value included in the HTTP <code class="docutils literal"><span class="pre">Content-Type</span></code> header, including the
MIME type specification and the character set encoding. If
<code class="docutils literal"><span class="pre">content_type</span></code> is specified, then its value is used. Otherwise,
<a class="reference internal" href="settings.html#std:setting-DEFAULT_CONTENT_TYPE"><code class="xref std std-setting docutils literal"><span class="pre">DEFAULT_CONTENT_TYPE</span></code></a> is used.</dd>
<dt><code class="docutils literal"><span class="pre">status</span></code></dt>
<dd>The HTTP status code for the response.</dd>
<dt><code class="docutils literal"><span class="pre">charset</span></code></dt>
<dd>The charset in which the response will be encoded. If not given it will
be extracted from <code class="docutils literal"><span class="pre">content_type</span></code>, and if that is unsuccessful, the
<a class="reference internal" href="settings.html#std:setting-DEFAULT_CHARSET"><code class="xref std std-setting docutils literal"><span class="pre">DEFAULT_CHARSET</span></code></a> setting will be used.</dd>
<dt><code class="docutils literal"><span class="pre">using</span></code></dt>
<dd>The <a class="reference internal" href="settings.html#std:setting-TEMPLATES-NAME"><code class="xref std std-setting docutils literal"><span class="pre">NAME</span></code></a> of a template engine to use for
loading the template.</dd>
</dl>
<div class="versionchanged">
<span class="title">Changed in Django 1.8:</span> <p>The <code class="docutils literal"><span class="pre">charset</span></code> and <code class="docutils literal"><span class="pre">using</span></code> parameters were added.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="django.template.response.SimpleTemplateResponse.resolve_context">
<code class="descclassname">SimpleTemplateResponse.</code><code class="descname">resolve_context</code>(<em>context</em>)<a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.resolve_context"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.resolve_context" title="Permalink to this definition">¶</a></dt>
<dd><p>Preprocesses context data that will be used for rendering a template.
Accepts a <code class="xref py py-class docutils literal"><span class="pre">dict</span></code> of context data. By default, returns the same
<code class="xref py py-class docutils literal"><span class="pre">dict</span></code>.</p>
<p>Override this method in order to customize the context.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.8:</span> <p><code class="docutils literal"><span class="pre">resolve_context</span></code> returns a <code class="xref py py-class docutils literal"><span class="pre">dict</span></code>. It used to return a
<a class="reference internal" href="templates/api.html#django.template.Context" title="django.template.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a>.</p>
</div>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.8: </span><code class="docutils literal"><span class="pre">resolve_context</span></code> no longer accepts a
<a class="reference internal" href="templates/api.html#django.template.Context" title="django.template.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a>.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="django.template.response.SimpleTemplateResponse.resolve_template">
<code class="descclassname">SimpleTemplateResponse.</code><code class="descname">resolve_template</code>(<em>template</em>)<a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.resolve_template"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.resolve_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Resolves the template instance to use for rendering. Accepts a
backend-dependent template object (such as those returned by
<a class="reference internal" href="../topics/templates.html#django.template.loader.get_template" title="django.template.loader.get_template"><code class="xref py py-func docutils literal"><span class="pre">get_template()</span></code></a>), the name of a template,
or a list of template names.</p>
<p>Returns the backend-dependent template object instance to be rendered.</p>
<p>Override this method in order to customize template loading.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.8:</span> <p><code class="docutils literal"><span class="pre">resolve_template</span></code> returns backend-dependent template object. It
used to return a <a class="reference internal" href="templates/api.html#django.template.Template" title="django.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a>.</p>
</div>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.8: </span><code class="docutils literal"><span class="pre">resolve_template</span></code> no longer accepts a
<a class="reference internal" href="templates/api.html#django.template.Template" title="django.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a>.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="django.template.response.SimpleTemplateResponse.add_post_render_callback">
<code class="descclassname">SimpleTemplateResponse.</code><code class="descname">add_post_render_callback</code>()<a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.add_post_render_callback"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.add_post_render_callback" title="Permalink to this definition">¶</a></dt>
<dd><p>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.</p>
<p>If the <a class="reference internal" href="#django.template.response.SimpleTemplateResponse" title="django.template.response.SimpleTemplateResponse"><code class="xref py py-class docutils literal"><span class="pre">SimpleTemplateResponse</span></code></a>
has already been rendered, the callback will be invoked
immediately.</p>
<p>When called, callbacks will be passed a single argument &#8211; the
rendered <a class="reference internal" href="#django.template.response.SimpleTemplateResponse" title="django.template.response.SimpleTemplateResponse"><code class="xref py py-class docutils literal"><span class="pre">SimpleTemplateResponse</span></code></a>
instance.</p>
<p>If the callback returns a value that is not <code class="docutils literal"><span class="pre">None</span></code>, this will be
used as the response instead of the original response object (and
will be passed to the next post rendering callback etc.)</p>
</dd></dl>

<dl class="method">
<dt id="django.template.response.SimpleTemplateResponse.render">
<code class="descclassname">SimpleTemplateResponse.</code><code class="descname">render</code>()<a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.render"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.render" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets <code class="docutils literal"><span class="pre">response.content</span></code> to the result obtained by
<a class="reference internal" href="#django.template.response.SimpleTemplateResponse.rendered_content" title="django.template.response.SimpleTemplateResponse.rendered_content"><code class="xref py py-attr docutils literal"><span class="pre">SimpleTemplateResponse.rendered_content</span></code></a>, runs all post-rendering
callbacks, and returns the resulting response object.</p>
<p><code class="docutils literal"><span class="pre">render()</span></code> will only have an effect the first time it is called. On
subsequent calls, it will return the result obtained from the first call.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-templateresponse-objects">
<span id="templateresponse-objects"></span><h2>TemplateResponse objects<a class="headerlink" href="#templateresponse-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.template.response.TemplateResponse">
<em class="property">class </em><code class="descname">TemplateResponse</code><a class="reference internal" href="../_modules/django/template/response.html#TemplateResponse"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.TemplateResponse" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal"><span class="pre">TemplateResponse</span></code> is a subclass of
<a class="reference internal" href="#django.template.response.SimpleTemplateResponse" title="django.template.response.SimpleTemplateResponse"><code class="xref py py-class docutils literal"><span class="pre">SimpleTemplateResponse</span></code></a> that knows about
the current <a class="reference internal" href="request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></code></a>.</p>
</dd></dl>

<div class="section" id="s-id1">
<span id="id1"></span><h3>Methods<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.template.response.TemplateResponse.__init__">
<code class="descclassname">TemplateResponse.</code><code class="descname">__init__</code>(<em>request</em>, <em>template</em>, <em>context=None</em>, <em>content_type=None</em>, <em>status=None</em>, <em>current_app=None</em>, <em>charset=None</em>, <em>using=None</em>)<a class="reference internal" href="../_modules/django/template/response.html#TemplateResponse.__init__"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.TemplateResponse.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Instantiates a <a class="reference internal" href="#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></code></a> object
with the given request, template, context, content type, HTTP status, and
charset.</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">request</span></code></dt>
<dd>An <a class="reference internal" href="request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></code></a> instance.</dd>
<dt><code class="docutils literal"><span class="pre">template</span></code></dt>
<dd><p class="first">A backend-dependent template object (such as those returned by
<a class="reference internal" href="../topics/templates.html#django.template.loader.get_template" title="django.template.loader.get_template"><code class="xref py py-func docutils literal"><span class="pre">get_template()</span></code></a>), the name of a template,
or a list of template names.</p>
<div class="last deprecated">
<p><span class="versionmodified">Deprecated since version 1.8: </span><code class="docutils literal"><span class="pre">template</span></code> used to accept a <a class="reference internal" href="templates/api.html#django.template.Template" title="django.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a>.</p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">context</span></code></dt>
<dd><p class="first">A <code class="xref py py-class docutils literal"><span class="pre">dict</span></code> of values to add to the template context. By default,
this is an empty dictionary.</p>
<div class="last deprecated">
<p><span class="versionmodified">Deprecated since version 1.8: </span><code class="docutils literal"><span class="pre">context</span></code> used to accept a <a class="reference internal" href="templates/api.html#django.template.Context" title="django.template.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a>.</p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">content_type</span></code></dt>
<dd>The value included in the HTTP <code class="docutils literal"><span class="pre">Content-Type</span></code> header, including the
MIME type specification and the character set encoding. If
<code class="docutils literal"><span class="pre">content_type</span></code> is specified, then its value is used. Otherwise,
<a class="reference internal" href="settings.html#std:setting-DEFAULT_CONTENT_TYPE"><code class="xref std std-setting docutils literal"><span class="pre">DEFAULT_CONTENT_TYPE</span></code></a> is used.</dd>
<dt><code class="docutils literal"><span class="pre">status</span></code></dt>
<dd>The HTTP status code for the response.</dd>
<dt><code class="docutils literal"><span class="pre">current_app</span></code></dt>
<dd><p class="first">A hint indicating which application contains the current view. See the
<a class="reference internal" href="../topics/http/urls.html#topics-http-reversing-url-namespaces"><span class="std std-ref">namespaced URL resolution strategy</span></a>
for more information.</p>
<div class="last deprecated">
<p><span class="versionmodified">Deprecated since version 1.8: </span>The <code class="docutils literal"><span class="pre">current_app</span></code> argument is deprecated. Instead you should set
<code class="docutils literal"><span class="pre">request.current_app</span></code>.</p>
</div>
</dd>
<dt><code class="docutils literal"><span class="pre">charset</span></code></dt>
<dd>The charset in which the response will be encoded. If not given it will
be extracted from <code class="docutils literal"><span class="pre">content_type</span></code>, and if that is unsuccessful, the
<a class="reference internal" href="settings.html#std:setting-DEFAULT_CHARSET"><code class="xref std std-setting docutils literal"><span class="pre">DEFAULT_CHARSET</span></code></a> setting will be used.</dd>
<dt><code class="docutils literal"><span class="pre">using</span></code></dt>
<dd>The <a class="reference internal" href="settings.html#std:setting-TEMPLATES-NAME"><code class="xref std std-setting docutils literal"><span class="pre">NAME</span></code></a> of a template engine to use for
loading the template.</dd>
</dl>
<div class="versionchanged">
<span class="title">Changed in Django 1.8:</span> <p>The <code class="docutils literal"><span class="pre">charset</span></code> and <code class="docutils literal"><span class="pre">using</span></code> parameters were added.</p>
</div>
</dd></dl>

</div>
</div>
<div class="section" id="s-the-rendering-process">
<span id="the-rendering-process"></span><h2>The rendering process<a class="headerlink" href="#the-rendering-process" title="Permalink to this headline">¶</a></h2>
<p>Before a <a class="reference internal" href="#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></code></a> 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.</p>
<p>There are three circumstances under which a <code class="docutils literal"><span class="pre">TemplateResponse</span></code> will be
rendered:</p>
<ul class="simple">
<li>When the <code class="docutils literal"><span class="pre">TemplateResponse</span></code> instance is explicitly rendered, using
the <a class="reference internal" href="#django.template.response.SimpleTemplateResponse.render" title="django.template.response.SimpleTemplateResponse.render"><code class="xref py py-meth docutils literal"><span class="pre">SimpleTemplateResponse.render()</span></code></a> method.</li>
<li>When the content of the response is explicitly set by assigning
<code class="docutils literal"><span class="pre">response.content</span></code>.</li>
<li>After passing through template response middleware, but before
passing through response middleware.</li>
</ul>
<p>A <code class="docutils literal"><span class="pre">TemplateResponse</span></code> can only be rendered once. The first call to
<a class="reference internal" href="#django.template.response.SimpleTemplateResponse.render" title="django.template.response.SimpleTemplateResponse.render"><code class="xref py py-meth docutils literal"><span class="pre">SimpleTemplateResponse.render()</span></code></a> sets the content of the response;
subsequent rendering calls do not change the response content.</p>
<p>However, when <code class="docutils literal"><span class="pre">response.content</span></code> is explicitly assigned, the
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:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># Set up a rendered TemplateResponse</span>
<span class="o">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">django.template.response</span> <span class="k">import</span> <span class="n">TemplateResponse</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">t</span> <span class="o">=</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;original.html&#39;</span><span class="p">,</span> <span class="p">{})</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">print</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">content</span><span class="p">)</span>
<span class="n">Original</span> <span class="n">content</span>

<span class="c1"># Re-rendering doesn&#39;t change content</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">t</span><span class="o">.</span><span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;new.html&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">print</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">content</span><span class="p">)</span>
<span class="n">Original</span> <span class="n">content</span>

<span class="c1"># Assigning content does change, no render() call required</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">t</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">rendered_content</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">print</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">content</span><span class="p">)</span>
<span class="n">New</span> <span class="n">content</span>
</pre></div>
</div>
<div class="section" id="s-post-render-callbacks">
<span id="post-render-callbacks"></span><h3>Post-render callbacks<a class="headerlink" href="#post-render-callbacks" title="Permalink to this headline">¶</a></h3>
<p>Some operations &#8211; such as caching &#8211; cannot be performed on an
unrendered template. They must be performed on a fully complete and
rendered response.</p>
<p>If you&#8217;re using middleware, the solution is easy. Middleware provides
multiple opportunities to process a response on exit from a view. If
you put behavior in the Response middleware is guaranteed to execute
after template rendering has taken place.</p>
<p>However, if you&#8217;re using a decorator, the same opportunities do not
exist. Any behavior defined in a decorator is handled immediately.</p>
<p>To compensate for this (and any other analogous use cases),
<a class="reference internal" href="#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></code></a> 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.</p>
<p>To define a post-render callback, just define a function that takes
a single argument &#8211; response &#8211; and register that function with
the template response:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.template.response</span> <span class="k">import</span> <span class="n">TemplateResponse</span>

<span class="k">def</span> <span class="nf">my_render_callback</span><span class="p">(</span><span class="n">response</span><span class="p">):</span>
    <span class="c1"># Do content-sensitive processing</span>
    <span class="n">do_post_processing</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c1"># Create a response</span>
    <span class="n">response</span> <span class="o">=</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;mytemplate.html&#39;</span><span class="p">,</span> <span class="p">{})</span>
    <span class="c1"># Register the callback</span>
    <span class="n">response</span><span class="o">.</span><span class="n">add_post_render_callback</span><span class="p">(</span><span class="n">my_render_callback</span><span class="p">)</span>
    <span class="c1"># Return the response</span>
    <span class="k">return</span> <span class="n">response</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">my_render_callback()</span></code> will be invoked after the <code class="docutils literal"><span class="pre">mytemplate.html</span></code>
has been rendered, and will be provided the fully rendered
<a class="reference internal" href="#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></code></a> instance as an argument.</p>
<p>If the template has already been rendered, the callback will be
invoked immediately.</p>
</div>
</div>
<div class="section" id="s-using-templateresponse-and-simpletemplateresponse">
<span id="using-templateresponse-and-simpletemplateresponse"></span><h2>Using TemplateResponse and SimpleTemplateResponse<a class="headerlink" href="#using-templateresponse-and-simpletemplateresponse" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></code></a> object can be used anywhere that a normal
<a class="reference internal" href="request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal"><span class="pre">django.http.HttpResponse</span></code></a> can be used. It can also be used as an
alternative to calling <a class="reference internal" href="../topics/http/shortcuts.html#django.shortcuts.render" title="django.shortcuts.render"><code class="xref py py-func docutils literal"><span class="pre">render()</span></code></a> or
<a class="reference internal" href="../topics/http/shortcuts.html#django.shortcuts.render_to_response" title="django.shortcuts.render_to_response"><code class="xref py py-func docutils literal"><span class="pre">render_to_response()</span></code></a>.</p>
<p>For example, the following simple view returns a <a class="reference internal" href="#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></code></a>
with a simple template and a context containing a queryset:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.template.response</span> <span class="k">import</span> <span class="n">TemplateResponse</span>

<span class="k">def</span> <span class="nf">blog_index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;entry_list.html&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;entries&#39;</span><span class="p">:</span> <span class="n">Entry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()})</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">TemplateResponse and SimpleTemplateResponse</a><ul>
<li><a class="reference internal" href="#simpletemplateresponse-objects">SimpleTemplateResponse objects</a><ul>
<li><a class="reference internal" href="#attributes">Attributes</a></li>
<li><a class="reference internal" href="#methods">Methods</a></li>
</ul>
</li>
<li><a class="reference internal" href="#templateresponse-objects">TemplateResponse objects</a><ul>
<li><a class="reference internal" href="#id1">Methods</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-rendering-process">The rendering process</a><ul>
<li><a class="reference internal" href="#post-render-callbacks">Post-render callbacks</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-templateresponse-and-simpletemplateresponse">Using TemplateResponse and SimpleTemplateResponse</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="templates/upgrading.html">Upgrading templates to Django 1.8</a></li>
    
    
      <li>Next: <a href="unicode.html">Unicode data</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../index.html">Django 1.8.19 documentation</a>
        
          <ul><li><a href="index.html">API Reference</a>
        
        <ul><li>TemplateResponse and SimpleTemplateResponse</li></ul>
        </li></ul>
      </li>
  </ul>

  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/ref/template-response.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 10, 2018</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="templates/upgrading.html" title="Upgrading templates to Django 1.8">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="unicode.html" title="Unicode data">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>