<!DOCTYPE html>

<html lang="en" data-content_root="../../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>The Django template language: for Python programmers &#8212; Django 5.2.8 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=34bb78ad" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css?v=bf4d74af" />
    <script src="../../_static/documentation_options.js?v=1e3ff871"></script>
    <script src="../../_static/doctools.js?v=9bcbadda"></script>
    <script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="TemplateResponse and SimpleTemplateResponse" href="../template-response.html" />
    <link rel="prev" title="Built-in template tags and filters" href="builtins.html" />



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

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 5.2.8 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="builtins.html" title="Built-in template tags and filters">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="../template-response.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;TemplateResponse&lt;/span&gt;&lt;/code&gt; and &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;SimpleTemplateResponse&lt;/span&gt;&lt;/code&gt;">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-templates-api">
            
  <section id="s-the-django-template-language-for-python-programmers">
<span id="the-django-template-language-for-python-programmers"></span><h1>The Django template language: for Python programmers<a class="headerlink" href="#the-django-template-language-for-python-programmers" title="Link to this heading">¶</a></h1>
<p>This document explains the Django template system from a technical
perspective – how it works and how to extend it. If you’re looking for
reference on the language syntax, see <a class="reference internal" href="language.html"><span class="doc">The Django template language</span></a>.</p>
<p>It assumes an understanding of templates, contexts, variables, tags, and
rendering. Start with the <a class="reference internal" href="../../topics/templates.html#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>
<section id="s-overview">
<span id="overview"></span><h2>Overview<a class="headerlink" href="#overview" title="Link to this heading">¶</a></h2>
<p>Using the template system in Python is a three-step process:</p>
<ol class="arabic simple">
<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"><span class="pre">Engine</span></code></a>.</p></li>
<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"><span class="pre">Template</span></code></a>.</p></li>
<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"><span class="pre">Context</span></code></a>.</p></li>
</ol>
<p>Django projects generally rely on the <a class="reference internal" href="../../topics/templates.html#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
lower level APIs:</p>
<ol class="arabic simple">
<li><p>For each <a class="reference internal" href="../../topics/templates.html#django.template.backends.django.DjangoTemplates" title="django.template.backends.django.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a> backend
in the <a class="reference internal" href="../settings.html#std-setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting, Django instantiates an
<a class="reference internal" href="#django.template.Engine" title="django.template.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>. <a class="reference internal" href="../../topics/templates.html#django.template.backends.django.DjangoTemplates" title="django.template.backends.django.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a>
wraps <a class="reference internal" href="#django.template.Engine" title="django.template.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> and adapts it to the common template backend API.</p></li>
<li><p>The <a class="reference internal" href="../../topics/templates.html#module-django.template.loader" title="django.template.loader"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.template.loader</span></code></a> module provides functions such as
<a class="reference internal" href="../../topics/templates.html#django.template.loader.get_template" title="django.template.loader.get_template"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_template()</span></code></a> for loading templates. They
return a <code class="docutils literal notranslate"><span class="pre">django.template.backends.django.Template</span></code> which wraps the
actual <a class="reference internal" href="#django.template.Template" title="django.template.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.template.Template</span></code></a>.</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">Template</span></code> obtained in the previous step has a
<a class="reference internal" href="../../topics/templates.html#django.template.backends.base.Template.render" title="django.template.backends.base.Template.render"><code class="xref py py-meth docutils literal notranslate"><span class="pre">render()</span></code></a> method which
marshals 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"><span class="pre">Context</span></code></a> and
delegates 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"><span class="pre">Template</span></code></a>.</p></li>
</ol>
</section>
<section id="s-configuring-an-engine">
<span id="configuring-an-engine"></span><h2>Configuring an engine<a class="headerlink" href="#configuring-an-engine" title="Link to this heading">¶</a></h2>
<p>If you are using the <a class="reference internal" href="../../topics/templates.html#django.template.backends.django.DjangoTemplates" title="django.template.backends.django.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a>
backend, this probably isn’t the documentation you’re looking for. An instance
of the <code class="docutils literal notranslate"><span class="pre">Engine</span></code> class described below is accessible using the <code class="docutils literal notranslate"><span class="pre">engine</span></code>
attribute of that backend and any attribute defaults mentioned below are
overridden by what’s passed by
<a class="reference internal" href="../../topics/templates.html#django.template.backends.django.DjangoTemplates" title="django.template.backends.django.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a>.</p>
<dl class="py class">
<dt class="sig sig-object py" id="django.template.Engine">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">Engine</span></span>(<em class="sig-param"><span class="n"><span class="pre">dirs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">app_dirs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context_processors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">debug</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">loaders</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">string_if_invalid</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file_charset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'utf-8'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">libraries</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">builtins</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">autoescape</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/engine.py#L13"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.Engine" title="Link to this definition">¶</a></dt>
<dd><p>When instantiating an <code class="docutils literal notranslate"><span class="pre">Engine</span></code> all arguments must be passed as keyword
arguments:</p>
<ul>
<li><p><code class="docutils literal notranslate"><span class="pre">dirs</span></code> is a list of directories where the engine should look for
template source files. It is used to configure
<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"><span class="pre">filesystem.Loader</span></code></a>.</p>
<p>It defaults to an empty list.</p>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">app_dirs</span></code> only affects the default value of <code class="docutils literal notranslate"><span class="pre">loaders</span></code>. See below.</p>
<p>It defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">autoescape</span></code> controls whether HTML autoescaping is enabled.</p>
<p>It defaults to <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Only set it to <code class="docutils literal notranslate"><span class="pre">False</span></code> if you’re rendering non-HTML templates!</p>
</div>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">context_processors</span></code> is a list of dotted Python paths to callables
that are used to populate the context when a template is rendered with a
request. These callables take a request object as their argument and
return 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"><span class="pre">dict</span></code></a> of items to be merged into the context.</p>
<p>It defaults to an empty list.</p>
<p>See <a class="reference internal" href="#django.template.RequestContext" title="django.template.RequestContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">RequestContext</span></code></a> for more information.</p>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">debug</span></code> is a boolean that turns on/off template debug mode. If it is
<code class="docutils literal notranslate"><span class="pre">True</span></code>, the template engine will store additional debug information
which can be used to display a detailed report for any exception raised
during template rendering.</p>
<p>It defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">loaders</span></code> is a list of template loader classes, specified as strings.
Each <code class="docutils literal notranslate"><span class="pre">Loader</span></code> class knows how to import templates from a particular
source. Optionally, a tuple can be used instead of a string. The first
item in the tuple should be the <code class="docutils literal notranslate"><span class="pre">Loader</span></code> class name, subsequent items
are passed to the <code class="docutils literal notranslate"><span class="pre">Loader</span></code> during initialization.</p>
<p>It defaults to a list containing:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">'django.template.loaders.filesystem.Loader'</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">'django.template.loaders.app_directories.Loader'</span></code> if and only if
<code class="docutils literal notranslate"><span class="pre">app_dirs</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p></li>
</ul>
<p>These loaders are then wrapped in
<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"><span class="pre">django.template.loaders.cached.Loader</span></code></a>.</p>
<p>See <a class="reference internal" href="#template-loaders"><span class="std std-ref">Loader types</span></a> for details.</p>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code> is the output, as a string, that the template
system should use for invalid (e.g. misspelled) variables.</p>
<p>It defaults to the empty string.</p>
<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>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">file_charset</span></code> is the charset used to read template files on disk.</p>
<p>It defaults to <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code>.</p>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">'libraries'</span></code>: A dictionary of labels and dotted Python paths of template
tag modules to register with the template engine. This is used to add new
libraries or provide alternate labels for existing ones. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Engine</span><span class="p">(</span>
    <span class="n">libraries</span><span class="o">=</span><span class="p">{</span>
        <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>
        <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>
    <span class="p">},</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Libraries can be loaded by passing the corresponding dictionary key to
the <a class="reference internal" href="builtins.html#std-templatetag-load"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">%}</span></code></a> tag.</p>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">'builtins'</span></code>: A list of dotted Python paths of template tag modules to
add to <a class="reference internal" href="builtins.html"><span class="doc">built-ins</span></a>. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Engine</span><span class="p">(</span>
    <span class="n">builtins</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;myapp.builtins&quot;</span><span class="p">],</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Tags and filters from built-in libraries can be used without first calling
the <a class="reference internal" href="builtins.html#std-templatetag-load"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">%}</span></code></a> tag.</p>
</li>
</ul>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="django.template.Engine.get_default">
<em class="property"><span class="pre">static</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">Engine.</span></span><span class="sig-name descname"><span class="pre">get_default</span></span>()<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/engine.py#L89"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.Engine.get_default" title="Link to this definition">¶</a></dt>
<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"><span class="pre">Engine</span></code></a> from the first configured
<a class="reference internal" href="../../topics/templates.html#django.template.backends.django.DjangoTemplates" title="django.template.backends.django.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a> engine. Raises
<a class="reference internal" href="../exceptions.html#django.core.exceptions.ImproperlyConfigured" title="django.core.exceptions.ImproperlyConfigured"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImproperlyConfigured</span></code></a> if no engines are
configured.</p>
<p>It’s required for preserving APIs that rely on a globally available,
implicitly configured engine. Any other use is strongly discouraged.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="django.template.Engine.from_string">
<span class="sig-prename descclassname"><span class="pre">Engine.</span></span><span class="sig-name descname"><span class="pre">from_string</span></span>(<em class="sig-param"><span class="n"><span class="pre">template_code</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/engine.py#L165"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.Engine.from_string" title="Link to this definition">¶</a></dt>
<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"><span class="pre">Template</span></code></a> object.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="django.template.Engine.get_template">
<span class="sig-prename descclassname"><span class="pre">Engine.</span></span><span class="sig-name descname"><span class="pre">get_template</span></span>(<em class="sig-param"><span class="n"><span class="pre">template_name</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/engine.py#L172"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.Engine.get_template" title="Link to this definition">¶</a></dt>
<dd><p>Loads a template with the given name, compiles it and returns a
<a class="reference internal" href="#django.template.Template" title="django.template.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> object.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="django.template.Engine.select_template">
<span class="sig-prename descclassname"><span class="pre">Engine.</span></span><span class="sig-name descname"><span class="pre">select_template</span></span>(<em class="sig-param"><span class="n"><span class="pre">template_name_list</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/engine.py#L219"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.Engine.select_template" title="Link to this definition">¶</a></dt>
<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"><span class="pre">get_template()</span></code></a>, except it takes a list of names
and returns the first template that was found.</p>
</dd></dl>

</section>
<section id="s-loading-a-template">
<span id="loading-a-template"></span><h2>Loading a template<a class="headerlink" href="#loading-a-template" title="Link to this heading">¶</a></h2>
<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"><span class="pre">Template</span></code></a> is by calling the factory
methods of the <a class="reference internal" href="#django.template.Engine" title="django.template.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></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"><span class="pre">get_template()</span></code></a>,
<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"><span class="pre">select_template()</span></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"><span class="pre">from_string()</span></code></a>.</p>
<p>In a Django project where the <a class="reference internal" href="../settings.html#std-setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting defines a
<a class="reference internal" href="../../topics/templates.html#django.template.backends.django.DjangoTemplates" title="django.template.backends.django.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a> engine, it’s
possible to instantiate a <a class="reference internal" href="#django.template.Template" title="django.template.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> directly. If more than one
<a class="reference internal" href="../../topics/templates.html#django.template.backends.django.DjangoTemplates" title="django.template.backends.django.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a> engine is defined,
the first one will be used.</p>
<dl class="py class">
<dt class="sig sig-object py" id="django.template.Template">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">Template</span></span><a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/base.py#L141"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.Template" title="Link to this definition">¶</a></dt>
<dd><p>This class lives at <code class="docutils literal notranslate"><span class="pre">django.template.Template</span></code>. The constructor takes
one argument — the raw template code:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">django.template</span><span class="w"> </span><span class="kn">import</span> <span class="n">Template</span>

<span class="n">template</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s2">&quot;My name is {{ my_name }}.&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<div class="admonition-behind-the-scenes admonition">
<p class="admonition-title">Behind the scenes</p>
<p>The system only parses your raw template code once – when you create the
<code class="docutils literal notranslate"><span class="pre">Template</span></code> object. From then on, it’s stored internally as a tree
structure for performance.</p>
<p>Even the parsing itself is quite fast. Most of the parsing happens via a
single call to a single, short, regular expression.</p>
</div>
</section>
<section id="s-rendering-a-context">
<span id="rendering-a-context"></span><h2>Rendering a context<a class="headerlink" href="#rendering-a-context" title="Link to this heading">¶</a></h2>
<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"><span class="pre">Template</span></code></a> object, you can render a context
with it. You can reuse the same template to render it several times with
different contexts.</p>
<dl class="py class">
<dt class="sig sig-object py" id="django.template.Context">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">Context</span></span>(<em class="sig-param"><span class="n"><span class="pre">dict_</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">autoescape</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_l10n</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_tz</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/context.py#L141"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.Context" title="Link to this definition">¶</a></dt>
<dd><p>The constructor of <code class="docutils literal notranslate"><span class="pre">django.template.Context</span></code> takes an optional argument —
a dictionary mapping variable names to variable values.</p>
<p>Three optional keyword arguments can also be specified:</p>
<ul>
<li><p><code class="docutils literal notranslate"><span class="pre">autoescape</span></code> controls whether HTML autoescaping is enabled.</p>
<p>It defaults to <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Only set it to <code class="docutils literal notranslate"><span class="pre">False</span></code> if you’re rendering non-HTML templates!</p>
</div>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">use_l10n</span></code> overrides whether values will be localized by default. If
set to <code class="docutils literal notranslate"><span class="pre">True</span></code> numbers and dates will be formatted based on locale.</p>
<p>It defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<p>See <a class="reference internal" href="../../topics/i18n/formatting.html#topic-l10n-templates"><span class="std std-ref">Controlling localization in templates</span></a> for details.</p>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">use_tz</span></code> overrides whether dates are converted to the local time when
rendered in a template. If set to <code class="docutils literal notranslate"><span class="pre">True</span></code> all dates will be rendered
using the local timezone. This takes precedence over <a class="reference internal" href="../settings.html#std-setting-USE_TZ"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_TZ</span></code></a>.</p>
<p>It defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<p>See <a class="reference internal" href="../../topics/i18n/timezones.html#time-zones-in-templates"><span class="std std-ref">Time zone aware output in templates</span></a> for details.</p>
</li>
</ul>
<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>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="django.template.Template.render">
<span class="sig-prename descclassname"><span class="pre">Template.</span></span><span class="sig-name descname"><span class="pre">render</span></span>(<em class="sig-param"><span class="n"><span class="pre">context</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/base.py#L168"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.Template.render" title="Link to this definition">¶</a></dt>
<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"><span class="pre">Template</span></code></a> object’s <code class="docutils literal notranslate"><span class="pre">render()</span></code> method with a
<a class="reference internal" href="#django.template.Context" title="django.template.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code></a> to “fill” the template:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span><span class="w"> </span><span class="nn">django.template</span><span class="w"> </span><span class="kn">import</span> <span class="n">Context</span><span class="p">,</span> <span class="n">Template</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">template</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s2">&quot;My name is {{ my_name }}.&quot;</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">context</span> <span class="o">=</span> <span class="n">Context</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
<span class="go">&quot;My name is Adrian.&quot;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">context</span> <span class="o">=</span> <span class="n">Context</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
<span class="go">&quot;My name is Dolores.&quot;</span>
</pre></div>
</div>
</dd></dl>

<section id="s-variables-and-lookups">
<span id="variables-and-lookups"></span><h3>Variables and lookups<a class="headerlink" href="#variables-and-lookups" title="Link to this heading">¶</a></h3>
<p>Variable names must consist of any letter (A-Z), any digit (0-9), an underscore
(but they must not start with an underscore) or a dot.</p>
<p>Dots have a special meaning in template rendering. A dot in a variable name
signifies a <strong>lookup</strong>. Specifically, when the template system encounters a
dot in a variable name, it tries the following lookups, in this order:</p>
<ul class="simple">
<li><p>Dictionary lookup. Example: <code class="docutils literal notranslate"><span class="pre">foo[&quot;bar&quot;]</span></code></p></li>
<li><p>Attribute lookup. Example: <code class="docutils literal notranslate"><span class="pre">foo.bar</span></code></p></li>
<li><p>List-index lookup. Example: <code class="docutils literal notranslate"><span class="pre">foo[bar]</span></code></p></li>
</ul>
<p>Note that “bar” in a template expression like <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">foo.bar</span> <span class="pre">}}</span></code> will be
interpreted as a literal string and not using the value of the variable “bar”,
if one exists in the template context.</p>
<p>The template system uses the first lookup type that works. It’s short-circuit
logic. Here are a few examples:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span><span class="w"> </span><span class="nn">django.template</span><span class="w"> </span><span class="kn">import</span> <span class="n">Context</span><span class="p">,</span> <span class="n">Template</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s2">&quot;My name is {{ person.first_name }}.&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">Context</span><span class="p">(</span><span class="n">d</span><span class="p">))</span>
<span class="go">&quot;My name is Joe.&quot;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span><span class="w"> </span><span class="nc">PersonClass</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PersonClass</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">first_name</span> <span class="o">=</span> <span class="s2">&quot;Ron&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">last_name</span> <span class="o">=</span> <span class="s2">&quot;Nasty&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">Context</span><span class="p">({</span><span class="s2">&quot;person&quot;</span><span class="p">:</span> <span class="n">p</span><span class="p">}))</span>
<span class="go">&quot;My name is Ron.&quot;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s2">&quot;The first stooge in the list is {{ stooges.0 }}.&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="go">&quot;The first stooge in the list is Larry.&quot;</span>
</pre></div>
</div>
<p>If any part of the variable is callable, the template system will try calling
it. Example:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span><span class="w"> </span><span class="nc">PersonClass2</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">def</span><span class="w"> </span><span class="nf">name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="s2">&quot;Samantha&quot;</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s2">&quot;My name is {{ person.name }}.&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">Context</span><span class="p">({</span><span class="s2">&quot;person&quot;</span><span class="p">:</span> <span class="n">PersonClass2</span><span class="p">}))</span>
<span class="go">&quot;My name is Samantha.&quot;</span>
</pre></div>
</div>
<p>Callable variables are slightly more complex than variables which only require
straight lookups. Here are some things to keep in mind:</p>
<ul>
<li><p>If the variable raises an exception when called, the exception will be
propagated, unless the exception has an attribute
<code class="docutils literal notranslate"><span class="pre">silent_variable_failure</span></code> whose value is <code class="docutils literal notranslate"><span class="pre">True</span></code>. If the exception
<em>does</em> have a <code class="docutils literal notranslate"><span class="pre">silent_variable_failure</span></code> attribute whose value is
<code class="docutils literal notranslate"><span class="pre">True</span></code>, the variable will render as the value of the engine’s
<code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code> configuration option (an empty string, by default).
Example:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s2">&quot;My name is {{ person.first_name }}.&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span><span class="w"> </span><span class="nc">PersonClass3</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">def</span><span class="w"> </span><span class="nf">first_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<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>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PersonClass3</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">Context</span><span class="p">({</span><span class="s2">&quot;person&quot;</span><span class="p">:</span> <span class="n">p</span><span class="p">}))</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">foo</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span><span class="w"> </span><span class="nc">SilentAssertionError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">silent_variable_failure</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span><span class="w"> </span><span class="nc">PersonClass4</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">def</span><span class="w"> </span><span class="nf">first_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">raise</span> <span class="n">SilentAssertionError</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PersonClass4</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">Context</span><span class="p">({</span><span class="s2">&quot;person&quot;</span><span class="p">:</span> <span class="n">p</span><span class="p">}))</span>
<span class="go">&quot;My name is .&quot;</span>
</pre></div>
</div>
<p>Note that <a class="reference internal" href="../exceptions.html#django.core.exceptions.ObjectDoesNotExist" title="django.core.exceptions.ObjectDoesNotExist"><code class="xref py py-exc docutils literal notranslate"><span class="pre">django.core.exceptions.ObjectDoesNotExist</span></code></a>, which is the
base class for all Django database API <code class="docutils literal notranslate"><span class="pre">DoesNotExist</span></code> exceptions, has
<code class="docutils literal notranslate"><span class="pre">silent_variable_failure</span> <span class="pre">=</span> <span class="pre">True</span></code>. So if you’re using Django templates
with Django model objects, any <code class="docutils literal notranslate"><span class="pre">DoesNotExist</span></code> exception will fail
silently.</p>
</li>
<li><p>A variable can only be called if it has no required arguments. Otherwise,
the system will return the value of the engine’s <code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code>
option.</p></li>
</ul>
<ul id="alters-data-description">
<li><p>There can be side effects when calling some variables, and it’d be either
foolish or a security hole to allow the template system to access them.</p>
<p>A good example is the <a class="reference internal" href="../models/instances.html#django.db.models.Model.delete" title="django.db.models.Model.delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete()</span></code></a> method on
each Django model object. The template system shouldn’t be allowed to do
something like this:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>I will now delete this valuable data. <span class="cp">{{</span> <span class="nv">data.delete</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>To prevent this, set an <code class="docutils literal notranslate"><span class="pre">alters_data</span></code> attribute on the callable
variable. The template system won’t call a variable if it has
<code class="docutils literal notranslate"><span class="pre">alters_data=True</span></code> set, and will instead replace the variable with
<code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code>, unconditionally.  The
dynamically-generated <a class="reference internal" href="../models/instances.html#django.db.models.Model.delete" title="django.db.models.Model.delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete()</span></code></a> and
<a class="reference internal" href="../models/instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><code class="xref py py-meth docutils literal notranslate"><span class="pre">save()</span></code></a> methods on Django model objects get
<code class="docutils literal notranslate"><span class="pre">alters_data=True</span></code> automatically. Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">sensitive_function</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">database_record</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>


<span class="n">sensitive_function</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
</li>
<li><p>Occasionally you may want to turn off this feature for other reasons,
and tell the template system to leave a variable uncalled no matter
what.  To do so, set a <code class="docutils literal notranslate"><span class="pre">do_not_call_in_templates</span></code> attribute on the
callable with the value <code class="docutils literal notranslate"><span class="pre">True</span></code>.  The template system then will act as
if your variable is not callable (allowing you to access attributes of
the callable, for example).</p></li>
</ul>
</section>
<section id="s-how-invalid-variables-are-handled">
<span id="s-invalid-template-variables"></span><span id="how-invalid-variables-are-handled"></span><span id="invalid-template-variables"></span><h3>How invalid variables are handled<a class="headerlink" href="#how-invalid-variables-are-handled" title="Link to this heading">¶</a></h3>
<p>Generally, if a variable doesn’t exist, the template system inserts the value
of the engine’s <code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code> configuration option, which is set to
<code class="docutils literal notranslate"><span class="pre">''</span></code> (the empty string) by default.</p>
<p>Filters that are applied to an invalid variable will only be applied if
<code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code> is set to <code class="docutils literal notranslate"><span class="pre">''</span></code> (the empty string). If
<code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code> is set to any other value, variable filters will be
ignored.</p>
<p>This behavior is slightly different for the <code class="docutils literal notranslate"><span class="pre">if</span></code>, <code class="docutils literal notranslate"><span class="pre">for</span></code> and <code class="docutils literal notranslate"><span class="pre">regroup</span></code>
template tags. If an invalid variable is provided to one of these template
tags, the variable will be interpreted as <code class="docutils literal notranslate"><span class="pre">None</span></code>. Filters are always
applied to invalid variables within these template tags.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code> contains a <code class="docutils literal notranslate"><span class="pre">'%s'</span></code>, the format marker will be
replaced with the name of the invalid variable.</p>
<div class="admonition-for-debug-purposes-only admonition">
<p class="admonition-title">For debug purposes only!</p>
<p>While <code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code> can be a useful debugging tool, it is a bad
idea to turn it on as a ‘development default’.</p>
<p>Many templates, including some of Django’s, rely upon the silence of the
template system when a nonexistent variable is encountered. If you assign a
value other than <code class="docutils literal notranslate"><span class="pre">''</span></code> to <code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code>, you will experience
rendering problems with these templates and sites.</p>
<p>Generally, <code class="docutils literal notranslate"><span class="pre">string_if_invalid</span></code> should only be enabled in order to debug
a specific template problem, then cleared once debugging is complete.</p>
</div>
</section>
<section id="s-built-in-variables">
<span id="built-in-variables"></span><h3>Built-in variables<a class="headerlink" href="#built-in-variables" title="Link to this heading">¶</a></h3>
<p>Every context contains <code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code> and <code class="docutils literal notranslate"><span class="pre">None</span></code>. As you would expect,
these variables resolve to the corresponding Python objects.</p>
</section>
<section id="s-limitations-with-string-literals">
<span id="limitations-with-string-literals"></span><h3>Limitations with string literals<a class="headerlink" href="#limitations-with-string-literals" title="Link to this heading">¶</a></h3>
<p>Django’s template language has no way to escape the characters used for its own
syntax. For example, the <a class="reference internal" href="builtins.html#std-templatetag-templatetag"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">templatetag</span></code></a> tag is required if you need to
output character sequences like <code class="docutils literal notranslate"><span class="pre">{%</span></code> and <code class="docutils literal notranslate"><span class="pre">%}</span></code>.</p>
<p>A similar issue exists if you want to include these sequences in template filter
or tag arguments. For example, when parsing a block tag, Django’s template
parser looks for the first occurrence of <code class="docutils literal notranslate"><span class="pre">%}</span></code> after a <code class="docutils literal notranslate"><span class="pre">{%</span></code>. This prevents
the use of <code class="docutils literal notranslate"><span class="pre">&quot;%}&quot;</span></code> as a string literal. For example, a <code class="docutils literal notranslate"><span class="pre">TemplateSyntaxError</span></code>
will be raised for the following expressions:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><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>

<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>
</pre></div>
</div>
<p>The same issue can be triggered by using a reserved sequence in filter
arguments:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><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>
</pre></div>
</div>
<p>If you need to use strings with these sequences, store them in template
variables or use a custom template tag or filter to workaround the limitation.</p>
</section>
</section>
<section id="s-playing-with-context-objects">
<span id="s-playing-with-context"></span><span id="playing-with-context-objects"></span><span id="playing-with-context"></span><h2>Playing with <code class="docutils literal notranslate"><span class="pre">Context</span></code> objects<a class="headerlink" href="#playing-with-context-objects" title="Link to this heading">¶</a></h2>
<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"><span class="pre">Context</span></code></a> objects by passing in a
fully-populated dictionary to <code class="docutils literal notranslate"><span class="pre">Context()</span></code>. But you can add and delete items
from a <code class="docutils literal notranslate"><span class="pre">Context</span></code> object once it’s been instantiated, too, using standard
dictionary syntax:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span><span class="w"> </span><span class="nn">django.template</span><span class="w"> </span><span class="kn">import</span> <span class="n">Context</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="go">&#39;bar&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">KeyError</span>: <span class="n">&#39;foo&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;newvariable&quot;</span><span class="p">]</span>
<span class="go">&#39;hello&#39;</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="django.template.Context.get">
<span class="sig-prename descclassname"><span class="pre">Context.</span></span><span class="sig-name descname"><span class="pre">get</span></span>(<em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">otherwise</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>)<a class="headerlink" href="#django.template.Context.get" title="Link to this definition">¶</a></dt>
<dd><p>Returns the value for <code class="docutils literal notranslate"><span class="pre">key</span></code> if <code class="docutils literal notranslate"><span class="pre">key</span></code> is in the context, else returns
<code class="docutils literal notranslate"><span class="pre">otherwise</span></code>.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="django.template.Context.setdefault">
<span class="sig-prename descclassname"><span class="pre">Context.</span></span><span class="sig-name descname"><span class="pre">setdefault</span></span>(<em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>)<a class="headerlink" href="#django.template.Context.setdefault" title="Link to this definition">¶</a></dt>
<dd><p>If <code class="docutils literal notranslate"><span class="pre">key</span></code> is in the context, returns its value. Otherwise inserts <code class="docutils literal notranslate"><span class="pre">key</span></code>
with a value of <code class="docutils literal notranslate"><span class="pre">default</span></code> and returns <code class="docutils literal notranslate"><span class="pre">default</span></code>.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="django.template.Context.pop">
<span class="sig-prename descclassname"><span class="pre">Context.</span></span><span class="sig-name descname"><span class="pre">pop</span></span>()<a class="headerlink" href="#django.template.Context.pop" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="django.template.Context.push">
<span class="sig-prename descclassname"><span class="pre">Context.</span></span><span class="sig-name descname"><span class="pre">push</span></span>()<a class="headerlink" href="#django.template.Context.push" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="py exception">
<dt class="sig sig-object py" id="django.template.ContextPopException">
<em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">ContextPopException</span></span><a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/context.py#L8"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.ContextPopException" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <code class="docutils literal notranslate"><span class="pre">Context</span></code> object is a stack. That is, you can <code class="docutils literal notranslate"><span class="pre">push()</span></code> and <code class="docutils literal notranslate"><span class="pre">pop()</span></code> it.
If you <code class="docutils literal notranslate"><span class="pre">pop()</span></code> too much, it’ll raise
<code class="docutils literal notranslate"><span class="pre">django.template.ContextPopException</span></code>:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">push</span><span class="p">()</span>
<span class="go">{}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="go">&#39;second level&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="go">{&#39;foo&#39;: &#39;second level&#39;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="go">&#39;first level&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="go">&#39;overwritten&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ContextPopException</span>
</pre></div>
</div>
<p>You can also use <code class="docutils literal notranslate"><span class="pre">push()</span></code> as a context manager to ensure a matching <code class="docutils literal notranslate"><span class="pre">pop()</span></code>
is called.</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">c</span><span class="o">.</span><span class="n">push</span><span class="p">():</span>
<span class="gp">... </span>    <span class="n">c</span><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>
<span class="gp">... </span>    <span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="gp">...</span>
<span class="go">&#39;second level&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="go">&#39;first level&#39;</span>
</pre></div>
</div>
<p>All arguments passed to <code class="docutils literal notranslate"><span class="pre">push()</span></code> will be passed to the <code class="docutils literal notranslate"><span class="pre">dict</span></code> constructor
used to build the new context level.</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">c</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">foo</span><span class="o">=</span><span class="s2">&quot;second level&quot;</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="gp">...</span>
<span class="go">&#39;second level&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="go">&#39;first level&#39;</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="django.template.Context.update">
<span class="sig-prename descclassname"><span class="pre">Context.</span></span><span class="sig-name descname"><span class="pre">update</span></span>(<em class="sig-param"><span class="n"><span class="pre">other_dict</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/context.py#L170"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.Context.update" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<p>In addition to <code class="docutils literal notranslate"><span class="pre">push()</span></code> and <code class="docutils literal notranslate"><span class="pre">pop()</span></code>, the <code class="docutils literal notranslate"><span class="pre">Context</span></code>
object also defines an <code class="docutils literal notranslate"><span class="pre">update()</span></code> method. This works like <code class="docutils literal notranslate"><span class="pre">push()</span></code>
but takes a dictionary as an argument and pushes that dictionary onto
the stack instead of an empty one.</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">update</span><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>
<span class="go">{&#39;foo&#39;: &#39;updated&#39;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="go">&#39;updated&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="go">{&#39;foo&#39;: &#39;updated&#39;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="go">&#39;first level&#39;</span>
</pre></div>
</div>
<p>Like <code class="docutils literal notranslate"><span class="pre">push()</span></code>, you can use <code class="docutils literal notranslate"><span class="pre">update()</span></code> as a context manager to ensure a
matching <code class="docutils literal notranslate"><span class="pre">pop()</span></code> is called.</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">c</span><span class="o">.</span><span class="n">update</span><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>
<span class="gp">... </span>    <span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="gp">...</span>
<span class="go">&#39;second level&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">]</span>
<span class="go">&#39;first level&#39;</span>
</pre></div>
</div>
<p>Using a <code class="docutils literal notranslate"><span class="pre">Context</span></code> as a stack comes in handy in <a class="reference internal" href="../../howto/custom-template-tags.html#howto-writing-custom-template-tags"><span class="std std-ref">some custom template
tags</span></a>.</p>
<dl class="py method">
<dt class="sig sig-object py" id="django.template.Context.flatten">
<span class="sig-prename descclassname"><span class="pre">Context.</span></span><span class="sig-name descname"><span class="pre">flatten</span></span>()<a class="headerlink" href="#django.template.Context.flatten" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<p>Using <code class="docutils literal notranslate"><span class="pre">flatten()</span></code> method you can get whole <code class="docutils literal notranslate"><span class="pre">Context</span></code> stack as one dictionary
including builtin variables.</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">update</span><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="go">{&#39;bar&#39;: &#39;second level&#39;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">flatten</span><span class="p">()</span>
<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>
</pre></div>
</div>
<p>A <code class="docutils literal notranslate"><span class="pre">flatten()</span></code> method is also internally used to make <code class="docutils literal notranslate"><span class="pre">Context</span></code> objects comparable.</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c1</span> <span class="o">=</span> <span class="n">Context</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c1</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c1</span><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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c2</span> <span class="o">=</span> <span class="n">Context</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c2</span><span class="o">.</span><span class="n">update</span><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>
<span class="go">{&#39;foo&#39;: &#39;first level&#39;, &#39;bar&#39;: &#39;second level&#39;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c1</span> <span class="o">==</span> <span class="n">c2</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Result from <code class="docutils literal notranslate"><span class="pre">flatten()</span></code> can be useful in unit tests to compare <code class="docutils literal notranslate"><span class="pre">Context</span></code>
against <code class="docutils literal notranslate"><span class="pre">dict</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">ContextTest</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
    <span class="k">def</span><span class="w"> </span><span class="nf">test_against_dictionary</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">c1</span> <span class="o">=</span> <span class="n">Context</span><span class="p">()</span>
        <span class="n">c1</span><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>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span>
            <span class="n">c1</span><span class="o">.</span><span class="n">flatten</span><span class="p">(),</span>
            <span class="p">{</span>
                <span class="s2">&quot;True&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
                <span class="s2">&quot;None&quot;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                <span class="s2">&quot;False&quot;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
                <span class="s2">&quot;update&quot;</span><span class="p">:</span> <span class="s2">&quot;value&quot;</span><span class="p">,</span>
            <span class="p">},</span>
        <span class="p">)</span>
</pre></div>
</div>
<section id="s-using-requestcontext">
<span id="s-subclassing-context-requestcontext"></span><span id="using-requestcontext"></span><span id="subclassing-context-requestcontext"></span><h3>Using <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code><a class="headerlink" href="#using-requestcontext" title="Link to this heading">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="django.template.RequestContext">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">RequestContext</span></span>(<em class="sig-param"><span class="n"><span class="pre">request</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dict_</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">processors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_l10n</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_tz</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">autoescape</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/context.py#L224"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.RequestContext" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<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"><span class="pre">Context</span></code></a> class,
<code class="docutils literal notranslate"><span class="pre">django.template.RequestContext</span></code>, that acts slightly differently from the
normal <code class="docutils literal notranslate"><span class="pre">django.template.Context</span></code>. The first difference is that it takes an
<a class="reference internal" href="../request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpRequest</span></code></a> as its first argument. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">c</span> <span class="o">=</span> <span class="n">RequestContext</span><span class="p">(</span>
    <span class="n">request</span><span class="p">,</span>
    <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>
    <span class="p">},</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The second difference is that it automatically populates the context with a
few variables, according to the engine’s <code class="docutils literal notranslate"><span class="pre">context_processors</span></code> configuration
option.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">context_processors</span></code> option is a list of callables – called <strong>context
processors</strong> – that take a request object as their argument and return a
dictionary of items to be merged into the context. In the default generated
settings file, the default template engine contains the following context
processors:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="s2">&quot;django.template.context_processors.debug&quot;</span><span class="p">,</span>
    <span class="s2">&quot;django.template.context_processors.request&quot;</span><span class="p">,</span>
    <span class="s2">&quot;django.contrib.auth.context_processors.auth&quot;</span><span class="p">,</span>
    <span class="s2">&quot;django.contrib.messages.context_processors.messages&quot;</span><span class="p">,</span>
<span class="p">]</span>
</pre></div>
</div>
<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"><span class="pre">RequestContext</span></code></a> always enables
<code class="docutils literal notranslate"><span class="pre">'django.template.context_processors.csrf'</span></code>.  This is a security related
context processor required by the admin and other contrib apps, and, in case
of accidental misconfiguration, it is deliberately hardcoded in and cannot be
turned off in the <code class="docutils literal notranslate"><span class="pre">context_processors</span></code> option.</p>
<p>Each processor is applied in order. That means, if one processor adds a
variable to the context and a second processor adds a variable with the same
name, the second will override the first. The default processors are explained
below.</p>
<div class="admonition-when-context-processors-are-applied admonition">
<p class="admonition-title">When context processors are applied</p>
<p>Context processors are applied on top of context data. This means that a
context processor may overwrite variables you’ve supplied to your
<a class="reference internal" href="#django.template.Context" title="django.template.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code></a> or <a class="reference internal" href="#django.template.RequestContext" title="django.template.RequestContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">RequestContext</span></code></a>, so take care to avoid
variable names that overlap with those supplied by your context
processors.</p>
<p>If you want context data to take priority over context processors, use the
following pattern:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">django.template</span><span class="w"> </span><span class="kn">import</span> <span class="n">RequestContext</span>

<span class="n">request_context</span> <span class="o">=</span> <span class="n">RequestContext</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
<span class="n">request_context</span><span class="o">.</span><span class="n">push</span><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>
</pre></div>
</div>
<p>Django does this to allow context data to override context processors in
APIs such as <a class="reference internal" href="../../topics/http/shortcuts.html#django.shortcuts.render" title="django.shortcuts.render"><code class="xref py py-func docutils literal notranslate"><span class="pre">render()</span></code></a> and
<a class="reference internal" href="../template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateResponse</span></code></a>.</p>
</div>
<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"><span class="pre">RequestContext</span></code></a> a list of additional processors,
using the optional, third positional argument, <code class="docutils literal notranslate"><span class="pre">processors</span></code>. In this
example, the <a class="reference internal" href="#django.template.RequestContext" title="django.template.RequestContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">RequestContext</span></code></a> instance gets an <code class="docutils literal notranslate"><span class="pre">ip_address</span></code> variable:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">django.http</span><span class="w"> </span><span class="kn">import</span> <span class="n">HttpResponse</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.template</span><span class="w"> </span><span class="kn">import</span> <span class="n">RequestContext</span><span class="p">,</span> <span class="n">Template</span>


<span class="k">def</span><span class="w"> </span><span class="nf">ip_address_processor</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;ip_address&quot;</span><span class="p">:</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">[</span><span class="s2">&quot;REMOTE_ADDR&quot;</span><span class="p">]}</span>


<span class="k">def</span><span class="w"> </span><span class="nf">client_ip_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">template</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s2">&quot;{{ title }}: {{ ip_address }}&quot;</span><span class="p">)</span>
    <span class="n">context</span> <span class="o">=</span> <span class="n">RequestContext</span><span class="p">(</span>
        <span class="n">request</span><span class="p">,</span>
        <span class="p">{</span>
            <span class="s2">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Your IP Address&quot;</span><span class="p">,</span>
        <span class="p">},</span>
        <span class="p">[</span><span class="n">ip_address_processor</span><span class="p">],</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">context</span><span class="p">))</span>
</pre></div>
</div>
</section>
<section id="s-built-in-template-context-processors">
<span id="s-context-processors"></span><span id="built-in-template-context-processors"></span><span id="context-processors"></span><h3>Built-in template context processors<a class="headerlink" href="#built-in-template-context-processors" title="Link to this heading">¶</a></h3>
<p>Here’s what each of the built-in processors does:</p>
<section id="s-django-contrib-auth-context-processors-auth">
<span id="django-contrib-auth-context-processors-auth"></span><h4><code class="docutils literal notranslate"><span class="pre">django.contrib.auth.context_processors.auth</span></code><a class="headerlink" href="#django-contrib-auth-context-processors-auth" title="Link to this heading">¶</a></h4>
<dl class="py function">
<dt class="sig sig-object py" id="django.contrib.auth.context_processors.auth">
<span class="sig-name descname"><span class="pre">auth</span></span>(<em class="sig-param"><span class="n"><span class="pre">request</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/contrib/auth/context_processors.py#L49"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.contrib.auth.context_processors.auth" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<p>If this processor is enabled, every <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code> will contain these
variables:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">user</span></code> – An <code class="docutils literal notranslate"><span class="pre">auth.User</span></code> instance representing the currently
logged-in user (or an <code class="docutils literal notranslate"><span class="pre">AnonymousUser</span></code> instance, if the client isn’t
logged in).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">perms</span></code> – An instance of
<code class="docutils literal notranslate"><span class="pre">django.contrib.auth.context_processors.PermWrapper</span></code>, representing the
permissions that the currently logged-in user has.</p></li>
</ul>
</section>
<section id="s-django-template-context-processors-debug">
<span id="django-template-context-processors-debug"></span><h4><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.debug</span></code><a class="headerlink" href="#django-template-context-processors-debug" title="Link to this heading">¶</a></h4>
<dl class="py function">
<dt class="sig sig-object py" id="django.template.context_processors.debug">
<span class="sig-name descname"><span class="pre">debug</span></span>(<em class="sig-param"><span class="n"><span class="pre">request</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/context_processors.py#L37"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.context_processors.debug" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<p>If this processor is enabled, every <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code> will contain these two
variables – but only if your <a class="reference internal" href="../settings.html#std-setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> setting is set to <code class="docutils literal notranslate"><span class="pre">True</span></code> and
the request’s IP address (<code class="docutils literal notranslate"><span class="pre">request.META['REMOTE_ADDR']</span></code>) is in the
<a class="reference internal" href="../settings.html#std-setting-INTERNAL_IPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INTERNAL_IPS</span></code></a> setting:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">debug</span></code> – <code class="docutils literal notranslate"><span class="pre">True</span></code>. You can use this in templates to test whether
you’re in <a class="reference internal" href="../settings.html#std-setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> mode.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">sql_queries</span></code> – A list of <code class="docutils literal notranslate"><span class="pre">{'sql':</span> <span class="pre">...,</span> <span class="pre">'time':</span> <span class="pre">...}</span></code> dictionaries,
representing every SQL query that has happened so far during the request
and how long it took. The list is in order by database alias and then by
query. It’s lazily generated on access.</p></li>
</ul>
</section>
<section id="s-django-template-context-processors-i18n">
<span id="django-template-context-processors-i18n"></span><h4><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.i18n</span></code><a class="headerlink" href="#django-template-context-processors-i18n" title="Link to this heading">¶</a></h4>
<dl class="py function">
<dt class="sig sig-object py" id="django.template.context_processors.i18n">
<span class="sig-name descname"><span class="pre">i18n</span></span>(<em class="sig-param"><span class="n"><span class="pre">request</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/context_processors.py#L59"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.context_processors.i18n" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<p>If this processor is enabled, every <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code> will contain these
variables:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">LANGUAGES</span></code> – The value of the <a class="reference internal" href="../settings.html#std-setting-LANGUAGES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGES</span></code></a> setting.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">LANGUAGE_BIDI</span></code> – <code class="docutils literal notranslate"><span class="pre">True</span></code> if the current language is a right-to-left
language, e.g. Hebrew, Arabic. <code class="docutils literal notranslate"><span class="pre">False</span></code> if it’s a left-to-right language,
e.g. English, French, German.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">LANGUAGE_CODE</span></code> – <code class="docutils literal notranslate"><span class="pre">request.LANGUAGE_CODE</span></code>, if it exists. Otherwise,
the value of the <a class="reference internal" href="../settings.html#std-setting-LANGUAGE_CODE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGE_CODE</span></code></a> setting.</p></li>
</ul>
<p>See <a class="reference internal" href="../../topics/i18n/translation.html#i18n-template-tags"><span class="std std-ref">i18n template tags</span></a> for template tags that
generate the same values.</p>
</section>
<section id="s-django-template-context-processors-media">
<span id="django-template-context-processors-media"></span><h4><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.media</span></code><a class="headerlink" href="#django-template-context-processors-media" title="Link to this heading">¶</a></h4>
<p>If this processor is enabled, every <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code> will contain a variable
<code class="docutils literal notranslate"><span class="pre">MEDIA_URL</span></code>, providing the value of the <a class="reference internal" href="../settings.html#std-setting-MEDIA_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_URL</span></code></a> setting.</p>
</section>
<section id="s-django-template-context-processors-static">
<span id="django-template-context-processors-static"></span><h4><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.static</span></code><a class="headerlink" href="#django-template-context-processors-static" title="Link to this heading">¶</a></h4>
<dl class="py function">
<dt class="sig sig-object py" id="django.template.context_processors.static">
<span class="sig-name descname"><span class="pre">static</span></span>(<em class="sig-param"><span class="n"><span class="pre">request</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/context_processors.py#L75"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.context_processors.static" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<p>If this processor is enabled, every <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code> will contain a variable
<code class="docutils literal notranslate"><span class="pre">STATIC_URL</span></code>, providing the value of the <a class="reference internal" href="../settings.html#std-setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> setting.</p>
</section>
<section id="s-django-template-context-processors-csrf">
<span id="django-template-context-processors-csrf"></span><h4><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.csrf</span></code><a class="headerlink" href="#django-template-context-processors-csrf" title="Link to this heading">¶</a></h4>
<p>This processor adds a token that is needed by the <a class="reference internal" href="builtins.html#std-templatetag-csrf_token"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">csrf_token</span></code></a> template
tag for protection against <a class="reference internal" href="../csrf.html"><span class="doc">Cross Site Request Forgeries</span></a>.</p>
</section>
<section id="s-django-template-context-processors-request">
<span id="django-template-context-processors-request"></span><h4><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.request</span></code><a class="headerlink" href="#django-template-context-processors-request" title="Link to this heading">¶</a></h4>
<p>If this processor is enabled, every <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code> will contain a variable
<code class="docutils literal notranslate"><span class="pre">request</span></code>, which is the current <a class="reference internal" href="../request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpRequest</span></code></a>.</p>
</section>
<section id="s-django-template-context-processors-tz">
<span id="django-template-context-processors-tz"></span><h4><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.tz</span></code><a class="headerlink" href="#django-template-context-processors-tz" title="Link to this heading">¶</a></h4>
<dl class="py function">
<dt class="sig sig-object py" id="django.template.context_processors.tz">
<span class="sig-name descname"><span class="pre">tz</span></span>(<em class="sig-param"><span class="n"><span class="pre">request</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/context_processors.py#L69"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.context_processors.tz" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

<p>If this processor is enabled, every <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code> will contain a variable
<code class="docutils literal notranslate"><span class="pre">TIME_ZONE</span></code>, providing the name of the currently active time zone.</p>
</section>
<section id="s-django-contrib-messages-context-processors-messages">
<span id="django-contrib-messages-context-processors-messages"></span><h4><code class="docutils literal notranslate"><span class="pre">django.contrib.messages.context_processors.messages</span></code><a class="headerlink" href="#django-contrib-messages-context-processors-messages" title="Link to this heading">¶</a></h4>
<p>If this processor is enabled, every <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code> will contain these two
variables:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">messages</span></code> – A list of messages (as strings) that have been set
via the <a class="reference internal" href="../contrib/messages.html"><span class="doc">messages framework</span></a>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">DEFAULT_MESSAGE_LEVELS</span></code> – A mapping of the message level names to
<a class="reference internal" href="../contrib/messages.html#message-level-constants"><span class="std std-ref">their numeric value</span></a>.</p></li>
</ul>
</section>
</section>
<section id="s-writing-your-own-context-processors">
<span id="writing-your-own-context-processors"></span><h3>Writing your own context processors<a class="headerlink" href="#writing-your-own-context-processors" title="Link to this heading">¶</a></h3>
<p>A context processor has a simple interface: It’s a Python function that takes
one argument, an <a class="reference internal" href="../request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpRequest</span></code></a> object, and returns a
dictionary that gets added to the template context.</p>
<p>For example, to add the <a class="reference internal" href="../settings.html#std-setting-DEFAULT_FROM_EMAIL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEFAULT_FROM_EMAIL</span></code></a> setting to every
context:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">django.conf</span><span class="w"> </span><span class="kn">import</span> <span class="n">settings</span>


<span class="k">def</span><span class="w"> </span><span class="nf">from_email</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">{</span>
        <span class="s2">&quot;DEFAULT_FROM_EMAIL&quot;</span><span class="p">:</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_FROM_EMAIL</span><span class="p">,</span>
    <span class="p">}</span>
</pre></div>
</div>
<p>Custom context processors can live anywhere in your code base. All Django
cares about is that your custom context processors are pointed to by the
<code class="docutils literal notranslate"><span class="pre">'context_processors'</span></code> option in your <a class="reference internal" href="../settings.html#std-setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting — or the
<code class="docutils literal notranslate"><span class="pre">context_processors</span></code> argument of <a class="reference internal" href="#django.template.Engine" title="django.template.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> if you’re
using it directly.</p>
</section>
</section>
<section id="s-loading-templates">
<span id="loading-templates"></span><h2>Loading templates<a class="headerlink" href="#loading-templates" title="Link to this heading">¶</a></h2>
<p>Generally, you’ll store templates in files on your filesystem rather than
using the low-level <a class="reference internal" href="#django.template.Template" title="django.template.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> API yourself. Save
templates in a directory specified as a <strong>template directory</strong>.</p>
<p>Django searches for template directories in a number of places, depending on
your template loading settings (see “Loader types” below), but the most basic
way of specifying template directories is by using the <a class="reference internal" href="../settings.html#std-setting-TEMPLATES-DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code></a> option.</p>
<section id="s-the-dirs-option">
<span id="the-dirs-option"></span><h3>The <a class="reference internal" href="../settings.html#std-setting-TEMPLATES-DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code></a> option<a class="headerlink" href="#the-dirs-option" title="Link to this heading">¶</a></h3>
<p>Tell Django what your template directories are by using the <a class="reference internal" href="../settings.html#std-setting-TEMPLATES-DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code></a> option in the <a class="reference internal" href="../settings.html#std-setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting in your settings
file — or the <code class="docutils literal notranslate"><span class="pre">dirs</span></code> argument of <a class="reference internal" href="#django.template.Engine" title="django.template.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>. This
should be set to a list of strings that contain full paths to your template
directories:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">TEMPLATES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span>
        <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>
        <span class="s2">&quot;DIRS&quot;</span><span class="p">:</span> <span class="p">[</span>
            <span class="s2">&quot;/home/html/templates/lawrence.com&quot;</span><span class="p">,</span>
            <span class="s2">&quot;/home/html/templates/default&quot;</span><span class="p">,</span>
        <span class="p">],</span>
    <span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Your templates can go anywhere you want, as long as the directories and
templates are readable by the web server. They can have any extension you want,
such as <code class="docutils literal notranslate"><span class="pre">.html</span></code> or <code class="docutils literal notranslate"><span class="pre">.txt</span></code>, or they can have no extension at all.</p>
<p>Note that these paths should use Unix-style forward slashes, even on Windows.</p>
</section>
<section id="s-loader-types">
<span id="s-template-loaders"></span><span id="loader-types"></span><span id="template-loaders"></span><h3>Loader types<a class="headerlink" href="#loader-types" title="Link to this heading">¶</a></h3>
<p>By default, Django uses a filesystem-based template loader, but Django comes
with a few other template loaders, which know how to load templates from other
sources.</p>
<p>Some of these other loaders are disabled by default, but you can activate them
by adding a <code class="docutils literal notranslate"><span class="pre">'loaders'</span></code> option to your <code class="docutils literal notranslate"><span class="pre">DjangoTemplates</span></code> backend in the
<a class="reference internal" href="../settings.html#std-setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting or passing a <code class="docutils literal notranslate"><span class="pre">loaders</span></code> argument to
<a class="reference internal" href="#django.template.Engine" title="django.template.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>. <code class="docutils literal notranslate"><span class="pre">loaders</span></code> should be a list of strings or
tuples, where each represents a template loader class. Here are the template
loaders that come with Django:</p>
<p><code class="docutils literal notranslate"><span class="pre">django.template.loaders.filesystem.Loader</span></code></p>
<dl class="py class">
<dt class="sig sig-object py" id="django.template.loaders.filesystem.Loader">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">filesystem.</span></span><span class="sig-name descname"><span class="pre">Loader</span></span><a class="headerlink" href="#django.template.loaders.filesystem.Loader" title="Link to this definition">¶</a></dt>
<dd><p>Loads templates from the filesystem, according to
<a class="reference internal" href="../settings.html#std-setting-TEMPLATES-DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code></a>.</p>
<p>This loader is enabled by default. However it won’t find any templates
until you set <a class="reference internal" href="../settings.html#std-setting-TEMPLATES-DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code></a> to a non-empty list:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">TEMPLATES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span>
        <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>
        <span class="s2">&quot;DIRS&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">BASE_DIR</span> <span class="o">/</span> <span class="s2">&quot;templates&quot;</span><span class="p">],</span>
    <span class="p">}</span>
<span class="p">]</span>
</pre></div>
</div>
<p>You can also override <code class="docutils literal notranslate"><span class="pre">'DIRS'</span></code> and specify specific directories for a
particular filesystem loader:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">TEMPLATES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span>
        <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>
        <span class="s2">&quot;OPTIONS&quot;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s2">&quot;loaders&quot;</span><span class="p">:</span> <span class="p">[</span>
                <span class="p">(</span>
                    <span class="s2">&quot;django.template.loaders.filesystem.Loader&quot;</span><span class="p">,</span>
                    <span class="p">[</span><span class="n">BASE_DIR</span> <span class="o">/</span> <span class="s2">&quot;templates&quot;</span><span class="p">],</span>
                <span class="p">),</span>
            <span class="p">],</span>
        <span class="p">},</span>
    <span class="p">}</span>
<span class="p">]</span>
</pre></div>
</div>
</dd></dl>

<p><code class="docutils literal notranslate"><span class="pre">django.template.loaders.app_directories.Loader</span></code></p>
<dl class="py class">
<dt class="sig sig-object py" id="django.template.loaders.app_directories.Loader">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">app_directories.</span></span><span class="sig-name descname"><span class="pre">Loader</span></span><a class="headerlink" href="#django.template.loaders.app_directories.Loader" title="Link to this definition">¶</a></dt>
<dd><p>Loads templates from Django apps on the filesystem. For each app in
<a class="reference internal" href="../settings.html#std-setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a>, the loader looks for a <code class="docutils literal notranslate"><span class="pre">templates</span></code>
subdirectory. If the directory exists, Django looks for templates in there.</p>
<p>This means you can store templates with your individual apps. This also
helps to distribute Django apps with default templates.</p>
<p>For example, for this setting:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">INSTALLED_APPS</span> <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>
</pre></div>
</div>
<p>…then <code class="docutils literal notranslate"><span class="pre">get_template('foo.html')</span></code> will look for <code class="docutils literal notranslate"><span class="pre">foo.html</span></code> in these
directories, in this order:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">/path/to/myproject/polls/templates/</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">/path/to/myproject/music/templates/</span></code></p></li>
</ul>
<p>… and will use the one it finds first.</p>
<p>The order of <a class="reference internal" href="../settings.html#std-setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> is significant! For example, if you
want to customize the Django admin, you might choose to override the
standard <code class="docutils literal notranslate"><span class="pre">admin/base_site.html</span></code> template, from <code class="docutils literal notranslate"><span class="pre">django.contrib.admin</span></code>,
with your own <code class="docutils literal notranslate"><span class="pre">admin/base_site.html</span></code> in <code class="docutils literal notranslate"><span class="pre">myproject.polls</span></code>. You must
then make sure that your <code class="docutils literal notranslate"><span class="pre">myproject.polls</span></code> comes <em>before</em>
<code class="docutils literal notranslate"><span class="pre">django.contrib.admin</span></code> in <a class="reference internal" href="../settings.html#std-setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a>, otherwise
<code class="docutils literal notranslate"><span class="pre">django.contrib.admin</span></code>’s will be loaded first and yours will be ignored.</p>
<p>Note that the loader performs an optimization when it first runs:
it caches a list of which <a class="reference internal" href="../settings.html#std-setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> packages have a
<code class="docutils literal notranslate"><span class="pre">templates</span></code> subdirectory.</p>
<p>You can enable this loader by setting <a class="reference internal" href="../settings.html#std-setting-TEMPLATES-APP_DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">APP_DIRS</span></code></a> to <code class="docutils literal notranslate"><span class="pre">True</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">TEMPLATES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span>
        <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>
        <span class="s2">&quot;APP_DIRS&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
    <span class="p">}</span>
<span class="p">]</span>
</pre></div>
</div>
</dd></dl>

<p><code class="docutils literal notranslate"><span class="pre">django.template.loaders.cached.Loader</span></code></p>
<dl class="py class">
<dt class="sig sig-object py" id="django.template.loaders.cached.Loader">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">cached.</span></span><span class="sig-name descname"><span class="pre">Loader</span></span><a class="headerlink" href="#django.template.loaders.cached.Loader" title="Link to this definition">¶</a></dt>
<dd><p>While the Django template system is quite fast, if it needs to read and
compile your templates every time they’re rendered, the overhead from that
can add up.</p>
<p>You configure the cached template loader with a list of other loaders that
it should wrap. The wrapped loaders are used to locate unknown templates
when they’re first encountered. The cached loader then stores the compiled
<code class="docutils literal notranslate"><span class="pre">Template</span></code> in memory. The cached <code class="docutils literal notranslate"><span class="pre">Template</span></code> instance is returned for
subsequent requests to load the same template.</p>
<p>This loader is automatically enabled if <a class="reference internal" href="../settings.html#std-setting-TEMPLATES-OPTIONS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">OPTIONS['loaders']</span></code></a> isn’t specified.</p>
<p>You can manually specify template caching with some custom template loaders
using settings like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">TEMPLATES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span>
        <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>
        <span class="s2">&quot;DIRS&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">BASE_DIR</span> <span class="o">/</span> <span class="s2">&quot;templates&quot;</span><span class="p">],</span>
        <span class="s2">&quot;OPTIONS&quot;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s2">&quot;loaders&quot;</span><span class="p">:</span> <span class="p">[</span>
                <span class="p">(</span>
                    <span class="s2">&quot;django.template.loaders.cached.Loader&quot;</span><span class="p">,</span>
                    <span class="p">[</span>
                        <span class="s2">&quot;django.template.loaders.filesystem.Loader&quot;</span><span class="p">,</span>
                        <span class="s2">&quot;django.template.loaders.app_directories.Loader&quot;</span><span class="p">,</span>
                        <span class="s2">&quot;path.to.custom.Loader&quot;</span><span class="p">,</span>
                    <span class="p">],</span>
                <span class="p">),</span>
            <span class="p">],</span>
        <span class="p">},</span>
    <span class="p">}</span>
