{"title":"The Django template language: for Python programmers","version":"6.1","locale":"en","docname":"ref/templates/api","url":"/en/6.1/ref/templates/api/","canonical":"https://djangodocs.dev/en/6.1/ref/templates/api/","summary":"This document explains the Django template system from a technical perspective – how it works and how to extend it.","html":"<h1>The Django template language: for Python programmers<a class=\"heading-anchor\" href=\"#the-django-template-language-for-python-programmers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document explains the Django template system from a technical\nperspective – how it works and how to extend it. If you’re looking for\nreference on the language syntax, see <a class=\"reference internal\" href=\"/en/6.1/ref/templates/language/\"><span class=\"doc\">The Django template language</span></a>.</p>\n<p>It assumes an understanding of templates, contexts, variables, tags, and\nrendering. Start with the <a class=\"reference internal\" href=\"/en/6.1/topics/templates/#template-language-intro\"><span class=\"std std-ref\">introduction to the Django template language</span></a> if you aren’t familiar with these concepts.</p>\n<section id=\"overview\">\n<h2>Overview<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Using the template system in Python is a three-step process:</p>\n<ol class=\"arabic simple\">\n<li><p>You configure an <a class=\"reference internal\" href=\"#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\">Engine</code></a>.</p></li>\n<li><p>You compile template code into a <a class=\"reference internal\" href=\"#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\">Template</code></a>.</p></li>\n<li><p>You render the template with a <a class=\"reference internal\" href=\"#django.template.Context\" title=\"django.template.Context\"><code class=\"xref py py-class docutils literal notranslate\">Context</code></a>.</p></li>\n</ol>\n<p>Django projects generally rely on the <a class=\"reference internal\" href=\"/en/6.1/topics/templates/#template-engines\"><span class=\"std std-ref\">high level, backend agnostic APIs</span></a> for each of these steps instead of the template system’s\nlower level APIs:</p>\n<ol class=\"arabic simple\">\n<li><p>For each <a class=\"reference internal\" href=\"/en/6.1/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\">DjangoTemplates</code></a> backend\nin the <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\">TEMPLATES</code></a> setting, Django instantiates an\n<a class=\"reference internal\" href=\"#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\">Engine</code></a>. <a class=\"reference internal\" href=\"/en/6.1/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\">DjangoTemplates</code></a>\nwraps <a class=\"reference internal\" href=\"#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\">Engine</code></a> and adapts it to the common template backend API.</p></li>\n<li><p>The <a class=\"reference internal\" href=\"/en/6.1/topics/templates/#module-django.template.loader\" title=\"django.template.loader\"><code class=\"xref py py-mod docutils literal notranslate\">django.template.loader</code></a> module provides functions such as\n<a class=\"reference internal\" href=\"/en/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> for loading templates. They\nreturn a <code class=\"docutils literal notranslate\">django.template.backends.django.Template</code> which wraps the\nactual <a class=\"reference internal\" href=\"#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\">django.template.Template</code></a>.</p></li>\n<li><p>The <code class=\"docutils literal notranslate\">Template</code> obtained in the previous step has a\n<a class=\"reference internal\" href=\"/en/6.1/topics/templates/#django.template.backends.base.Template.render\" title=\"django.template.backends.base.Template.render\"><code class=\"xref py py-meth docutils literal notranslate\">render()</code></a> method which\nmarshals a context and possibly a request into a <a class=\"reference internal\" href=\"#django.template.Context\" title=\"django.template.Context\"><code class=\"xref py py-class docutils literal notranslate\">Context</code></a> and\ndelegates the rendering to the underlying <a class=\"reference internal\" href=\"#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\">Template</code></a>.</p></li>\n</ol>\n</section>\n<section id=\"configuring-an-engine\">\n<h2>Configuring an engine<a class=\"heading-anchor\" href=\"#configuring-an-engine\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you are using the <a class=\"reference internal\" href=\"/en/6.1/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\">DjangoTemplates</code></a>\nbackend, this probably isn’t the documentation you’re looking for. An instance\nof the <code class=\"docutils literal notranslate\">Engine</code> class described below is accessible using the <code class=\"docutils literal notranslate\">engine</code>\nattribute of that backend and any attribute defaults mentioned below are\noverridden by what’s passed by\n<a class=\"reference internal\" href=\"/en/6.1/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\">DjangoTemplates</code></a>.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.Engine\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Engine</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">dirs<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">app_dirs<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">context_processors<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">debug<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">loaders<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">string_if_invalid<span class=\"o\">=</span><span class=\"default_value\">''</span></em>, <em class=\"sig-param\">file_charset<span class=\"o\">=</span><span class=\"default_value\">'utf-8'</span></em>, <em class=\"sig-param\">libraries<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">builtins<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">autoescape<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Engine\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>When instantiating an <code class=\"docutils literal notranslate\">Engine</code> all arguments must be passed as keyword\narguments:</p>\n<ul>\n<li><p><code class=\"docutils literal notranslate\">dirs</code> is a list of directories where the engine should look for\ntemplate source files. It is used to configure\n<a class=\"reference internal\" href=\"#django.template.loaders.filesystem.Loader\" title=\"django.template.loaders.filesystem.Loader\"><code class=\"xref py py-class docutils literal notranslate\">filesystem.Loader</code></a>.</p>\n<p>It defaults to an empty list.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\">app_dirs</code> only affects the default value of <code class=\"docutils literal notranslate\">loaders</code>. See below.</p>\n<p>It defaults to <code class=\"docutils literal notranslate\">False</code>.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\">autoescape</code> controls whether HTML autoescaping is enabled.</p>\n<p>It defaults to <code class=\"docutils literal notranslate\">True</code>.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Only set it to <code class=\"docutils literal notranslate\">False</code> if you’re rendering non-HTML templates!</p>\n</aside>\n</li>\n<li><p><code class=\"docutils literal notranslate\">context_processors</code> is a list of dotted Python paths to callables\nthat are used to populate the context when a template is rendered with a\nrequest. These callables take a request object as their argument and\nreturn a <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">dict</code></a> of items to be merged into the context.</p>\n<p>It defaults to an empty list.</p>\n<p>See <a class=\"reference internal\" href=\"#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\">RequestContext</code></a> for more information.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\">debug</code> is a boolean that turns on/off template debug mode. If it is\n<code class=\"docutils literal notranslate\">True</code>, the template engine will store additional debug information\nwhich can be used to display a detailed report for any exception raised\nduring template rendering.</p>\n<p>It defaults to <code class=\"docutils literal notranslate\">False</code>.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\">loaders</code> is a list of template loader classes, specified as strings.\nEach <code class=\"docutils literal notranslate\">Loader</code> class knows how to import templates from a particular\nsource. Optionally, a tuple can be used instead of a string. The first\nitem in the tuple should be the <code class=\"docutils literal notranslate\">Loader</code> class name, subsequent items\nare passed to the <code class=\"docutils literal notranslate\">Loader</code> during initialization.</p>\n<p>It defaults to a list containing:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">'django.template.loaders.filesystem.Loader'</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">'django.template.loaders.app_directories.Loader'</code> if and only if\n<code class=\"docutils literal notranslate\">app_dirs</code> is <code class=\"docutils literal notranslate\">True</code>.</p></li>\n</ul>\n<p>These loaders are then wrapped in\n<a class=\"reference internal\" href=\"#django.template.loaders.cached.Loader\" title=\"django.template.loaders.cached.Loader\"><code class=\"xref py py-class docutils literal notranslate\">django.template.loaders.cached.Loader</code></a>.</p>\n<p>See <a class=\"reference internal\" href=\"#template-loaders\"><span class=\"std std-ref\">Loader types</span></a> for details.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\">string_if_invalid</code> is the output, as a string, that the template\nsystem should use for invalid (e.g. misspelled) variables.</p>\n<p>It defaults to the empty string.</p>\n<p>See <a class=\"reference internal\" href=\"#invalid-template-variables\"><span class=\"std std-ref\">How invalid variables are handled</span></a> for details.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\">file_charset</code> is the charset used to read template files on disk.</p>\n<p>It defaults to <code class=\"docutils literal notranslate\">'utf-8'</code>.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\">'libraries'</code>: A dictionary of labels and dotted Python paths of\ntemplate tag modules to register with the template engine. This is used\nto add new libraries or provide alternate labels for existing ones. For\nexample:</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>Engine<span class=\"p\">(</span>\n    libraries<span class=\"o\">=</span><span class=\"p\">{</span>\n        <span class=\"s2\">&quot;myapp_tags&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;path.to.myapp.tags&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;admin.urls&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.contrib.admin.templatetags.admin_urls&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>Libraries can be loaded by passing the corresponding dictionary key to\nthe <a class=\"reference internal\" href=\"/en/6.1/ref/templates/builtins/#std-templatetag-load\"><code class=\"xref std std-ttag docutils literal notranslate\">{% load %}</code></a> tag.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\">'builtins'</code>: A list of dotted Python paths of template tag modules to\nadd to <a class=\"reference internal\" href=\"/en/6.1/ref/templates/builtins/\"><span class=\"doc\">built-ins</span></a>. For example:</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>Engine<span class=\"p\">(</span>\n    builtins<span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;myapp.builtins&quot;</span><span class=\"p\">],</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>Tags and filters from built-in libraries can be used without first\ncalling the <a class=\"reference internal\" href=\"/en/6.1/ref/templates/builtins/#std-templatetag-load\"><code class=\"xref std std-ttag docutils literal notranslate\">{% load %}</code></a> tag.</p>\n</li>\n</ul>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Engine.get_default\">\n<em class=\"property\"><span class=\"k\">static</span> </em><span class=\"sig-prename descclassname\">Engine.</span><span class=\"sig-name descname\">get_default</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Engine.get_default\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the underlying <a class=\"reference internal\" href=\"#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\">Engine</code></a> from the first configured\n<a class=\"reference internal\" href=\"/en/6.1/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\">DjangoTemplates</code></a> engine. Raises\n<a class=\"reference internal\" href=\"/en/6.1/ref/exceptions/#django.core.exceptions.ImproperlyConfigured\" title=\"django.core.exceptions.ImproperlyConfigured\"><code class=\"xref py py-exc docutils literal notranslate\">ImproperlyConfigured</code></a> if no engines are\nconfigured.</p>\n<p>It’s required for preserving APIs that rely on a globally available,\nimplicitly configured engine. Any other use is strongly discouraged.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Engine.from_string\">\n<span class=\"sig-prename descclassname\">Engine.</span><span class=\"sig-name descname\">from_string</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">template_code</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Engine.from_string\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Compiles the given template code and returns a <a class=\"reference internal\" href=\"#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\">Template</code></a> object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Engine.get_template\">\n<span class=\"sig-prename descclassname\">Engine.</span><span class=\"sig-name descname\">get_template</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">template_name</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Engine.get_template\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Loads a template with the given name, compiles it and returns a\n<a class=\"reference internal\" href=\"#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\">Template</code></a> object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Engine.select_template\">\n<span class=\"sig-prename descclassname\">Engine.</span><span class=\"sig-name descname\">select_template</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">template_name_list</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Engine.select_template\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Like <a class=\"reference internal\" href=\"#django.template.Engine.get_template\" title=\"django.template.Engine.get_template\"><code class=\"xref py py-meth docutils literal notranslate\">get_template()</code></a>, except it takes a list of names\nand returns the first template that was found.</p>\n</dd></dl>\n\n</section>\n<section id=\"loading-a-template\">\n<h2>Loading a template<a class=\"heading-anchor\" href=\"#loading-a-template\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The recommended way to create a <a class=\"reference internal\" href=\"#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\">Template</code></a> is by calling the factory\nmethods of the <a class=\"reference internal\" href=\"#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\">Engine</code></a>: <a class=\"reference internal\" href=\"#django.template.Engine.get_template\" title=\"django.template.Engine.get_template\"><code class=\"xref py py-meth docutils literal notranslate\">get_template()</code></a>,\n<a class=\"reference internal\" href=\"#django.template.Engine.select_template\" title=\"django.template.Engine.select_template\"><code class=\"xref py py-meth docutils literal notranslate\">select_template()</code></a> and <a class=\"reference internal\" href=\"#django.template.Engine.from_string\" title=\"django.template.Engine.from_string\"><code class=\"xref py py-meth docutils literal notranslate\">from_string()</code></a>.</p>\n<p>In a Django project where the <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\">TEMPLATES</code></a> setting defines a\n<a class=\"reference internal\" href=\"/en/6.1/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\">DjangoTemplates</code></a> engine, it’s\npossible to instantiate a <a class=\"reference internal\" href=\"#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\">Template</code></a> directly. If more than one\n<a class=\"reference internal\" href=\"/en/6.1/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\">DjangoTemplates</code></a> engine is defined,\nthe first one will be used.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.Template\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Template</span><a class=\"heading-anchor\" href=\"#django.template.Template\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This class lives at <code class=\"docutils literal notranslate\">django.template.Template</code>. The constructor takes\none argument — the raw template code:</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</span> <span class=\"kn\">import</span> Template\n\ntemplate <span class=\"o\">=</span> Template<span class=\"p\">(</span><span class=\"s2\">&quot;My name is {{ my_name }}.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<aside class=\"admonition-behind-the-scenes admonition\" role=\"note\">\n<p class=\"admonition-title\">Behind the scenes</p>\n<p>The system only parses your raw template code once – when you create the\n<code class=\"docutils literal notranslate\">Template</code> object. From then on, it’s stored internally as a tree\nstructure for performance.</p>\n<p>Even the parsing itself is quite fast. Most of the parsing happens via a\nsingle call to a single, short, regular expression.</p>\n</aside>\n</section>\n<section id=\"rendering-a-context\">\n<h2>Rendering a context<a class=\"heading-anchor\" href=\"#rendering-a-context\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Once you have a compiled <a class=\"reference internal\" href=\"#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\">Template</code></a> object, you can render a context\nwith it. You can reuse the same template to render it several times with\ndifferent contexts.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.Context\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Context</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">dict_<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">autoescape<span class=\"o\">=</span><span class=\"default_value\">True</span></em>, <em class=\"sig-param\">use_l10n<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">use_tz<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Context\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The constructor of <code class=\"docutils literal notranslate\">django.template.Context</code> takes an optional argument —\na dictionary mapping variable names to variable values.</p>\n<p>Three optional keyword arguments can also be specified:</p>\n<ul>\n<li><p><code class=\"docutils literal notranslate\">autoescape</code> controls whether HTML autoescaping is enabled.</p>\n<p>It defaults to <code class=\"docutils literal notranslate\">True</code>.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Only set it to <code class=\"docutils literal notranslate\">False</code> if you’re rendering non-HTML templates!</p>\n</aside>\n</li>\n<li><p><code class=\"docutils literal notranslate\">use_l10n</code> overrides whether values will be localized by default. If\nset to <code class=\"docutils literal notranslate\">True</code> numbers and dates will be formatted based on locale.</p>\n<p>It defaults to <code class=\"docutils literal notranslate\">None</code>.</p>\n<p>See <a class=\"reference internal\" href=\"/en/6.1/topics/i18n/formatting/#topic-l10n-templates\"><span class=\"std std-ref\">Controlling localization in templates</span></a> for details.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\">use_tz</code> overrides whether dates are converted to the local time when\nrendered in a template. If set to <code class=\"docutils literal notranslate\">True</code> all dates will be rendered\nusing the local timezone. This takes precedence over <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\">USE_TZ</code></a>.</p>\n<p>It defaults to <code class=\"docutils literal notranslate\">None</code>.</p>\n<p>See <a class=\"reference internal\" href=\"/en/6.1/topics/i18n/timezones/#time-zones-in-templates\"><span class=\"std std-ref\">Time zone aware output in templates</span></a> for details.</p>\n</li>\n</ul>\n<p>For example usage, see <a class=\"reference internal\" href=\"#playing-with-context\"><span class=\"std std-ref\">Playing with Context objects</span></a> below.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Template.render\">\n<span class=\"sig-prename descclassname\">Template.</span><span class=\"sig-name descname\">render</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">context</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Template.render\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Call the <a class=\"reference internal\" href=\"#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\">Template</code></a> object’s <code class=\"docutils literal notranslate\">render()</code> method with a\n<a class=\"reference internal\" href=\"#django.template.Context\" title=\"django.template.Context\"><code class=\"xref py py-class docutils literal notranslate\">Context</code></a> to “fill” the template:</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=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.template</span> <span class=\"kn\">import</span> Context<span class=\"p\">,</span> Template\n<span class=\"gp\">&gt;&gt;&gt; </span>template <span class=\"o\">=</span> Template<span class=\"p\">(</span><span class=\"s2\">&quot;My name is {{ my_name }}.&quot;</span><span class=\"p\">)</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>context <span class=\"o\">=</span> Context<span class=\"p\">({</span><span class=\"s2\">&quot;my_name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Adrian&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>template<span class=\"o\">.</span>render<span class=\"p\">(</span>context<span class=\"p\">)</span>\n<span class=\"go\">&quot;My name is Adrian.&quot;</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>context <span class=\"o\">=</span> Context<span class=\"p\">({</span><span class=\"s2\">&quot;my_name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Dolores&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>template<span class=\"o\">.</span>render<span class=\"p\">(</span>context<span class=\"p\">)</span>\n<span class=\"go\">&quot;My name is Dolores.&quot;</span>\n</code></pre></div>\n</dd></dl>\n\n<section id=\"variables-and-lookups\">\n<h3>Variables and lookups<a class=\"heading-anchor\" href=\"#variables-and-lookups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Variable names must consist of any letter (A-Z), any digit (0-9), an underscore\n(but they must not start with an underscore) or a dot.</p>\n<p>Dots have a special meaning in template rendering. A dot in a variable name\nsignifies a <strong>lookup</strong>. Specifically, when the template system encounters a\ndot in a variable name, it tries the following lookups, in this order:</p>\n<ul class=\"simple\">\n<li><p>Dictionary lookup. Example: <code class=\"docutils literal notranslate\">foo[&quot;bar&quot;]</code></p></li>\n<li><p>Attribute lookup. Example: <code class=\"docutils literal notranslate\">foo.bar</code></p></li>\n<li><p>List-index lookup. Example: <code class=\"docutils literal notranslate\">foo[bar]</code></p></li>\n</ul>\n<p>Note that “bar” in a template expression like <code class=\"docutils literal notranslate\">{{ foo.bar }}</code> will be\ninterpreted as a literal string and not using the value of the variable “bar”,\nif one exists in the template context.</p>\n<p>The template system uses the first lookup type that works. It’s short-circuit\nlogic. Here are a few examples:</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=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.template</span> <span class=\"kn\">import</span> Context<span class=\"p\">,</span> Template\n<span class=\"gp\">&gt;&gt;&gt; </span>t <span class=\"o\">=</span> Template<span class=\"p\">(</span><span class=\"s2\">&quot;My name is {{ person.first_name }}.&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>d <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s2\">&quot;person&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span><span class=\"s2\">&quot;first_name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Joe&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;last_name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Johnson&quot;</span><span class=\"p\">}}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>render<span class=\"p\">(</span>Context<span class=\"p\">(</span>d<span class=\"p\">))</span>\n<span class=\"go\">&quot;My name is Joe.&quot;</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">PersonClass</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>    <span class=\"k\">pass</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>p <span class=\"o\">=</span> PersonClass<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>p<span class=\"o\">.</span>first_name <span class=\"o\">=</span> <span class=\"s2\">&quot;Ron&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>p<span class=\"o\">.</span>last_name <span class=\"o\">=</span> <span class=\"s2\">&quot;Nasty&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>render<span class=\"p\">(</span>Context<span class=\"p\">({</span><span class=\"s2\">&quot;person&quot;</span><span class=\"p\">:</span> p<span class=\"p\">}))</span>\n<span class=\"go\">&quot;My name is Ron.&quot;</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>t <span class=\"o\">=</span> Template<span class=\"p\">(</span><span class=\"s2\">&quot;The first stooge in the list is {{ stooges.0 }}.&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Context<span class=\"p\">({</span><span class=\"s2\">&quot;stooges&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;Larry&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Curly&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Moe&quot;</span><span class=\"p\">]})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>render<span class=\"p\">(</span>c<span class=\"p\">)</span>\n<span class=\"go\">&quot;The first stooge in the list is Larry.&quot;</span>\n</code></pre></div>\n<p>If any part of the variable is callable, the template system will try calling\nit. Example:</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=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">PersonClass2</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>    <span class=\"k\">def</span> <span class=\"nf\">name</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>        <span class=\"k\">return</span> <span class=\"s2\">&quot;Samantha&quot;</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t <span class=\"o\">=</span> Template<span class=\"p\">(</span><span class=\"s2\">&quot;My name is {{ person.name }}.&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>render<span class=\"p\">(</span>Context<span class=\"p\">({</span><span class=\"s2\">&quot;person&quot;</span><span class=\"p\">:</span> PersonClass2<span class=\"p\">}))</span>\n<span class=\"go\">&quot;My name is Samantha.&quot;</span>\n</code></pre></div>\n<p>Callable variables are slightly more complex than variables which only require\nstraight lookups. Here are some things to keep in mind:</p>\n<ul>\n<li><p>If the variable raises an exception when called, the exception will be\npropagated, unless the exception has an attribute\n<code class=\"docutils literal notranslate\">silent_variable_failure</code> whose value is <code class=\"docutils literal notranslate\">True</code>. If the exception\n<em>does</em> have a <code class=\"docutils literal notranslate\">silent_variable_failure</code> attribute whose value is\n<code class=\"docutils literal notranslate\">True</code>, the variable will render as the value of the engine’s\n<code class=\"docutils literal notranslate\">string_if_invalid</code> configuration option (an empty string, by default).\nExample:</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=\"gp\">&gt;&gt;&gt; </span>t <span class=\"o\">=</span> Template<span class=\"p\">(</span><span class=\"s2\">&quot;My name is {{ person.first_name }}.&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">PersonClass3</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>    <span class=\"k\">def</span> <span class=\"nf\">first_name</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>        <span class=\"k\">raise</span> <span class=\"ne\">AssertionError</span><span class=\"p\">(</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>p <span class=\"o\">=</span> PersonClass3<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>render<span class=\"p\">(</span>Context<span class=\"p\">({</span><span class=\"s2\">&quot;person&quot;</span><span class=\"p\">:</span> p<span class=\"p\">}))</span>\n<span class=\"gt\">Traceback (most recent call last):</span>\n<span class=\"c\">...</span>\n<span class=\"gr\">AssertionError</span>: foo\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">SilentAssertionError</span><span class=\"p\">(</span><span class=\"ne\">Exception</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    silent_variable_failure <span class=\"o\">=</span> <span class=\"kc\">True</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">PersonClass4</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>    <span class=\"k\">def</span> <span class=\"nf\">first_name</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>        <span class=\"k\">raise</span> SilentAssertionError\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>p <span class=\"o\">=</span> PersonClass4<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>render<span class=\"p\">(</span>Context<span class=\"p\">({</span><span class=\"s2\">&quot;person&quot;</span><span class=\"p\">:</span> p<span class=\"p\">}))</span>\n<span class=\"go\">&quot;My name is .&quot;</span>\n</code></pre></div>\n<p>Note that <a class=\"reference internal\" href=\"/en/6.1/ref/exceptions/#django.core.exceptions.ObjectDoesNotExist\" title=\"django.core.exceptions.ObjectDoesNotExist\"><code class=\"xref py py-exc docutils literal notranslate\">django.core.exceptions.ObjectDoesNotExist</code></a>, which is the\nbase class for all Django database API <code class=\"docutils literal notranslate\">DoesNotExist</code> exceptions, has\n<code class=\"docutils literal notranslate\">silent_variable_failure = True</code>. So if you’re using Django templates\nwith Django model objects, any <code class=\"docutils literal notranslate\">DoesNotExist</code> exception will fail\nsilently.</p>\n</li>\n<li><p>A variable can only be called if it has no required arguments. Otherwise,\nthe system will return the value of the engine’s <code class=\"docutils literal notranslate\">string_if_invalid</code>\noption.</p></li>\n</ul>\n<ul id=\"alters-data-description\">\n<li><p>There can be side effects when calling some variables, and it’d be either\nfoolish or a security hole to allow the template system to access them.</p>\n<p>A good example is the <a class=\"reference internal\" href=\"/en/6.1/ref/models/instances/#django.db.models.Model.delete\" title=\"django.db.models.Model.delete\"><code class=\"xref py py-meth docutils literal notranslate\">delete()</code></a> method on\neach Django model object. The template system shouldn’t be allowed to do\nsomething like this:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code>I will now delete this valuable data. <span class=\"cp\">{{</span> <span class=\"nv\">data.delete</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>To prevent this, set an <code class=\"docutils literal notranslate\">alters_data</code> attribute on the callable\nvariable. The template system won’t call a variable if it has\n<code class=\"docutils literal notranslate\">alters_data=True</code> set, and will instead replace the variable with\n<code class=\"docutils literal notranslate\">string_if_invalid</code>, unconditionally. The\ndynamically-generated <a class=\"reference internal\" href=\"/en/6.1/ref/models/instances/#django.db.models.Model.delete\" title=\"django.db.models.Model.delete\"><code class=\"xref py py-meth docutils literal notranslate\">delete()</code></a> and\n<a class=\"reference internal\" href=\"/en/6.1/ref/models/instances/#django.db.models.Model.save\" title=\"django.db.models.Model.save\"><code class=\"xref py py-meth docutils literal notranslate\">save()</code></a> methods on Django model objects get\n<code class=\"docutils literal notranslate\">alters_data=True</code> automatically. Example:</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=\"k\">def</span> <span class=\"nf\">sensitive_function</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    <span class=\"bp\">self</span><span class=\"o\">.</span>database_record<span class=\"o\">.</span>delete<span class=\"p\">()</span>\n\n\nsensitive_function<span class=\"o\">.</span>alters_data <span class=\"o\">=</span> <span class=\"kc\">True</span>\n</code></pre></div>\n</li>\n<li><p>Occasionally you may want to turn off this feature for other reasons,\nand tell the template system to leave a variable uncalled no matter\nwhat. To do so, set a <code class=\"docutils literal notranslate\">do_not_call_in_templates</code> attribute on the\ncallable with the value <code class=\"docutils literal notranslate\">True</code>. The template system then will act as\nif your variable is not callable (allowing you to access attributes of\nthe callable, for example).</p></li>\n</ul>\n</section>\n<section id=\"how-invalid-variables-are-handled\">\n<span id=\"invalid-template-variables\"></span><h3>How invalid variables are handled<a class=\"heading-anchor\" href=\"#how-invalid-variables-are-handled\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Generally, if a variable doesn’t exist, the template system inserts the value\nof the engine’s <code class=\"docutils literal notranslate\">string_if_invalid</code> configuration option, which is set to\n<code class=\"docutils literal notranslate\">''</code> (the empty string) by default.</p>\n<p>Filters that are applied to an invalid variable will only be applied if\n<code class=\"docutils literal notranslate\">string_if_invalid</code> is set to <code class=\"docutils literal notranslate\">''</code> (the empty string). If\n<code class=\"docutils literal notranslate\">string_if_invalid</code> is set to any other value, variable filters will be\nignored.</p>\n<p>This behavior is slightly different for the <code class=\"docutils literal notranslate\">if</code>, <code class=\"docutils literal notranslate\">for</code> and <code class=\"docutils literal notranslate\">regroup</code>\ntemplate tags. If an invalid variable is provided to one of these template\ntags, the variable will be interpreted as <code class=\"docutils literal notranslate\">None</code>. Filters are always\napplied to invalid variables within these template tags.</p>\n<p>If <code class=\"docutils literal notranslate\">string_if_invalid</code> contains a <code class=\"docutils literal notranslate\">'%s'</code>, the format marker will be\nreplaced with the name of the invalid variable.</p>\n<aside class=\"admonition-for-debug-purposes-only admonition\" role=\"note\">\n<p class=\"admonition-title\">For debug purposes only!</p>\n<p>While <code class=\"docutils literal notranslate\">string_if_invalid</code> can be a useful debugging tool, it is a bad\nidea to turn it on as a ‘development default’.</p>\n<p>Many templates, including some of Django’s, rely upon the silence of the\ntemplate system when a nonexistent variable is encountered. If you assign a\nvalue other than <code class=\"docutils literal notranslate\">''</code> to <code class=\"docutils literal notranslate\">string_if_invalid</code>, you will experience\nrendering problems with these templates and sites.</p>\n<p>Generally, <code class=\"docutils literal notranslate\">string_if_invalid</code> should only be enabled in order to debug\na specific template problem, then cleared once debugging is complete.</p>\n</aside>\n</section>\n<section id=\"built-in-variables\">\n<h3>Built-in variables<a class=\"heading-anchor\" href=\"#built-in-variables\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Every context contains <code class=\"docutils literal notranslate\">True</code>, <code class=\"docutils literal notranslate\">False</code> and <code class=\"docutils literal notranslate\">None</code>. As you would expect,\nthese variables resolve to the corresponding Python objects.</p>\n</section>\n<section id=\"limitations-with-string-literals\">\n<h3>Limitations with string literals<a class=\"heading-anchor\" href=\"#limitations-with-string-literals\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django’s template language has no way to escape the characters used for its own\nsyntax. For example, the <a class=\"reference internal\" href=\"/en/6.1/ref/templates/builtins/#std-templatetag-templatetag\"><code class=\"xref std std-ttag docutils literal notranslate\">templatetag</code></a> tag is required if you need to\noutput character sequences like <code class=\"docutils literal notranslate\">{%</code> and <code class=\"docutils literal notranslate\">%}</code>.</p>\n<p>A similar issue exists if you want to include these sequences in template\nfilter or tag arguments. For example, when parsing a block tag, Django’s\ntemplate parser looks for the first occurrence of <code class=\"docutils literal notranslate\">%}</code> after a <code class=\"docutils literal notranslate\">{%</code>. This\nprevents the use of <code class=\"docutils literal notranslate\">&quot;%}&quot;</code> as a string literal. For example, a\n<code class=\"docutils literal notranslate\">TemplateSyntaxError</code> will be raised for the following expressions:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">include</span> <span class=\"s2\">&quot;template.html&quot;</span> <span class=\"nv\">tvar</span><span class=\"o\">=</span><span class=\"s2\">&quot;Some string literal with %} in it.&quot;</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">with</span> <span class=\"nv\">tvar</span><span class=\"o\">=</span><span class=\"s2\">&quot;Some string literal with %} in it.&quot;</span> <span class=\"cp\">%}{%</span> <span class=\"k\">endwith</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The same issue can be triggered by using a reserved sequence in filter\narguments:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">some.variable</span><span class=\"o\">|</span><span class=\"nf\">default</span><span class=\"s2\">:&quot;}}&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If you need to use strings with these sequences, store them in template\nvariables or use a custom template tag or filter to workaround the limitation.</p>\n</section>\n</section>\n<section id=\"playing-with-context-objects\">\n<span id=\"playing-with-context\"></span><h2>Playing with <code class=\"docutils literal notranslate\">Context</code> objects<a class=\"heading-anchor\" href=\"#playing-with-context-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Most of the time, you’ll instantiate <a class=\"reference internal\" href=\"#django.template.Context\" title=\"django.template.Context\"><code class=\"xref py py-class docutils literal notranslate\">Context</code></a> objects by passing in a\nfully-populated dictionary to <code class=\"docutils literal notranslate\">Context()</code>. But you can add and delete items\nfrom a <code class=\"docutils literal notranslate\">Context</code> object once it’s been instantiated, too, using standard\ndictionary syntax:</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=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.template</span> <span class=\"kn\">import</span> Context\n<span class=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Context<span class=\"p\">({</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;bar&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;bar&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">del</span> c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"gt\">Traceback (most recent call last):</span>\n<span class=\"c\">...</span>\n<span class=\"gr\">KeyError</span>: &#39;foo&#39;\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;newvariable&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;hello&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;newvariable&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;hello&#39;</span>\n</code></pre></div>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Context.get\">\n<span class=\"sig-prename descclassname\">Context.</span><span class=\"sig-name descname\">get</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">key</em>, <em class=\"sig-param\">otherwise<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Context.get\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the value for <code class=\"docutils literal notranslate\">key</code> if <code class=\"docutils literal notranslate\">key</code> is in the context, else returns\n<code class=\"docutils literal notranslate\">otherwise</code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Context.setdefault\">\n<span class=\"sig-prename descclassname\">Context.</span><span class=\"sig-name descname\">setdefault</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">key</em>, <em class=\"sig-param\">default<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Context.setdefault\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>If <code class=\"docutils literal notranslate\">key</code> is in the context, returns its value. Otherwise inserts <code class=\"docutils literal notranslate\">key</code>\nwith a value of <code class=\"docutils literal notranslate\">default</code> and returns <code class=\"docutils literal notranslate\">default</code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Context.pop\">\n<span class=\"sig-prename descclassname\">Context.</span><span class=\"sig-name descname\">pop</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Context.pop\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Context.push\">\n<span class=\"sig-prename descclassname\">Context.</span><span class=\"sig-name descname\">push</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Context.push\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<dl class=\"py exception\">\n<dt class=\"sig sig-object py\" id=\"django.template.ContextPopException\">\n<em class=\"property\"><span class=\"k\">exception</span> </em><span class=\"sig-name descname\">ContextPopException</span><a class=\"heading-anchor\" href=\"#django.template.ContextPopException\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A <code class=\"docutils literal notranslate\">Context</code> object is a stack. That is, you can <code class=\"docutils literal notranslate\">push()</code> and <code class=\"docutils literal notranslate\">pop()</code> it.\nIf you <code class=\"docutils literal notranslate\">pop()</code> too much, it’ll raise\n<code class=\"docutils literal notranslate\">django.template.ContextPopException</code>:</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=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Context<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;first level&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>push<span class=\"p\">()</span>\n<span class=\"go\">{}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;second level&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;second level&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>pop<span class=\"p\">()</span>\n<span class=\"go\">{&#39;foo&#39;: &#39;second level&#39;}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;first level&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;overwritten&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;overwritten&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>pop<span class=\"p\">()</span>\n<span class=\"gt\">Traceback (most recent call last):</span>\n<span class=\"c\">...</span>\n<span class=\"gr\">ContextPopException</span>\n</code></pre></div>\n<p>You can also use <code class=\"docutils literal notranslate\">push()</code> as a context manager to ensure a matching <code class=\"docutils literal notranslate\">pop()</code>\nis called.</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=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Context<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;first level&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">with</span> c<span class=\"o\">.</span>push<span class=\"p\">():</span>\n<span class=\"gp\">... </span>    c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;second level&quot;</span>\n<span class=\"gp\">... </span>    c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n<span class=\"go\">&#39;second level&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;first level&#39;</span>\n</code></pre></div>\n<p>All arguments passed to <code class=\"docutils literal notranslate\">push()</code> will be passed to the <code class=\"docutils literal notranslate\">dict</code> constructor\nused to build the new context level.</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=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Context<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;first level&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">with</span> c<span class=\"o\">.</span>push<span class=\"p\">(</span>foo<span class=\"o\">=</span><span class=\"s2\">&quot;second level&quot;</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n<span class=\"go\">&#39;second level&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;first level&#39;</span>\n</code></pre></div>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Context.update\">\n<span class=\"sig-prename descclassname\">Context.</span><span class=\"sig-name descname\">update</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">other_dict</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Context.update\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>In addition to <code class=\"docutils literal notranslate\">push()</code> and <code class=\"docutils literal notranslate\">pop()</code>, the <code class=\"docutils literal notranslate\">Context</code>\nobject also defines an <code class=\"docutils literal notranslate\">update()</code> method. This works like <code class=\"docutils literal notranslate\">push()</code>\nbut takes a dictionary as an argument and pushes that dictionary onto\nthe stack instead of an empty one.</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=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Context<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;first level&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>update<span class=\"p\">({</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;updated&quot;</span><span class=\"p\">})</span>\n<span class=\"go\">{&#39;foo&#39;: &#39;updated&#39;}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;updated&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>pop<span class=\"p\">()</span>\n<span class=\"go\">{&#39;foo&#39;: &#39;updated&#39;}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;first level&#39;</span>\n</code></pre></div>\n<p>Like <code class=\"docutils literal notranslate\">push()</code>, you can use <code class=\"docutils literal notranslate\">update()</code> as a context manager to ensure a\nmatching <code class=\"docutils literal notranslate\">pop()</code> is called.</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=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Context<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;first level&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">with</span> c<span class=\"o\">.</span>update<span class=\"p\">({</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;second level&quot;</span><span class=\"p\">}):</span>\n<span class=\"gp\">... </span>    c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n<span class=\"go\">&#39;second level&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">&#39;first level&#39;</span>\n</code></pre></div>\n<p>Using a <code class=\"docutils literal notranslate\">Context</code> as a stack comes in handy in <a class=\"reference internal\" href=\"/en/6.1/howto/custom-template-tags/#howto-writing-custom-template-tags\"><span class=\"std std-ref\">some custom template\ntags</span></a>.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.Context.flatten\">\n<span class=\"sig-prename descclassname\">Context.</span><span class=\"sig-name descname\">flatten</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.Context.flatten\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Using <code class=\"docutils literal notranslate\">flatten()</code> method you can get whole <code class=\"docutils literal notranslate\">Context</code> stack as one\ndictionary including builtin variables.</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=\"gp\">&gt;&gt;&gt; </span>c <span class=\"o\">=</span> Context<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;first level&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>update<span class=\"p\">({</span><span class=\"s2\">&quot;bar&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;second level&quot;</span><span class=\"p\">})</span>\n<span class=\"go\">{&#39;bar&#39;: &#39;second level&#39;}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c<span class=\"o\">.</span>flatten<span class=\"p\">()</span>\n<span class=\"go\">{&#39;True&#39;: True, &#39;None&#39;: None, &#39;foo&#39;: &#39;first level&#39;, &#39;False&#39;: False, &#39;bar&#39;: &#39;second level&#39;}</span>\n</code></pre></div>\n<p>A <code class=\"docutils literal notranslate\">flatten()</code> method is also internally used to make <code class=\"docutils literal notranslate\">Context</code> objects\ncomparable.</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=\"gp\">&gt;&gt;&gt; </span>c1 <span class=\"o\">=</span> Context<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c1<span class=\"p\">[</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;first level&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c1<span class=\"p\">[</span><span class=\"s2\">&quot;bar&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;second level&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c2 <span class=\"o\">=</span> Context<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c2<span class=\"o\">.</span>update<span class=\"p\">({</span><span class=\"s2\">&quot;bar&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;second level&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;first level&quot;</span><span class=\"p\">})</span>\n<span class=\"go\">{&#39;foo&#39;: &#39;first level&#39;, &#39;bar&#39;: &#39;second level&#39;}</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>c1 <span class=\"o\">==</span> c2\n<span class=\"go\">True</span>\n</code></pre></div>\n<p>Result from <code class=\"docutils literal notranslate\">flatten()</code> can be useful in unit tests to compare <code class=\"docutils literal notranslate\">Context</code>\nagainst <code class=\"docutils literal notranslate\">dict</code>:</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=\"k\">class</span> <span class=\"nc\">ContextTest</span><span class=\"p\">(</span>unittest<span class=\"o\">.</span>TestCase<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">test_against_dictionary</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        c1 <span class=\"o\">=</span> Context<span class=\"p\">()</span>\n        c1<span class=\"p\">[</span><span class=\"s2\">&quot;update&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;value&quot;</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span>\n            c1<span class=\"o\">.</span>flatten<span class=\"p\">(),</span>\n            <span class=\"p\">{</span>\n                <span class=\"s2\">&quot;True&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n                <span class=\"s2\">&quot;None&quot;</span><span class=\"p\">:</span> <span class=\"kc\">None</span><span class=\"p\">,</span>\n                <span class=\"s2\">&quot;False&quot;</span><span class=\"p\">:</span> <span class=\"kc\">False</span><span class=\"p\">,</span>\n                <span class=\"s2\">&quot;update&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;value&quot;</span><span class=\"p\">,</span>\n            <span class=\"p\">},</span>\n        <span class=\"p\">)</span>\n</code></pre></div>\n<section id=\"using-requestcontext\">\n<span id=\"subclassing-context-requestcontext\"></span><h3>Using <code class=\"docutils literal notranslate\">RequestContext</code><a class=\"heading-anchor\" href=\"#using-requestcontext\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.RequestContext\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">RequestContext</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">dict_<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">processors<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">use_l10n<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">use_tz<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">autoescape<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.RequestContext\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Django comes with a special <a class=\"reference internal\" href=\"#django.template.Context\" title=\"django.template.Context\"><code class=\"xref py py-class docutils literal notranslate\">Context</code></a> class,\n<code class=\"docutils literal notranslate\">django.template.RequestContext</code>, that acts slightly differently from the\nnormal <code class=\"docutils literal notranslate\">django.template.Context</code>. The first difference is that it takes an\n<a class=\"reference internal\" href=\"/en/6.1/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\">HttpRequest</code></a> as its first argument. For example:</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>c <span class=\"o\">=</span> RequestContext<span class=\"p\">(</span>\n    request<span class=\"p\">,</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;bar&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>The second difference is that it automatically populates the context with a\nfew variables, according to the engine’s <code class=\"docutils literal notranslate\">context_processors</code> configuration\noption.</p>\n<p>The <code class=\"docutils literal notranslate\">context_processors</code> option is a list of callables – called <strong>context\nprocessors</strong> – that take a request object as their argument and return a\ndictionary of items to be merged into the context. In the default generated\nsettings file, the default template engine contains the following context\nprocessors:</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=\"p\">[</span>\n    <span class=\"s2\">&quot;django.template.context_processors.request&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.contrib.auth.context_processors.auth&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.contrib.messages.context_processors.messages&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>In addition to these, <a class=\"reference internal\" href=\"#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\">RequestContext</code></a> always enables\n<code class=\"docutils literal notranslate\">'django.template.context_processors.csrf'</code>. This is a security related\ncontext processor required by the admin and other contrib apps, and, in case\nof accidental misconfiguration, it is deliberately hardcoded in and cannot be\nturned off in the <code class=\"docutils literal notranslate\">context_processors</code> option.</p>\n<p>Each processor is applied in order. That means, if one processor adds a\nvariable to the context and a second processor adds a variable with the same\nname, the second will override the first. The default processors are explained\nbelow.</p>\n<aside class=\"admonition-when-context-processors-are-applied admonition\" role=\"note\">\n<p class=\"admonition-title\">When context processors are applied</p>\n<p>Context processors are applied on top of context data. This means that a\ncontext processor may overwrite variables you’ve supplied to your\n<a class=\"reference internal\" href=\"#django.template.Context\" title=\"django.template.Context\"><code class=\"xref py py-class docutils literal notranslate\">Context</code></a> or <a class=\"reference internal\" href=\"#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\">RequestContext</code></a>, so take care to avoid\nvariable names that overlap with those supplied by your context\nprocessors.</p>\n<p>If you want context data to take priority over context processors, use the\nfollowing pattern:</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</span> <span class=\"kn\">import</span> RequestContext\n\nrequest_context <span class=\"o\">=</span> RequestContext<span class=\"p\">(</span>request<span class=\"p\">)</span>\nrequest_context<span class=\"o\">.</span>push<span class=\"p\">({</span><span class=\"s2\">&quot;my_name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Adrian&quot;</span><span class=\"p\">})</span>\n</code></pre></div>\n<p>Django does this to allow context data to override context processors in\nAPIs such as <a class=\"reference internal\" href=\"/en/6.1/topics/http/shortcuts/#django.shortcuts.render\" title=\"django.shortcuts.render\"><code class=\"xref py py-func docutils literal notranslate\">render()</code></a> and\n<a class=\"reference internal\" href=\"/en/6.1/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a>.</p>\n</aside>\n<p>Also, you can give <a class=\"reference internal\" href=\"#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\">RequestContext</code></a> a list of additional processors,\nusing the optional, third positional argument, <code class=\"docutils literal notranslate\">processors</code>. In this\nexample, the <a class=\"reference internal\" href=\"#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\">RequestContext</code></a> instance gets an <code class=\"docutils literal notranslate\">ip_address</code> variable:</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.http</span> <span class=\"kn\">import</span> HttpResponse\n<span class=\"kn\">from</span> <span class=\"nn\">django.template</span> <span class=\"kn\">import</span> RequestContext<span class=\"p\">,</span> Template\n\n\n<span class=\"k\">def</span> <span class=\"nf\">ip_address_processor</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"p\">{</span><span class=\"s2\">&quot;ip_address&quot;</span><span class=\"p\">:</span> request<span class=\"o\">.</span>META<span class=\"p\">[</span><span class=\"s2\">&quot;REMOTE_ADDR&quot;</span><span class=\"p\">]}</span>\n\n\n<span class=\"k\">def</span> <span class=\"nf\">client_ip_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    template <span class=\"o\">=</span> Template<span class=\"p\">(</span><span class=\"s2\">&quot;{{ title }}: {{ ip_address }}&quot;</span><span class=\"p\">)</span>\n    context <span class=\"o\">=</span> RequestContext<span class=\"p\">(</span>\n        request<span class=\"p\">,</span>\n        <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;title&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Your IP Address&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n        <span class=\"p\">[</span>ip_address_processor<span class=\"p\">],</span>\n    <span class=\"p\">)</span>\n    <span class=\"k\">return</span> HttpResponse<span class=\"p\">(</span>template<span class=\"o\">.</span>render<span class=\"p\">(</span>context<span class=\"p\">))</span>\n</code></pre></div>\n</section>\n<section id=\"built-in-template-context-processors\">\n<span id=\"context-processors\"></span><h3>Built-in template context processors<a class=\"heading-anchor\" href=\"#built-in-template-context-processors\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Here’s what each of the built-in processors does:</p>\n<section id=\"django-contrib-auth-context-processors-auth\">\n<h4><code class=\"docutils literal notranslate\">django.contrib.auth.context_processors.auth</code><a class=\"heading-anchor\" href=\"#django-contrib-auth-context-processors-auth\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.context_processors.auth\">\n<span class=\"sig-name descname\">auth</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.context_processors.auth\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If this processor is enabled, every <code class=\"docutils literal notranslate\">RequestContext</code> will contain these\nvariables:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">user</code> – An <code class=\"docutils literal notranslate\">auth.User</code> instance representing the currently\nlogged-in user (or an <code class=\"docutils literal notranslate\">AnonymousUser</code> instance, if the client isn’t\nlogged in).</p></li>\n<li><p><code class=\"docutils literal notranslate\">perms</code> – An instance of\n<code class=\"docutils literal notranslate\">django.contrib.auth.context_processors.PermWrapper</code>, representing the\npermissions that the currently logged-in user has.</p></li>\n</ul>\n</section>\n<section id=\"django-template-context-processors-debug\">\n<h4><code class=\"docutils literal notranslate\">django.template.context_processors.debug</code><a class=\"heading-anchor\" href=\"#django-template-context-processors-debug\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.template.context_processors.debug\">\n<span class=\"sig-name descname\">debug</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.context_processors.debug\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If this processor is enabled, every <code class=\"docutils literal notranslate\">RequestContext</code> will contain these two\nvariables – but only if your <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> setting is set to <code class=\"docutils literal notranslate\">True</code> and\nthe request’s IP address (<code class=\"docutils literal notranslate\">request.META['REMOTE_ADDR']</code>) is in the\n<a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-INTERNAL_IPS\"><code class=\"xref std std-setting docutils literal notranslate\">INTERNAL_IPS</code></a> setting:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">debug</code> – <code class=\"docutils literal notranslate\">True</code>. You can use this in templates to test whether\nyou’re in <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> mode.</p></li>\n<li><p><code class=\"docutils literal notranslate\">sql_queries</code> – A list of <code class=\"docutils literal notranslate\">{'sql': ..., 'time': ...}</code> dictionaries,\nrepresenting every SQL query that has happened so far during the request\nand how long it took. The list is in order by database alias and then by\nquery. It’s lazily generated on access.</p></li>\n</ul>\n</section>\n<section id=\"django-template-context-processors-i18n\">\n<h4><code class=\"docutils literal notranslate\">django.template.context_processors.i18n</code><a class=\"heading-anchor\" href=\"#django-template-context-processors-i18n\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.template.context_processors.i18n\">\n<span class=\"sig-name descname\">i18n</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.context_processors.i18n\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If this processor is enabled, every <code class=\"docutils literal notranslate\">RequestContext</code> will contain these\nvariables:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">LANGUAGES</code> – The value of the <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-LANGUAGES\"><code class=\"xref std std-setting docutils literal notranslate\">LANGUAGES</code></a> setting.</p></li>\n<li><p><code class=\"docutils literal notranslate\">LANGUAGE_BIDI</code> – <code class=\"docutils literal notranslate\">True</code> if the current language is a right-to-left\nlanguage, e.g. Hebrew, Arabic. <code class=\"docutils literal notranslate\">False</code> if it’s a left-to-right language,\ne.g. English, French, German.</p></li>\n<li><p><code class=\"docutils literal notranslate\">LANGUAGE_CODE</code> – <code class=\"docutils literal notranslate\">request.LANGUAGE_CODE</code>, if it exists. Otherwise,\nthe value of the <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-LANGUAGE_CODE\"><code class=\"xref std std-setting docutils literal notranslate\">LANGUAGE_CODE</code></a> setting.</p></li>\n</ul>\n<p>See <a class=\"reference internal\" href=\"/en/6.1/topics/i18n/translation/#i18n-template-tags\"><span class=\"std std-ref\">i18n template tags</span></a> for template tags that\ngenerate the same values.</p>\n</section>\n<section id=\"django-template-context-processors-media\">\n<h4><code class=\"docutils literal notranslate\">django.template.context_processors.media</code><a class=\"heading-anchor\" href=\"#django-template-context-processors-media\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>If this processor is enabled, every <code class=\"docutils literal notranslate\">RequestContext</code> will contain a variable\n<code class=\"docutils literal notranslate\">MEDIA_URL</code>, providing the value of the <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_URL</code></a> setting.</p>\n</section>\n<section id=\"django-template-context-processors-static\">\n<h4><code class=\"docutils literal notranslate\">django.template.context_processors.static</code><a class=\"heading-anchor\" href=\"#django-template-context-processors-static\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.template.context_processors.static\">\n<span class=\"sig-name descname\">static</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.context_processors.static\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If this processor is enabled, every <code class=\"docutils literal notranslate\">RequestContext</code> will contain a variable\n<code class=\"docutils literal notranslate\">STATIC_URL</code>, providing the value of the <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_URL</code></a> setting.</p>\n</section>\n<section id=\"django-template-context-processors-csrf\">\n<h4><code class=\"docutils literal notranslate\">django.template.context_processors.csrf</code><a class=\"heading-anchor\" href=\"#django-template-context-processors-csrf\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>This processor adds a token that is needed by the <a class=\"reference internal\" href=\"/en/6.1/ref/templates/builtins/#std-templatetag-csrf_token\"><code class=\"xref std std-ttag docutils literal notranslate\">csrf_token</code></a> template\ntag for protection against <a class=\"reference internal\" href=\"/en/6.1/ref/csrf/\"><span class=\"doc\">Cross Site Request Forgeries</span></a>.</p>\n<p>This is always enabled by <a class=\"reference internal\" href=\"#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\">RequestContext</code></a> and cannot\nbe disabled. You do not need to include it within <code class=\"docutils literal notranslate\">context_processors</code>.</p>\n</section>\n<section id=\"django-template-context-processors-csp\">\n<h4><code class=\"docutils literal notranslate\">django.template.context_processors.csp</code><a class=\"heading-anchor\" href=\"#django-template-context-processors-csp\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.template.context_processors.csp\">\n<span class=\"sig-name descname\">csp</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.context_processors.csp\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"version-note version-added\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">New in Django 6.0</p></aside>\n<p>If this processor is enabled, every <code class=\"docutils literal notranslate\">RequestContext</code> will contain a variable\n<code class=\"docutils literal notranslate\">csp_nonce</code>, providing a securely generated, request-specific nonce suitable\nfor use under a Content Security Policy. See <a class=\"reference internal\" href=\"/en/6.1/ref/csp/#csp-nonce\"><span class=\"std std-ref\">CSP nonce usage</span></a>\nfor details.</p>\n</section>\n<section id=\"django-template-context-processors-request\">\n<h4><code class=\"docutils literal notranslate\">django.template.context_processors.request</code><a class=\"heading-anchor\" href=\"#django-template-context-processors-request\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>If this processor is enabled, every <code class=\"docutils literal notranslate\">RequestContext</code> will contain a variable\n<code class=\"docutils literal notranslate\">request</code>, which is the current <a class=\"reference internal\" href=\"/en/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</section>\n<section id=\"django-template-context-processors-tz\">\n<h4><code class=\"docutils literal notranslate\">django.template.context_processors.tz</code><a class=\"heading-anchor\" href=\"#django-template-context-processors-tz\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.template.context_processors.tz\">\n<span class=\"sig-name descname\">tz</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.context_processors.tz\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If this processor is enabled, every <code class=\"docutils literal notranslate\">RequestContext</code> will contain a variable\n<code class=\"docutils literal notranslate\">TIME_ZONE</code>, providing the name of the currently active time zone.</p>\n</section>\n<section id=\"django-contrib-messages-context-processors-messages\">\n<h4><code class=\"docutils literal notranslate\">django.contrib.messages.context_processors.messages</code><a class=\"heading-anchor\" href=\"#django-contrib-messages-context-processors-messages\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>If this processor is enabled, every <code class=\"docutils literal notranslate\">RequestContext</code> will contain these two\nvariables:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">messages</code> – A list of messages (as strings) that have been set\nvia the <a class=\"reference internal\" href=\"/en/6.1/ref/contrib/messages/\"><span class=\"doc\">messages framework</span></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\">DEFAULT_MESSAGE_LEVELS</code> – A mapping of the message level names to\n<a class=\"reference internal\" href=\"/en/6.1/ref/contrib/messages/#message-level-constants\"><span class=\"std std-ref\">their numeric value</span></a>.</p></li>\n</ul>\n</section>\n</section>\n<section id=\"writing-your-own-context-processors\">\n<h3>Writing your own context processors<a class=\"heading-anchor\" href=\"#writing-your-own-context-processors\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>A context processor has a simple interface: It’s a Python function that takes\none argument, an <a class=\"reference internal\" href=\"/en/6.1/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\">HttpRequest</code></a> object, and returns a\ndictionary that gets added to the template context.</p>\n<p>For example, to add the <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-DEFAULT_FROM_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_FROM_EMAIL</code></a> setting to every\ncontext:</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.conf</span> <span class=\"kn\">import</span> settings\n\n\n<span class=\"k\">def</span> <span class=\"nf\">from_email</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;DEFAULT_FROM_EMAIL&quot;</span><span class=\"p\">:</span> settings<span class=\"o\">.</span>DEFAULT_FROM_EMAIL<span class=\"p\">,</span>\n    <span class=\"p\">}</span>\n</code></pre></div>\n<p>Custom context processors can live anywhere in your code base. All Django\ncares about is that your custom context processors are pointed to by the\n<code class=\"docutils literal notranslate\">'context_processors'</code> option in your <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\">TEMPLATES</code></a> setting — or the\n<code class=\"docutils literal notranslate\">context_processors</code> argument of <a class=\"reference internal\" href=\"#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\">Engine</code></a> if you’re\nusing it directly.</p>\n</section>\n</section>\n<section id=\"loading-templates\">\n<h2>Loading templates<a class=\"heading-anchor\" href=\"#loading-templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Generally, you’ll store templates in files on your filesystem rather than\nusing the low-level <a class=\"reference internal\" href=\"#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\">Template</code></a> API yourself. Save\ntemplates in a directory specified as a <strong>template directory</strong>.</p>\n<p>Django searches for template directories in a number of places, depending on\nyour template loading settings (see “Loader types” below), but the most basic\nway of specifying template directories is by using the <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">DIRS</code></a> option.</p>\n<section id=\"the-dirs-option\">\n<h3>The <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">DIRS</code></a> option<a class=\"heading-anchor\" href=\"#the-dirs-option\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Tell Django what your template directories are by using the <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">DIRS</code></a> option in the <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\">TEMPLATES</code></a> setting in your settings\nfile — or the <code class=\"docutils literal notranslate\">dirs</code> argument of <a class=\"reference internal\" href=\"#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\">Engine</code></a>. This\nshould be set to a list of strings that contain full paths to your template\ndirectories:</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>TEMPLATES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.template.backends.django.DjangoTemplates&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;DIRS&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n            <span class=\"s2\">&quot;/home/html/templates/lawrence.com&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;/home/html/templates/default&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">],</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Your templates can go anywhere you want, as long as the directories and\ntemplates are readable by the web server. They can have any extension you want,\nsuch as <code class=\"docutils literal notranslate\">.html</code> or <code class=\"docutils literal notranslate\">.txt</code>, or they can have no extension at all.</p>\n<p>Note that these paths should use Unix-style forward slashes, even on Windows.</p>\n</section>\n<section id=\"loader-types\">\n<span id=\"template-loaders\"></span><h3>Loader types<a class=\"heading-anchor\" href=\"#loader-types\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>By default, Django uses a filesystem-based template loader, but Django comes\nwith a few other template loaders, which know how to load templates from other\nsources.</p>\n<p>Some of these other loaders are disabled by default, but you can activate them\nby adding a <code class=\"docutils literal notranslate\">'loaders'</code> option to your <code class=\"docutils literal notranslate\">DjangoTemplates</code> backend in the\n<a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\">TEMPLATES</code></a> setting or passing a <code class=\"docutils literal notranslate\">loaders</code> argument to\n<a class=\"reference internal\" href=\"#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\">Engine</code></a>. <code class=\"docutils literal notranslate\">loaders</code> should be a list of strings or\ntuples, where each represents a template loader class. Here are the template\nloaders that come with Django:</p>\n<p><code class=\"docutils literal notranslate\">django.template.loaders.filesystem.Loader</code></p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.loaders.filesystem.Loader\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">filesystem.</span><span class=\"sig-name descname\">Loader</span><a class=\"heading-anchor\" href=\"#django.template.loaders.filesystem.Loader\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Loads templates from the filesystem, according to\n<a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">DIRS</code></a>.</p>\n<p>This loader is enabled by default. However it won’t find any templates\nuntil you set <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">DIRS</code></a> to a non-empty list:</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>TEMPLATES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.template.backends.django.DjangoTemplates&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;DIRS&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>BASE_DIR <span class=\"o\">/</span> <span class=\"s2\">&quot;templates&quot;</span><span class=\"p\">],</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>You can also override <code class=\"docutils literal notranslate\">'DIRS'</code> and specify specific directories for a\nparticular filesystem loader:</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>TEMPLATES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.template.backends.django.DjangoTemplates&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;loaders&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n                <span class=\"p\">(</span>\n                    <span class=\"s2\">&quot;django.template.loaders.filesystem.Loader&quot;</span><span class=\"p\">,</span>\n                    <span class=\"p\">[</span>BASE_DIR <span class=\"o\">/</span> <span class=\"s2\">&quot;templates&quot;</span><span class=\"p\">],</span>\n                <span class=\"p\">),</span>\n            <span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n</dd></dl>\n\n<p><code class=\"docutils literal notranslate\">django.template.loaders.app_directories.Loader</code></p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.loaders.app_directories.Loader\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">app_directories.</span><span class=\"sig-name descname\">Loader</span><a class=\"heading-anchor\" href=\"#django.template.loaders.app_directories.Loader\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Loads templates from Django apps on the filesystem. For each app in\n<a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a>, the loader looks for a <code class=\"docutils literal notranslate\">templates</code>\nsubdirectory. If the directory exists, Django looks for templates in there.</p>\n<p>This means you can store templates with your individual apps. This also\nhelps to distribute Django apps with default templates.</p>\n<p>For example, for this setting:</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>INSTALLED_APPS <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;myproject.polls&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;myproject.music&quot;</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>…then <code class=\"docutils literal notranslate\">get_template('foo.html')</code> will look for <code class=\"docutils literal notranslate\">foo.html</code> in these\ndirectories, in this order:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">/path/to/myproject/polls/templates/</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">/path/to/myproject/music/templates/</code></p></li>\n</ul>\n<p>… and will use the one it finds first.</p>\n<p>The order of <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> is significant! For example, if you\nwant to customize the Django admin, you might choose to override the\nstandard <code class=\"docutils literal notranslate\">admin/base_site.html</code> template, from <code class=\"docutils literal notranslate\">django.contrib.admin</code>,\nwith your own <code class=\"docutils literal notranslate\">admin/base_site.html</code> in <code class=\"docutils literal notranslate\">myproject.polls</code>. You must\nthen make sure that your <code class=\"docutils literal notranslate\">myproject.polls</code> comes <em>before</em>\n<code class=\"docutils literal notranslate\">django.contrib.admin</code> in <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a>, otherwise\n<code class=\"docutils literal notranslate\">django.contrib.admin</code>’s will be loaded first and yours will be ignored.</p>\n<p>Note that the loader performs an optimization when it first runs:\nit caches a list of which <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> packages have a\n<code class=\"docutils literal notranslate\">templates</code> subdirectory.</p>\n<p>You can enable this loader by setting <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">APP_DIRS</code></a> to <code class=\"docutils literal notranslate\">True</code>:</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>TEMPLATES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.template.backends.django.DjangoTemplates&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;APP_DIRS&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n</dd></dl>\n\n<p><code class=\"docutils literal notranslate\">django.template.loaders.cached.Loader</code></p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.loaders.cached.Loader\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">cached.</span><span class=\"sig-name descname\">Loader</span><a class=\"heading-anchor\" href=\"#django.template.loaders.cached.Loader\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>While the Django template system is quite fast, if it needs to read and\ncompile your templates every time they’re rendered, the overhead from that\ncan add up.</p>\n<p>You configure the cached template loader with a list of other loaders that\nit should wrap. The wrapped loaders are used to locate unknown templates\nwhen they’re first encountered. The cached loader then stores the compiled\n<code class=\"docutils literal notranslate\">Template</code> in memory. The cached <code class=\"docutils literal notranslate\">Template</code> instance is returned for\nsubsequent requests to load the same template.</p>\n<p>This loader is automatically enabled if <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-TEMPLATES-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\">OPTIONS['loaders']</code></a> isn’t specified.</p>\n<p>You can manually specify template caching with some custom template loaders\nusing settings like this:</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>TEMPLATES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.template.backends.django.DjangoTemplates&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;DIRS&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>BASE_DIR <span class=\"o\">/</span> <span class=\"s2\">&quot;templates&quot;</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;loaders&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n                <span class=\"p\">(</span>\n                    <span class=\"s2\">&quot;django.template.loaders.cached.Loader&quot;</span><span class=\"p\">,</span>\n                    <span class=\"p\">[</span>\n                        <span class=\"s2\">&quot;django.template.loaders.filesystem.Loader&quot;</span><span class=\"p\">,</span>\n                        <span class=\"s2\">&quot;django.template.loaders.app_directories.Loader&quot;</span><span class=\"p\">,</span>\n                        <span class=\"s2\">&quot;path.to.custom.Loader&quot;</span><span class=\"p\">,</span>\n                    <span class=\"p\">],</span>\n                <span class=\"p\">),</span>\n            <span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>All of the built-in Django template tags are safe to use with the\ncached loader, but if you’re using custom template tags that come from\nthird party packages, or that you wrote yourself, you should ensure\nthat the <code class=\"docutils literal notranslate\">Node</code> implementation for each tag is thread-safe. For more\ninformation, see <a class=\"reference internal\" href=\"/en/6.1/howto/custom-template-tags/#template-tag-thread-safety\"><span class=\"std std-ref\">template tag thread safety considerations</span></a>.</p>\n</aside>\n</dd></dl>\n\n<p><code class=\"docutils literal notranslate\">django.template.loaders.locmem.Loader</code></p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.loaders.locmem.Loader\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">locmem.</span><span class=\"sig-name descname\">Loader</span><a class=\"heading-anchor\" href=\"#django.template.loaders.locmem.Loader\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Loads templates from a Python dictionary. This is useful for testing.</p>\n<p>This loader takes a dictionary of templates as its first argument:</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>TEMPLATES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.template.backends.django.DjangoTemplates&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;loaders&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n                <span class=\"p\">(</span>\n                    <span class=\"s2\">&quot;django.template.loaders.locmem.Loader&quot;</span><span class=\"p\">,</span>\n                    <span class=\"p\">{</span>\n                        <span class=\"s2\">&quot;index.html&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;content here&quot;</span><span class=\"p\">,</span>\n                    <span class=\"p\">},</span>\n                <span class=\"p\">),</span>\n            <span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>This loader is disabled by default.</p>\n</dd></dl>\n\n<p>Django uses the template loaders in order according to the <code class=\"docutils literal notranslate\">'loaders'</code>\noption. It uses each loader until a loader finds a match.</p>\n</section>\n</section>\n<section id=\"custom-loaders\">\n<span id=\"custom-template-loaders\"></span><h2>Custom loaders<a class=\"heading-anchor\" href=\"#custom-loaders\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>It’s possible to load templates from additional sources using custom template\nloaders. Custom <code class=\"docutils literal notranslate\">Loader</code> classes should inherit from\n<code class=\"docutils literal notranslate\">django.template.loaders.base.Loader</code> and define the <code class=\"docutils literal notranslate\">get_contents()</code> and\n<code class=\"docutils literal notranslate\">get_template_sources()</code> methods.</p>\n<section id=\"loader-methods\">\n<h3>Loader methods<a class=\"heading-anchor\" href=\"#loader-methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.loaders.base.Loader\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Loader</span><a class=\"heading-anchor\" href=\"#django.template.loaders.base.Loader\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Loads templates from a given source, such as the filesystem or a database.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.loaders.base.Loader.get_template_sources\">\n<span class=\"sig-name descname\">get_template_sources</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">template_name</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.loaders.base.Loader.get_template_sources\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A method that takes a <code class=\"docutils literal notranslate\">template_name</code> and yields\n<a class=\"reference internal\" href=\"#django.template.base.Origin\" title=\"django.template.base.Origin\"><code class=\"xref py py-class docutils literal notranslate\">Origin</code></a> instances for each possible\nsource.</p>\n<p>For example, the filesystem loader may receive <code class=\"docutils literal notranslate\">'index.html'</code> as a\n<code class=\"docutils literal notranslate\">template_name</code> argument. This method would yield origins for the\nfull path of <code class=\"docutils literal notranslate\">index.html</code> as it appears in each template directory\nthe loader looks at.</p>\n<p>The method doesn’t need to verify that the template exists at a given\npath, but it should ensure the path is valid. For instance, the\nfilesystem loader makes sure the path lies under a valid template\ndirectory.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.loaders.base.Loader.get_contents\">\n<span class=\"sig-name descname\">get_contents</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">origin</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.loaders.base.Loader.get_contents\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the contents for a template given a\n<a class=\"reference internal\" href=\"#django.template.base.Origin\" title=\"django.template.base.Origin\"><code class=\"xref py py-class docutils literal notranslate\">Origin</code></a> instance.</p>\n<p>This is where a filesystem loader would read contents from the\nfilesystem, or a database loader would read from the database. If a\nmatching template doesn’t exist, this should raise a\n<a class=\"reference internal\" href=\"/en/6.1/topics/templates/#django.template.TemplateDoesNotExist\" title=\"django.template.TemplateDoesNotExist\"><code class=\"xref py py-exc docutils literal notranslate\">TemplateDoesNotExist</code></a> error.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.loaders.base.Loader.get_template\">\n<span class=\"sig-name descname\">get_template</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">template_name</em>, <em class=\"sig-param\">skip<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.loaders.base.Loader.get_template\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a <code class=\"docutils literal notranslate\">Template</code> object for a given <code class=\"docutils literal notranslate\">template_name</code> by looping\nthrough results from <a class=\"reference internal\" href=\"#django.template.loaders.base.Loader.get_template_sources\" title=\"django.template.loaders.base.Loader.get_template_sources\"><code class=\"xref py py-meth docutils literal notranslate\">get_template_sources()</code></a> and calling\n<a class=\"reference internal\" href=\"#django.template.loaders.base.Loader.get_contents\" title=\"django.template.loaders.base.Loader.get_contents\"><code class=\"xref py py-meth docutils literal notranslate\">get_contents()</code></a>. This returns the first matching template. If no\ntemplate is found, <a class=\"reference internal\" href=\"/en/6.1/topics/templates/#django.template.TemplateDoesNotExist\" title=\"django.template.TemplateDoesNotExist\"><code class=\"xref py py-exc docutils literal notranslate\">TemplateDoesNotExist</code></a> is\nraised.</p>\n<p>The optional <code class=\"docutils literal notranslate\">skip</code> argument is a list of origins to ignore when\nextending templates. This allow templates to extend other templates of\nthe same name. It also used to avoid recursion errors.</p>\n<p>In general, it is enough to define <a class=\"reference internal\" href=\"#django.template.loaders.base.Loader.get_template_sources\" title=\"django.template.loaders.base.Loader.get_template_sources\"><code class=\"xref py py-meth docutils literal notranslate\">get_template_sources()</code></a> and\n<a class=\"reference internal\" href=\"#django.template.loaders.base.Loader.get_contents\" title=\"django.template.loaders.base.Loader.get_contents\"><code class=\"xref py py-meth docutils literal notranslate\">get_contents()</code></a> for custom template loaders. <code class=\"docutils literal notranslate\">get_template()</code>\nwill usually not need to be overridden.</p>\n</dd></dl>\n\n</dd></dl>\n\n<aside class=\"admonition-building-your-own admonition\" role=\"note\">\n<p class=\"admonition-title\">Building your own</p>\n<p>For examples, read the <a class=\"extlink-source reference external\" href=\"https://github.com/django/django/blob/stable/6.1.x/django/template/loaders\">source code for Django’s built-in loaders</a>.</p>\n</aside>\n</section>\n</section>\n<section id=\"template-origin\">\n<h2>Template origin<a class=\"heading-anchor\" href=\"#template-origin\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Templates have an <code class=\"docutils literal notranslate\">origin</code> containing attributes depending on the source\nthey are loaded from.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.base.Origin\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Origin</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">name</em>, <em class=\"sig-param\">template_name<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">loader<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.base.Origin\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.template.base.Origin.name\">\n<span class=\"sig-name descname\">name</span><a class=\"heading-anchor\" href=\"#django.template.base.Origin.name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The path to the template as returned by the template loader.\nFor loaders that read from the file system, this is the full\npath to the template.</p>\n<p>If the template is instantiated directly rather than through a\ntemplate loader, this is a string value of <code class=\"docutils literal notranslate\">&lt;unknown_source&gt;</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.template.base.Origin.template_name\">\n<span class=\"sig-name descname\">template_name</span><a class=\"heading-anchor\" href=\"#django.template.base.Origin.template_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The relative path to the template as passed into the\ntemplate loader.</p>\n<p>If the template is instantiated directly rather than through a\ntemplate loader, this is <code class=\"docutils literal notranslate\">None</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.template.base.Origin.loader\">\n<span class=\"sig-name descname\">loader</span><a class=\"heading-anchor\" href=\"#django.template.base.Origin.loader\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The template loader instance that constructed this <code class=\"docutils literal notranslate\">Origin</code>.</p>\n<p>If the template is instantiated directly rather than through a\ntemplate loader, this is <code class=\"docutils literal notranslate\">None</code>.</p>\n<p><a class=\"reference internal\" href=\"#django.template.loaders.cached.Loader\" title=\"django.template.loaders.cached.Loader\"><code class=\"xref py py-class docutils literal notranslate\">django.template.loaders.cached.Loader</code></a> requires all of its\nwrapped loaders to set this attribute, typically by instantiating\nthe <code class=\"docutils literal notranslate\">Origin</code> with <code class=\"docutils literal notranslate\">loader=self</code>.</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>","rootId":"the-django-template-language-for-python-programmers","toc":[{"title":"Overview","anchor":"overview","children":[]},{"title":"Configuring an engine","anchor":"configuring-an-engine","children":[]},{"title":"Loading a template","anchor":"loading-a-template","children":[]},{"title":"Rendering a context","anchor":"rendering-a-context","children":[{"title":"Variables and lookups","anchor":"variables-and-lookups","children":[]},{"title":"How invalid variables are handled","anchor":"how-invalid-variables-are-handled","children":[]},{"title":"Built-in variables","anchor":"built-in-variables","children":[]},{"title":"Limitations with string literals","anchor":"limitations-with-string-literals","children":[]}]},{"title":"Playing with Context objects","anchor":"playing-with-context-objects","children":[{"title":"Using RequestContext","anchor":"using-requestcontext","children":[]},{"title":"Built-in template context processors","anchor":"built-in-template-context-processors","children":[{"title":"django.contrib.auth.context_processors.auth","anchor":"django-contrib-auth-context-processors-auth","children":[]},{"title":"django.template.context_processors.debug","anchor":"django-template-context-processors-debug","children":[]},{"title":"django.template.context_processors.i18n","anchor":"django-template-context-processors-i18n","children":[]},{"title":"django.template.context_processors.media","anchor":"django-template-context-processors-media","children":[]},{"title":"django.template.context_processors.static","anchor":"django-template-context-processors-static","children":[]},{"title":"django.template.context_processors.csrf","anchor":"django-template-context-processors-csrf","children":[]},{"title":"django.template.context_processors.csp","anchor":"django-template-context-processors-csp","children":[]},{"title":"django.template.context_processors.request","anchor":"django-template-context-processors-request","children":[]},{"title":"django.template.context_processors.tz","anchor":"django-template-context-processors-tz","children":[]},{"title":"django.contrib.messages.context_processors.messages","anchor":"django-contrib-messages-context-processors-messages","children":[]}]},{"title":"Writing your own context processors","anchor":"writing-your-own-context-processors","children":[]}]},{"title":"Loading templates","anchor":"loading-templates","children":[{"title":"The DIRS option","anchor":"the-dirs-option","children":[]},{"title":"Loader types","anchor":"loader-types","children":[]}]},{"title":"Custom loaders","anchor":"custom-loaders","children":[{"title":"Loader methods","anchor":"loader-methods","children":[]}]},{"title":"Template origin","anchor":"template-origin","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/en/6.1/ref/"},{"docname":"ref/templates/index","title":"Templates","url":"/en/6.1/ref/templates/"}],"prev":{"docname":"ref/templates/builtins","title":"Built-in template tags and filters","url":"/en/6.1/ref/templates/builtins/"},"next":{"docname":"ref/template-response","title":"TemplateResponse and SimpleTemplateResponse","url":"/en/6.1/ref/template-response/"},"formats":{"html":"/en/6.1/ref/templates/api/","markdown":"/en/6.1/ref/templates/api.md","json":"/en/6.1/ref/templates/api.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/ref/templates/api.txt","official":"https://docs.djangoproject.com/en/6.1/ref/templates/api/","inVersions":["dev","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","1.8"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}