{"title":"TemplateResponse and SimpleTemplateResponse","version":"1.10","locale":"id","docname":"ref/template-response","url":"/id/1.10/ref/template-response/","canonical":"https://djangodocs.dev/id/1.10/ref/template-response/","summary":"Standard HttpResponse objects are static structures. They are provided with a block of pre-rendered content at time of construction, and while that content…","html":"<span id=\"templateresponse-and-simpletemplateresponse\"></span><h1><code class=\"docutils literal notranslate\">TemplateResponse</code> and <code class=\"docutils literal notranslate\">SimpleTemplateResponse</code><a class=\"heading-anchor\" href=\"#module-django.template.response\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Standard <a class=\"reference internal\" href=\"/id/1.10/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">HttpResponse</code></a> objects are static structures.\nThey are provided with a block of pre-rendered content at time of\nconstruction, and while that content can be modified, it isn't in a form that\nmakes it easy to perform modifications.</p>\n<p>However, it can sometimes be beneficial to allow decorators or\nmiddleware to modify a response <em>after</em> it has been constructed by the\nview. For example, you may want to change the template that is used,\nor put additional data into the context.</p>\n<p>TemplateResponse provides a way to do just that. Unlike basic\n<a class=\"reference internal\" href=\"/id/1.10/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">HttpResponse</code></a> objects, TemplateResponse objects retain\nthe details of the template and context that was provided by the view to\ncompute the response. The final output of the response is not computed until\nit is needed, later in the response process.</p>\n<section id=\"simpletemplateresponse-objects\">\n<h2>Obyek <code class=\"docutils literal notranslate\">SimpleTemplateResponse</code><a class=\"heading-anchor\" href=\"#simpletemplateresponse-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.SimpleTemplateResponse\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">SimpleTemplateResponse</span><a class=\"heading-anchor\" href=\"#django.template.response.SimpleTemplateResponse\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<section id=\"attributes\">\n<h3>Atribut<a class=\"heading-anchor\" href=\"#attributes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.SimpleTemplateResponse.template_name\">\n<span class=\"sig-prename descclassname\">SimpleTemplateResponse.</span><span class=\"sig-name descname\">template_name</span><a class=\"heading-anchor\" href=\"#django.template.response.SimpleTemplateResponse.template_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of the template to be rendered. Accepts a backend-dependent\ntemplate object (such as those returned by\n<a class=\"reference internal\" href=\"/id/1.10/topics/templates/#django.template.loader.get_template\" title=\"django.template.loader.get_template\"><code class=\"xref py py-func docutils literal notranslate\">get_template()</code></a>), the name of a template,\nor a list of template names.</p>\n<p>Contoh: <code class=\"docutils literal notranslate\">['foo.html', 'path/to/bar.html']</code></p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.SimpleTemplateResponse.context_data\">\n<span class=\"sig-prename descclassname\">SimpleTemplateResponse.</span><span class=\"sig-name descname\">context_data</span><a class=\"heading-anchor\" href=\"#django.template.response.SimpleTemplateResponse.context_data\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The context data to be used when rendering the template. It must be a\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">dict</code></a>.</p>\n<p>Contoh: <code class=\"docutils literal notranslate\">{'foo': 123}</code></p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.SimpleTemplateResponse.rendered_content\">\n<span class=\"sig-prename descclassname\">SimpleTemplateResponse.</span><span class=\"sig-name descname\">rendered_content</span><a class=\"heading-anchor\" href=\"#django.template.response.SimpleTemplateResponse.rendered_content\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The current rendered value of the response content, using the current\ntemplate and context data.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.SimpleTemplateResponse.is_rendered\">\n<span class=\"sig-prename descclassname\">SimpleTemplateResponse.</span><span class=\"sig-name descname\">is_rendered</span><a class=\"heading-anchor\" href=\"#django.template.response.SimpleTemplateResponse.is_rendered\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A boolean indicating whether the response content has been rendered.</p>\n</dd></dl>\n\n</section>\n<section id=\"methods\">\n<h3>Cara<a class=\"heading-anchor\" href=\"#methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.SimpleTemplateResponse.__init__\">\n<span class=\"sig-prename descclassname\">SimpleTemplateResponse.</span><span class=\"sig-name descname\">__init__</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">template</em>, <em class=\"sig-param\">context<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">content_type<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">status<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">charset<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">using<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.response.SimpleTemplateResponse.__init__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<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 notranslate\">SimpleTemplateResponse</code></a>\nobject with the given template, context, content type, HTTP status, and\ncharset.</p>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\">template</code></dt><dd><p>A backend-dependent template object (such as those returned by\n<a class=\"reference internal\" href=\"/id/1.10/topics/templates/#django.template.loader.get_template\" title=\"django.template.loader.get_template\"><code class=\"xref py py-func docutils literal notranslate\">get_template()</code></a>), the name of a template,\nor a list of template names.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">context</code></dt><dd><p>A <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">dict</code></a> of values to add to the template context. By default,\nthis is an empty dictionary.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">content_type</code></dt><dd><p>The value included in the HTTP <code class=\"docutils literal notranslate\">Content-Type</code> header, including the\nMIME type specification and the character set encoding. If\n<code class=\"docutils literal notranslate\">content_type</code> is specified, then its value is used. Otherwise,\n<a class=\"reference internal\" href=\"/id/1.10/ref/settings/#std-setting-DEFAULT_CONTENT_TYPE\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CONTENT_TYPE</code></a> is used.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">status</code></dt><dd><p>The HTTP status code for the response.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">charset</code></dt><dd><p>The charset in which the response will be encoded. If not given it will\nbe extracted from <code class=\"docutils literal notranslate\">content_type</code>, and if that is unsuccessful, the\n<a class=\"reference internal\" href=\"/id/1.10/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CHARSET</code></a> setting will be used.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">menggunakan</code></dt><dd><p>The <a class=\"reference internal\" href=\"/id/1.10/ref/settings/#std-setting-TEMPLATES-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> of a template engine to use for\nloading the template.</p>\n</dd>\n</dl>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.SimpleTemplateResponse.resolve_context\">\n<span class=\"sig-prename descclassname\">SimpleTemplateResponse.</span><span class=\"sig-name descname\">resolve_context</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">context</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.response.SimpleTemplateResponse.resolve_context\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Preprocesses context data that will be used for rendering a template.\nAccepts a <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">dict</code></a> of context data. By default, returns the same\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">dict</code></a>.</p>\n<p>Override this method in order to customize the context.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.SimpleTemplateResponse.resolve_template\">\n<span class=\"sig-prename descclassname\">SimpleTemplateResponse.</span><span class=\"sig-name descname\">resolve_template</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">template</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.response.SimpleTemplateResponse.resolve_template\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Resolves the template instance to use for rendering. Accepts a\nbackend-dependent template object (such as those returned by\n<a class=\"reference internal\" href=\"/id/1.10/topics/templates/#django.template.loader.get_template\" title=\"django.template.loader.get_template\"><code class=\"xref py py-func docutils literal notranslate\">get_template()</code></a>), the name of a template,\nor a list of template names.</p>\n<p>Returns the backend-dependent template object instance to be rendered.</p>\n<p>Override this method in order to customize template loading.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.SimpleTemplateResponse.add_post_render_callback\">\n<span class=\"sig-prename descclassname\">SimpleTemplateResponse.</span><span class=\"sig-name descname\">add_post_render_callback</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.response.SimpleTemplateResponse.add_post_render_callback\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Add a callback that will be invoked after rendering has taken\nplace. This hook can be used to defer certain processing\noperations (such as caching) until after rendering has occurred.</p>\n<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 notranslate\">SimpleTemplateResponse</code></a>\nhas already been rendered, the callback will be invoked\nimmediately.</p>\n<p>When called, callbacks will be passed a single argument -- the\nrendered <a class=\"reference internal\" href=\"#django.template.response.SimpleTemplateResponse\" title=\"django.template.response.SimpleTemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">SimpleTemplateResponse</code></a>\ninstance.</p>\n<p>If the callback returns a value that is not <code class=\"docutils literal notranslate\">None</code>, this will be\nused as the response instead of the original response object (and\nwill be passed to the next post rendering callback etc.)</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.SimpleTemplateResponse.render\">\n<span class=\"sig-prename descclassname\">SimpleTemplateResponse.</span><span class=\"sig-name descname\">render</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.response.SimpleTemplateResponse.render\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sets <code class=\"docutils literal notranslate\">response.content</code> to the result obtained by\n<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 notranslate\">SimpleTemplateResponse.rendered_content</code></a>, runs all post-rendering\ncallbacks, and returns the resulting response object.</p>\n<p><code class=\"docutils literal notranslate\">render()</code> will only have an effect the first time it is called. On\nsubsequent calls, it will return the result obtained from the first call.</p>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"templateresponse-objects\">\n<h2>Obyek <code class=\"docutils literal notranslate\">TemplateResponse</code><a class=\"heading-anchor\" href=\"#templateresponse-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.TemplateResponse\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">TemplateResponse</span><a class=\"heading-anchor\" href=\"#django.template.response.TemplateResponse\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><code class=\"docutils literal notranslate\">TemplateResponse</code> is a subclass of\n<a class=\"reference internal\" href=\"#django.template.response.SimpleTemplateResponse\" title=\"django.template.response.SimpleTemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">SimpleTemplateResponse</code></a> that knows about\nthe current <a class=\"reference internal\" href=\"/id/1.10/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\">HttpRequest</code></a>.</p>\n</dd></dl>\n\n<section id=\"id1\">\n<h3>Cara<a class=\"heading-anchor\" href=\"#id1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.response.TemplateResponse.__init__\">\n<span class=\"sig-prename descclassname\">TemplateResponse.</span><span class=\"sig-name descname\">__init__</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">template</em>, <em class=\"sig-param\">context<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">content_type<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">status<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">charset<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">using<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.response.TemplateResponse.__init__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<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 notranslate\">TemplateResponse</code></a> object\nwith the given request, template, context, content type, HTTP status, and\ncharset.</p>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\">request</code></dt><dd><p>An <a class=\"reference internal\" href=\"/id/1.10/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\">HttpRequest</code></a> instance.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">template</code></dt><dd><p>A backend-dependent template object (such as those returned by\n<a class=\"reference internal\" href=\"/id/1.10/topics/templates/#django.template.loader.get_template\" title=\"django.template.loader.get_template\"><code class=\"xref py py-func docutils literal notranslate\">get_template()</code></a>), the name of a template,\nor a list of template names.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">context</code></dt><dd><p>A <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">dict</code></a> of values to add to the template context. By default,\nthis is an empty dictionary.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">content_type</code></dt><dd><p>The value included in the HTTP <code class=\"docutils literal notranslate\">Content-Type</code> header, including the\nMIME type specification and the character set encoding. If\n<code class=\"docutils literal notranslate\">content_type</code> is specified, then its value is used. Otherwise,\n<a class=\"reference internal\" href=\"/id/1.10/ref/settings/#std-setting-DEFAULT_CONTENT_TYPE\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CONTENT_TYPE</code></a> is used.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">status</code></dt><dd><p>The HTTP status code for the response.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">charset</code></dt><dd><p>The charset in which the response will be encoded. If not given it will\nbe extracted from <code class=\"docutils literal notranslate\">content_type</code>, and if that is unsuccessful, the\n<a class=\"reference internal\" href=\"/id/1.10/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CHARSET</code></a> setting will be used.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">menggunakan</code></dt><dd><p>The <a class=\"reference internal\" href=\"/id/1.10/ref/settings/#std-setting-TEMPLATES-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> of a template engine to use for\nloading the template.</p>\n</dd>\n</dl>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"the-rendering-process\">\n<h2>Pengolahan membangun<a class=\"heading-anchor\" href=\"#the-rendering-process\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<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 notranslate\">TemplateResponse</code></a> instance can be\nreturned to the client, it must be rendered. The rendering process takes the\nintermediate representation of template and context, and turns it into the\nfinal byte stream that can be served to the client.</p>\n<p>There are three circumstances under which a <code class=\"docutils literal notranslate\">TemplateResponse</code> will be\nrendered:</p>\n<ul class=\"simple\">\n<li><p>When the <code class=\"docutils literal notranslate\">TemplateResponse</code> instance is explicitly rendered, using\nthe <a class=\"reference internal\" href=\"#django.template.response.SimpleTemplateResponse.render\" title=\"django.template.response.SimpleTemplateResponse.render\"><code class=\"xref py py-meth docutils literal notranslate\">SimpleTemplateResponse.render()</code></a> method.</p></li>\n<li><p>When the content of the response is explicitly set by assigning\n<code class=\"docutils literal notranslate\">response.content</code>.</p></li>\n<li><p>After passing through template response middleware, but before\npassing through response middleware.</p></li>\n</ul>\n<p>A <code class=\"docutils literal notranslate\">TemplateResponse</code> can only be rendered once. The first call to\n<a class=\"reference internal\" href=\"#django.template.response.SimpleTemplateResponse.render\" title=\"django.template.response.SimpleTemplateResponse.render\"><code class=\"xref py py-meth docutils literal notranslate\">SimpleTemplateResponse.render()</code></a> sets the content of the response;\nsubsequent rendering calls do not change the response content.</p>\n<p>However, when <code class=\"docutils literal notranslate\">response.content</code> is explicitly assigned, the\nchange is always applied. If you want to force the content to be\nre-rendered, you can re-evaluate the rendered content, and assign\nthe content of the response manually:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># Set up a rendered TemplateResponse</span>\n<span class=\"o\">&gt;&gt;&gt;</span> <span class=\"kn\">from</span> <span class=\"nn\">django.template.response</span> <span class=\"kn\">import</span> TemplateResponse\n<span class=\"o\">&gt;&gt;&gt;</span> t <span class=\"o\">=</span> TemplateResponse<span class=\"p\">(</span>request<span class=\"p\">,</span> <span class=\"s1\">&#39;original.html&#39;</span><span class=\"p\">,</span> <span class=\"p\">{})</span>\n<span class=\"o\">&gt;&gt;&gt;</span> t<span class=\"o\">.</span>render<span class=\"p\">()</span>\n<span class=\"o\">&gt;&gt;&gt;</span> <span class=\"nb\">print</span><span class=\"p\">(</span>t<span class=\"o\">.</span>content<span class=\"p\">)</span>\nOriginal content\n\n<span class=\"c1\"># Re-rendering doesn&#39;t change content</span>\n<span class=\"o\">&gt;&gt;&gt;</span> t<span class=\"o\">.</span>template_name <span class=\"o\">=</span> <span class=\"s1\">&#39;new.html&#39;</span>\n<span class=\"o\">&gt;&gt;&gt;</span> t<span class=\"o\">.</span>render<span class=\"p\">()</span>\n<span class=\"o\">&gt;&gt;&gt;</span> <span class=\"nb\">print</span><span class=\"p\">(</span>t<span class=\"o\">.</span>content<span class=\"p\">)</span>\nOriginal content\n\n<span class=\"c1\"># Assigning content does change, no render() call required</span>\n<span class=\"o\">&gt;&gt;&gt;</span> t<span class=\"o\">.</span>content <span class=\"o\">=</span> t<span class=\"o\">.</span>rendered_content\n<span class=\"o\">&gt;&gt;&gt;</span> <span class=\"nb\">print</span><span class=\"p\">(</span>t<span class=\"o\">.</span>content<span class=\"p\">)</span>\nNew content\n</code></pre></div>\n<section id=\"post-render-callbacks\">\n<h3>Post-render callbacks<a class=\"heading-anchor\" href=\"#post-render-callbacks\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Some operations -- such as caching -- cannot be performed on an\nunrendered template. They must be performed on a fully complete and\nrendered response.</p>\n<p>If you're using middleware, the solution is easy. Middleware provides\nmultiple opportunities to process a response on exit from a view. If\nyou put behavior in the response middleware, it's guaranteed to execute\nafter template rendering has taken place.</p>\n<p>However, if you're using a decorator, the same opportunities do not\nexist. Any behavior defined in a decorator is handled immediately.</p>\n<p>To compensate for this (and any other analogous use cases),\n<a class=\"reference internal\" href=\"#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> allows you to register callbacks that will\nbe invoked when rendering has completed. Using this callback, you can\ndefer critical processing until a point where you can guarantee that\nrendered content will be available.</p>\n<p>To define a post-render callback, just define a function that takes\na single argument -- response -- and register that function with\nthe template response:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.template.response</span> <span class=\"kn\">import</span> TemplateResponse\n\n<span class=\"k\">def</span> <span class=\"nf\">my_render_callback</span><span class=\"p\">(</span>response<span class=\"p\">):</span>\n    <span class=\"c1\"># Do content-sensitive processing</span>\n    do_post_processing<span class=\"p\">()</span>\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"c1\"># Create a response</span>\n    response <span class=\"o\">=</span> TemplateResponse<span class=\"p\">(</span>request<span class=\"p\">,</span> <span class=\"s1\">&#39;mytemplate.html&#39;</span><span class=\"p\">,</span> <span class=\"p\">{})</span>\n    <span class=\"c1\"># Register the callback</span>\n    response<span class=\"o\">.</span>add_post_render_callback<span class=\"p\">(</span>my_render_callback<span class=\"p\">)</span>\n    <span class=\"c1\"># Return the response</span>\n    <span class=\"k\">return</span> response\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">my_render_callback()</code> will be invoked after the <code class=\"docutils literal notranslate\">mytemplate.html</code>\nhas been rendered, and will be provided the fully rendered\n<a class=\"reference internal\" href=\"#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> instance as an argument.</p>\n<p>If the template has already been rendered, the callback will be\ninvoked immediately.</p>\n</section>\n</section>\n<section id=\"using-templateresponse-and-simpletemplateresponse\">\n<h2>Menggunakan <code class=\"docutils literal notranslate\">TemplateResponse</code> dan <code class=\"docutils literal notranslate\">SimpleTemplateResponse</code><a class=\"heading-anchor\" href=\"#using-templateresponse-and-simpletemplateresponse\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A <a class=\"reference internal\" href=\"#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> object can be used anywhere that a normal\n<a class=\"reference internal\" href=\"/id/1.10/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">django.http.HttpResponse</code></a> can be used. It can also be used as an\nalternative to calling <a class=\"reference internal\" href=\"/id/1.10/topics/http/shortcuts/#django.shortcuts.render\" title=\"django.shortcuts.render\"><code class=\"xref py py-func docutils literal notranslate\">render()</code></a>.</p>\n<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 notranslate\">TemplateResponse</code></a>\nwith a simple template and a context containing a queryset:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.template.response</span> <span class=\"kn\">import</span> TemplateResponse\n\n<span class=\"k\">def</span> <span class=\"nf\">blog_index</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">return</span> TemplateResponse<span class=\"p\">(</span>request<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> Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">()})</span>\n</code></pre></div>\n</section>","rootId":"module-django.template.response","toc":[{"title":"Obyek SimpleTemplateResponse","anchor":"simpletemplateresponse-objects","children":[{"title":"Atribut","anchor":"attributes","children":[]},{"title":"Cara","anchor":"methods","children":[]}]},{"title":"Obyek TemplateResponse","anchor":"templateresponse-objects","children":[{"title":"Cara","anchor":"id1","children":[]}]},{"title":"Pengolahan membangun","anchor":"the-rendering-process","children":[{"title":"Post-render callbacks","anchor":"post-render-callbacks","children":[]}]},{"title":"Menggunakan TemplateResponse dan SimpleTemplateResponse","anchor":"using-templateresponse-and-simpletemplateresponse","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"Acuan API","url":"/id/1.10/ref/"}],"prev":{"docname":"ref/templates/upgrading","title":"Upgrading templates to Django 1.8","url":"/id/1.10/ref/templates/upgrading/"},"next":{"docname":"ref/unicode","title":"Unicode data","url":"/id/1.10/ref/unicode/"},"formats":{"html":"/id/1.10/ref/template-response/","markdown":"/id/1.10/ref/template-response.md","json":"/id/1.10/ref/template-response.json"},"source":"https://github.com/django/django/blob/stable/1.10.x/docs/ref/template-response.txt","official":"https://docs.djangoproject.com/id/1.10/ref/template-response/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","fr","ja","id","pt-br","es","el","pl"]}