<span class="p">]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>All of the built-in Django template tags are safe to use with the
cached loader, but if you’re using custom template tags that come from
third party packages, or that you wrote yourself, you should ensure
that the <code class="docutils literal notranslate"><span class="pre">Node</span></code> implementation for each tag is thread-safe. For more
information, see <a class="reference internal" href="../../howto/custom-template-tags.html#template-tag-thread-safety"><span class="std std-ref">template tag thread safety considerations</span></a>.</p>
</div>
</dd></dl>

<p><code class="docutils literal notranslate"><span class="pre">django.template.loaders.locmem.Loader</span></code></p>
<dl class="py class">
<dt class="sig sig-object py" id="django.template.loaders.locmem.Loader">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">locmem.</span></span><span class="sig-name descname"><span class="pre">Loader</span></span><a class="headerlink" href="#django.template.loaders.locmem.Loader" title="Link to this definition">¶</a></dt>
<dd><p>Loads templates from a Python dictionary. This is useful for testing.</p>
<p>This loader takes a dictionary of templates as its first argument:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">TEMPLATES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span>
        <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>
        <span class="s2">&quot;OPTIONS&quot;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s2">&quot;loaders&quot;</span><span class="p">:</span> <span class="p">[</span>
                <span class="p">(</span>
                    <span class="s2">&quot;django.template.loaders.locmem.Loader&quot;</span><span class="p">,</span>
                    <span class="p">{</span>
                        <span class="s2">&quot;index.html&quot;</span><span class="p">:</span> <span class="s2">&quot;content here&quot;</span><span class="p">,</span>
                    <span class="p">},</span>
                <span class="p">),</span>
            <span class="p">],</span>
        <span class="p">},</span>
    <span class="p">}</span>
