{"title":"TemplateResponse dan``SimpleTemplateResponse``","version":"6.1","locale":"id","docname":"ref/template-response","url":"/id/6.1/ref/template-response/","canonical":"https://djangodocs.dev/id/6.1/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> dan``SimpleTemplateResponse``<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/6.1/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/6.1/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/6.1/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>Data konteks untuk digunakan ketika membangun cetakan. Itu harus berupa <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>Nilai dibangun saat ini dari isi tanggapan, menggunakan cetakan saat ini dan data konteks.</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>Sebuah boolean menunjukkan apakah tanggapan isi dapat dibangun.</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>, <em class=\"sig-param\">headers<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>Memberi contoh obyek <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> dengan cetakan diberikan, konteks, jenis isi, keadaan HTTP, dan charset.</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/6.1/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>Sebuah <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> dari nilai-nilai untuk menambahkan konteks cetakan. Secara awalan, ini adalah dictionary kosong.</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<code class=\"docutils literal notranslate\">'text/html'</code> is used.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">status</code></dt><dd><p>Kode keadaan HTTP untuk tanggapan.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">charset</code></dt><dd><p>Charset dimana tanggapan akan disandikan. Jika tidak diberikan itu akan dikeluarkan dari <code class=\"docutils literal notranslate\">content_type</code>, dan jika itu tidak berhasil, pengaturan <a class=\"reference internal\" href=\"/id/6.1/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CHARSET</code></a> akan digunakan.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">menggunakan</code></dt><dd><p><a class=\"reference internal\" href=\"/id/6.1/ref/settings/#std-setting-TEMPLATES-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> dari mesin cetakan untuk digunakan untuk memuat cetakan.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">headers</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 HTTP headers to add to the response.</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>Timpa metode ini untuk menyesuaikan konteks.</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/6.1/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>Mengembalikan contoh obyek cetakan bergantung-backend untuk dikirim.</p>\n<p>TImpa metode ini untuk menyesuaikan pemuatan cetakan.</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>Jika <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> sudah dibangun, callback akan dipanggil segera.</p>\n<p>Ketika dipanggil, callback akan melewatkan argumen tunggal -- instance <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> terbangun.</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/6.1/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>, <em class=\"sig-param\">headers<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>Sebuah instance <a class=\"reference internal\" href=\"/id/6.1/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>\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/6.1/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>Sebuah <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> dari nilai-nilai untuk menambahkan konteks cetakan. Secara awalan, ini adalah dictionary kosong.</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<code class=\"docutils literal notranslate\">'text/html'</code> is used.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">status</code></dt><dd><p>Kode keadaan HTTP untuk tanggapan.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">charset</code></dt><dd><p>Charset dimana tanggapan akan disandikan. Jika tidak diberikan itu akan dikeluarkan dari <code class=\"docutils literal notranslate\">content_type</code>, dan jika itu tidak berhasil, pengaturan <a class=\"reference internal\" href=\"/id/6.1/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CHARSET</code></a> akan digunakan.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">menggunakan</code></dt><dd><p><a class=\"reference internal\" href=\"/id/6.1/ref/settings/#std-setting-TEMPLATES-NAME\"><code class=\"xref std std-setting docutils literal notranslate\">NAME</code></a> dari mesin cetakan untuk digunakan untuk memuat cetakan.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\">headers</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 HTTP headers to add to the response.</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>Ada tiga keadaan dibawah mana <code class=\"docutils literal notranslate\">TemplateResponse</code> akan dibangun:</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>Ketika isi dari tanggapan secara tegas disetel dengan memberikan <code class=\"docutils literal notranslate\">response.content</code>.</p></li>\n<li><p>Setelah melewatkan melalui cetakan tanggapan middleware, tetapi sebelum melewatkan melalui tanggapan middleware.</p></li>\n</ul>\n<p>Sebuah <code class=\"docutils literal notranslate\">TemplateResponse</code> hanya dapat dibangun sekali. Panggilan pertama pada  <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> mensetel isi dari tanggapan; selanjutnya membangun panggilan untuk tidak merubah isi tanggapan.</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 reevaluate the rendered content, and assign\nthe content of the response manually:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"go\"># Set up a rendered TemplateResponse</span>\n<span class=\"gp\">&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=\"gp\">&gt;&gt;&gt; </span>t <span class=\"o\">=</span> TemplateResponse<span class=\"p\">(</span>request<span class=\"p\">,</span> <span class=\"s2\">&quot;original.html&quot;</span><span class=\"p\">,</span> <span class=\"p\">{})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>render<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>t<span class=\"o\">.</span>content<span class=\"p\">)</span>\n<span class=\"go\">Original content</span>\n\n<span class=\"go\"># Re-rendering doesn&#39;t change content</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>template_name <span class=\"o\">=</span> <span class=\"s2\">&quot;new.html&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>render<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>t<span class=\"o\">.</span>content<span class=\"p\">)</span>\n<span class=\"go\">Original content</span>\n\n<span class=\"go\"># Assigning content does change, no render() call required</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>content <span class=\"o\">=</span> t<span class=\"o\">.</span>rendered_content\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>t<span class=\"o\">.</span>content<span class=\"p\">)</span>\n<span class=\"go\">New content</span>\n</code></pre></div>\n<section id=\"post-render-callbacks\">\n<h3>Callback pasca-dibangun<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>Beberapa tindakan - seperti menyimpan sementara - tidak dapat dilakukan pada cetakan yang belum dibangun. Mereka harus dilakukan pada sepenuhnya lengkap dan tanggapan yang dibangun.</p>\n<p>If you're using middleware, you can do that. 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>Bagaimanapun, jika anda sedang menggunakan penghias, kesempatan sama tidak ada. Perilaku apapun ditentukan dalam sebuah penghias ditangani segera.</p>\n<p>Untuk mengimbangi ini (dan penggunaan kasus sejalan lainnya), <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> mengizinkan anda mendaftarkan callback yang akan dipanggil ketika pembangunan lengkap. Menggunakan callback ini, anda dapat menunda pengolahan kritis sampai titik dimana anda dapat menjamin bahwa isi dibangun akan tersedia.</p>\n<p>To define a post-render callback, 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\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\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=\"s2\">&quot;mytemplate.html&quot;</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> akan diminta setelah <code class=\"docutils literal notranslate\">mytemplate.html</code> telah dibangun, dan akan disediakan sepenuhnya contoh <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> dibangun sebagai sebuah argumen.</p>\n<p>Jika cetakan sudah dibangun, callback akan diminta segera.</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/6.1/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/6.1/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 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> with a\ntemplate 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\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>\n        request<span class=\"p\">,</span> <span class=\"s2\">&quot;entry_list.html&quot;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s2\">&quot;entries&quot;</span><span class=\"p\">:</span> Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">()}</span>\n    <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":"Callback pasca-dibangun","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/6.1/ref/"}],"prev":{"docname":"ref/templates/api","title":"Bahasa cetakan Django: untuk pemrogram Python","url":"/id/6.1/ref/templates/api/"},"next":{"docname":"ref/unicode","title":"Data Unicode","url":"/id/6.1/ref/unicode/"},"formats":{"html":"/id/6.1/ref/template-response/","markdown":"/id/6.1/ref/template-response.md","json":"/id/6.1/ref/template-response.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/ref/template-response.txt","official":"https://docs.djangoproject.com/id/6.1/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","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}