<span class="p">]</span>
</pre></div>
</div>
<p>This loader is disabled by default.</p>
</dd></dl>

<p>Django uses the template loaders in order according to the <code class="docutils literal notranslate"><span class="pre">'loaders'</span></code>
option. It uses each loader until a loader finds a match.</p>
</section>
</section>
<section id="s-custom-loaders">
<span id="s-custom-template-loaders"></span><span id="custom-loaders"></span><span id="custom-template-loaders"></span><h2>Custom loaders<a class="headerlink" href="#custom-loaders" title="Link to this heading">¶</a></h2>
<p>It’s possible to load templates from additional sources using custom template
loaders. Custom <code class="docutils literal notranslate"><span class="pre">Loader</span></code> classes should inherit from
<code class="docutils literal notranslate"><span class="pre">django.template.loaders.base.Loader</span></code> and define the <code class="docutils literal notranslate"><span class="pre">get_contents()</span></code> and
<code class="docutils literal notranslate"><span class="pre">get_template_sources()</span></code> methods.</p>
<section id="s-loader-methods">
<span id="loader-methods"></span><h3>Loader methods<a class="headerlink" href="#loader-methods" title="Link to this heading">¶</a></h3>
<dl class="py class">
<dt class="sig sig-object py" id="django.template.loaders.base.Loader">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">Loader</span></span><a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/loaders/base.py#L4"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.loaders.base.Loader" title="Link to this definition">¶</a></dt>
<dd><p>Loads templates from a given source, such as the filesystem or a database.</p>
<dl class="py method">
<dt class="sig sig-object py" id="django.template.loaders.base.Loader.get_template_sources">
<span class="sig-name descname"><span class="pre">get_template_sources</span></span>(<em class="sig-param"><span class="n"><span class="pre">template_name</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/loaders/base.py#L37"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.loaders.base.Loader.get_template_sources" title="Link to this definition">¶</a></dt>
<dd><p>A method that takes a <code class="docutils literal notranslate"><span class="pre">template_name</span></code> and yields
<a class="reference internal" href="#django.template.base.Origin" title="django.template.base.Origin"><code class="xref py py-class docutils literal notranslate"><span class="pre">Origin</span></code></a> instances for each possible
source.</p>
<p>For example, the filesystem loader may receive <code class="docutils literal notranslate"><span class="pre">'index.html'</span></code> as a
<code class="docutils literal notranslate"><span class="pre">template_name</span></code> argument.  This method would yield origins for the
full path of <code class="docutils literal notranslate"><span class="pre">index.html</span></code> as it appears in each template directory
the loader looks at.</p>
<p>The method doesn’t need to verify that the template exists at a given
path, but it should ensure the path is valid. For instance, the
filesystem loader makes sure the path lies under a valid template
directory.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="django.template.loaders.base.Loader.get_contents">
<span class="sig-name descname"><span class="pre">get_contents</span></span>(<em class="sig-param"><span class="n"><span class="pre">origin</span></span></em>)<a class="headerlink" href="#django.template.loaders.base.Loader.get_contents" title="Link to this definition">¶</a></dt>
<dd><p>Returns the contents for a template given a
<a class="reference internal" href="#django.template.base.Origin" title="django.template.base.Origin"><code class="xref py py-class docutils literal notranslate"><span class="pre">Origin</span></code></a> instance.</p>
<p>This is where a filesystem loader would read contents from the
filesystem, or a database loader would read from the database. If a
matching template doesn’t exist, this should raise a
<a class="reference internal" href="../../topics/templates.html#django.template.TemplateDoesNotExist" title="django.template.TemplateDoesNotExist"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TemplateDoesNotExist</span></code></a> error.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="django.template.loaders.base.Loader.get_template">
<span class="sig-name descname"><span class="pre">get_template</span></span>(<em class="sig-param"><span class="n"><span class="pre">template_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">skip</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/loaders/base.py#L8"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.loaders.base.Loader.get_template" title="Link to this definition">¶</a></dt>
<dd><p>Returns a <code class="docutils literal notranslate"><span class="pre">Template</span></code> object for a given <code class="docutils literal notranslate"><span class="pre">template_name</span></code> by looping
through 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"><span class="pre">get_template_sources()</span></code></a> and calling
<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"><span class="pre">get_contents()</span></code></a>. This returns the first matching template. If no
template is found, <a class="reference internal" href="../../topics/templates.html#django.template.TemplateDoesNotExist" title="django.template.TemplateDoesNotExist"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TemplateDoesNotExist</span></code></a> is
raised.</p>
<p>The optional <code class="docutils literal notranslate"><span class="pre">skip</span></code> argument is a list of origins to ignore when
extending templates. This allow templates to extend other templates of
the same name. It also used to avoid recursion errors.</p>
<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"><span class="pre">get_template_sources()</span></code></a> and
<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"><span class="pre">get_contents()</span></code></a> for custom template loaders. <code class="docutils literal notranslate"><span class="pre">get_template()</span></code>
will usually not need to be overridden.</p>
</dd></dl>

</dd></dl>

<div class="admonition-building-your-own admonition">
<p class="admonition-title">Building your own</p>
<p>For examples, read the <a class="extlink-source reference external" href="https://github.com/django/django/blob/main/django/template/loaders">source code for Django’s built-in loaders</a>.</p>
</div>
</section>
</section>
<section id="s-template-origin">
<span id="template-origin"></span><h2>Template origin<a class="headerlink" href="#template-origin" title="Link to this heading">¶</a></h2>
<p>Templates have an <code class="docutils literal notranslate"><span class="pre">origin</span></code> containing attributes depending on the source
they are loaded from.</p>
<dl class="py class">
<dt class="sig sig-object py" id="django.template.base.Origin">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">Origin</span></span>(<em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">template_name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">loader</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>)<a class="reference external" href="https://github.com/django/django/blob/stable/5.1.x/django/template/base.py#L113"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#django.template.base.Origin" title="Link to this definition">¶</a></dt>
<dd><dl class="py attribute">
<dt class="sig sig-object py" id="django.template.base.Origin.name">
<span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#django.template.base.Origin.name" title="Link to this definition">¶</a></dt>
<dd><p>The path to the template as returned by the template loader.
For loaders that read from the file system, this is the full
path to the template.</p>
<p>If the template is instantiated directly rather than through a
template loader, this is a string value of <code class="docutils literal notranslate"><span class="pre">&lt;unknown_source&gt;</span></code>.</p>
</dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="django.template.base.Origin.template_name">
<span class="sig-name descname"><span class="pre">template_name</span></span><a class="headerlink" href="#django.template.base.Origin.template_name" title="Link to this definition">¶</a></dt>
<dd><p>The relative path to the template as passed into the
template loader.</p>
<p>If the template is instantiated directly rather than through a
template loader, this is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="django.template.base.Origin.loader">
<span class="sig-name descname"><span class="pre">loader</span></span><a class="headerlink" href="#django.template.base.Origin.loader" title="Link to this definition">¶</a></dt>
<dd><p>The template loader instance that constructed this <code class="docutils literal notranslate"><span class="pre">Origin</span></code>.</p>
<p>If the template is instantiated directly rather than through a
template loader, this is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<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"><span class="pre">django.template.loaders.cached.Loader</span></code></a> requires all of its
wrapped loaders to set this attribute, typically by instantiating
the <code class="docutils literal notranslate"><span class="pre">Origin</span></code> with <code class="docutils literal notranslate"><span class="pre">loader=self</span></code>.</p>
</dd></dl>

</dd></dl>

</section>
</section>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3><a href="../../contents.html">Table of Contents</a></h3>
    <ul>
<li><a class="reference internal" href="#">The Django template language: for Python programmers</a><ul>
<li><a class="reference internal" href="#overview">Overview</a></li>
<li><a class="reference internal" href="#configuring-an-engine">Configuring an engine</a></li>
<li><a class="reference internal" href="#loading-a-template">Loading a template</a></li>
<li><a class="reference internal" href="#rendering-a-context">Rendering a context</a><ul>
<li><a class="reference internal" href="#variables-and-lookups">Variables and lookups</a></li>
<li><a class="reference internal" href="#how-invalid-variables-are-handled">How invalid variables are handled</a></li>
<li><a class="reference internal" href="#built-in-variables">Built-in variables</a></li>
<li><a class="reference internal" href="#limitations-with-string-literals">Limitations with string literals</a></li>
</ul>
</li>
<li><a class="reference internal" href="#playing-with-context-objects">Playing with <code class="docutils literal notranslate"><span class="pre">Context</span></code> objects</a><ul>
<li><a class="reference internal" href="#using-requestcontext">Using <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code></a></li>
<li><a class="reference internal" href="#built-in-template-context-processors">Built-in template context processors</a><ul>
<li><a class="reference internal" href="#django-contrib-auth-context-processors-auth"><code class="docutils literal notranslate"><span class="pre">django.contrib.auth.context_processors.auth</span></code></a></li>
<li><a class="reference internal" href="#django-template-context-processors-debug"><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.debug</span></code></a></li>
<li><a class="reference internal" href="#django-template-context-processors-i18n"><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.i18n</span></code></a></li>
<li><a class="reference internal" href="#django-template-context-processors-media"><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.media</span></code></a></li>
<li><a class="reference internal" href="#django-template-context-processors-static"><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.static</span></code></a></li>
<li><a class="reference internal" href="#django-template-context-processors-csrf"><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.csrf</span></code></a></li>
<li><a class="reference internal" href="#django-template-context-processors-request"><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.request</span></code></a></li>
<li><a class="reference internal" href="#django-template-context-processors-tz"><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.tz</span></code></a></li>
<li><a class="reference internal" href="#django-contrib-messages-context-processors-messages"><code class="docutils literal notranslate"><span class="pre">django.contrib.messages.context_processors.messages</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#writing-your-own-context-processors">Writing your own context processors</a></li>
</ul>
</li>
<li><a class="reference internal" href="#loading-templates">Loading templates</a><ul>
<li><a class="reference internal" href="#the-dirs-option">The <code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code> option</a></li>
<li><a class="reference internal" href="#loader-types">Loader types</a></li>
</ul>
</li>
<li><a class="reference internal" href="#custom-loaders">Custom loaders</a><ul>
<li><a class="reference internal" href="#loader-methods">Loader methods</a></li>
</ul>
</li>
<li><a class="reference internal" href="#template-origin">Template origin</a></li>
</ul>
</li>
</ul>

  </div>
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="builtins.html"
                          title="previous chapter">Built-in template tags and filters</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="../template-response.html"
                          title="next chapter"><code class="docutils literal notranslate"><span class="pre">TemplateResponse</span></code> and <code class="docutils literal notranslate"><span class="pre">SimpleTemplateResponse</span></code></a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/templates/api.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Dec 02, 2025</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="builtins.html" title="Built-in template tags and filters">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="../template-response.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;TemplateResponse&lt;/span&gt;&lt;/code&gt; and &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;SimpleTemplateResponse&lt;/span&gt;&lt;/code&gt;">next</a> &raquo;</div>
    </div>
  </div>

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