{"title":"Templates","version":"1.8","locale":"en","docname":"topics/templates","url":"/en/1.8/topics/templates/","canonical":"https://djangodocs.dev/en/1.8/topics/templates/","summary":"Being a web framework, Django needs a convenient way to generate HTML dynamically. The most common approach relies on templates. A template contains the static…","html":"<span id=\"templates\"></span><h1>Templates<a class=\"heading-anchor\" href=\"#module-django.template\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Being a web framework, Django needs a convenient way to generate HTML\ndynamically. The most common approach relies on templates. A template contains\nthe static parts of the desired HTML output as well as some special syntax\ndescribing how dynamic content will be inserted. For a hands-on example of\ncreating HTML pages with templates, see <a class=\"reference internal\" href=\"/en/1.8/intro/tutorial03/\"><span class=\"doc\">Tutorial 3</span></a>.</p>\n<p>A Django project can be configured with one or several template engines (or\neven zero if you don’t use templates). Django ships built-in backends for its\nown template system, creatively called the Django template language (DTL), and\nfor the popular alternative <a class=\"reference external\" href=\"http://jinja.pocoo.org/\">Jinja2</a>. Backends for other template languages may\nbe available from third-parties.</p>\n<p>Django defines a standard API for loading and rendering templates regardless\nof the backend. Loading consists of finding the template for a given identifier\nand preprocessing it, usually compiling it to an in-memory representation.\nRendering means interpolating the template with context data and returning the\nresulting string.</p>\n<p>The <a class=\"reference internal\" href=\"/en/1.8/ref/templates/language/\"><span class=\"doc\">Django template language</span></a> is Django’s own\ntemplate system. Until Django 1.8 it was the only built-in option available.\nIt’s a good template library even though it’s fairly opinionated and sports a\nfew idiosyncrasies. If you don’t have a pressing reason to choose another\nbackend, you should use the DTL, especially if you’re writing a pluggable\napplication and you intend to distribute templates. Django’s contrib apps that\ninclude templates, like <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/admin/\"><span class=\"doc\">django.contrib.admin</span></a>,\nuse the DTL.</p>\n<p>For historical reasons, both the generic support for template engines and the\nimplementation of the Django template language live in the <code class=\"docutils literal notranslate\"><span class=\"pre\">django.template</span></code>\nnamespace.</p>\n<section id=\"support-for-template-engines\">\n<span id=\"template-engines\"></span><h2>Support for template engines<a class=\"heading-anchor\" href=\"#support-for-template-engines\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<aside class=\"version-note version-added\" data-version=\"1.8\">\n<p class=\"version-note-title\">New in Django 1.8</p><p>Support for multiple template engines and the <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> setting\nwere added in Django 1.8.</p>\n</aside>\n<section id=\"configuration\">\n<h3>Configuration<a class=\"heading-anchor\" href=\"#configuration\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Templates engines are configured with the <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> setting. It’s a\nlist of configurations, one for each engine. The default value is empty. The\n<code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code> generated by the <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startproject</span></code></a> command defines a\nmore useful value:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">TEMPLATES</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;BACKEND&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;django.template.backends.django.DjangoTemplates&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;DIRS&#39;</span><span class=\"p\">:</span> <span class=\"p\">[],</span>\n        <span class=\"s1\">&#39;APP_DIRS&#39;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;OPTIONS&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"c1\"># ... some options here ...</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p><a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">BACKEND</span></code></a> is a dotted Python path to a template\nengine class implementing Django’s template backend API. The built-in backends\nare <a class=\"reference internal\" href=\"#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.template.backends.django.DjangoTemplates</span></code></a> and\n<a class=\"reference internal\" href=\"#django.template.backends.jinja2.Jinja2\" title=\"django.template.backends.jinja2.Jinja2\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.template.backends.jinja2.Jinja2</span></code></a>.</p>\n<p>Since most engines load templates from files, the top-level configuration for\neach engine contains two common settings:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DIRS</span></code></a> defines a list of directories where the\nengine should look for template source files, in search order.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APP_DIRS</span></code></a> tells whether the engine should\nlook for templates inside installed applications. Each backend defines a\nconventional name for the subdirectory inside applications where its\ntemplates should be stored.</p></li>\n</ul>\n<p>While uncommon, it’s possible to configure several instances of the same\nbackend with different options. In that case you should define a unique\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a> for each engine.</p>\n<p><a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a> contains backend-specific settings.</p>\n</section>\n<section id=\"usage\">\n<h3>Usage<a class=\"heading-anchor\" href=\"#usage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p id=\"module-django.template.loader\"><span id=\"template-loading\"></span>The <code class=\"docutils literal notranslate\"><span class=\"pre\">django.template.loader</span></code> module defines two functions to load templates.</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.template.loader.get_template\">\n<span class=\"sig-name descname\"><span class=\"pre\">get_template</span></span><span class=\"sig-paren\">(</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\">dirs</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">_dirs_undefined</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">using</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.loader.get_template\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This function loads the template with the given name and returns a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Template</span></code> object.</p>\n<p>The exact type of the return value depends on the backend that loaded the\ntemplate. Each backend has its own <code class=\"docutils literal notranslate\"><span class=\"pre\">Template</span></code> class.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">get_template()</span></code> tries each template engine in order until one succeeds.\nIf the template cannot be found, it raises\n<a class=\"reference internal\" href=\"#django.template.TemplateDoesNotExist\" title=\"django.template.TemplateDoesNotExist\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">TemplateDoesNotExist</span></code></a>. If the template is found but\ncontains invalid syntax, it raises\n<a class=\"reference internal\" href=\"#django.template.TemplateSyntaxError\" title=\"django.template.TemplateSyntaxError\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">TemplateSyntaxError</span></code></a>.</p>\n<p>How templates are searched and loaded depends on each engine’s backend and\nconfiguration.</p>\n<p>If you want to restrict the search to a particular template engine, pass\nthe engine’s <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a> in the <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.7\">\n<p class=\"version-note-title\">Changed in Django 1.7</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">dirs</span></code> parameter was added.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"1.8\">\n<p class=\"version-note-title\">Deprecated since Django 1.8</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.8: </span>The <code class=\"docutils literal notranslate\"><span class=\"pre\">dirs</span></code> parameter was deprecated.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"1.8\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> parameter was added.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"1.8\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p><code class=\"docutils literal notranslate\"><span class=\"pre\">get_template()</span></code> returns a backend-dependent <code class=\"docutils literal notranslate\"><span class=\"pre\">Template</span></code> instead\nof a <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#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>\n</aside>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.template.loader.select_template\">\n<span class=\"sig-name descname\"><span class=\"pre\">select_template</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name_list</span></span></em>, <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\">_dirs_undefined</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">using</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.loader.select_template\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><code class=\"docutils literal notranslate\"><span class=\"pre\">select_template()</span></code> is just like <code class=\"docutils literal notranslate\"><span class=\"pre\">get_template()</span></code>, except it takes a\nlist of template names. It tries each name in order and returns the first\ntemplate that exists.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.7\">\n<p class=\"version-note-title\">Changed in Django 1.7</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">dirs</span></code> parameter was added.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"1.8\">\n<p class=\"version-note-title\">Deprecated since Django 1.8</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.8: </span>The <code class=\"docutils literal notranslate\"><span class=\"pre\">dirs</span></code> parameter was deprecated.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"1.8\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> parameter was added.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"1.8\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p><code class=\"docutils literal notranslate\"><span class=\"pre\">select_template()</span></code> returns a backend-dependent <code class=\"docutils literal notranslate\"><span class=\"pre\">Template</span></code> instead\nof a <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#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>\n</aside>\n</dd></dl>\n\n<p>If loading a template fails, the following two exceptions, defined in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.template</span></code>, may be raised:</p>\n<dl class=\"py exception\">\n<dt class=\"sig sig-object py\" id=\"django.template.TemplateDoesNotExist\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">exception</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">TemplateDoesNotExist</span></span><a class=\"heading-anchor\" href=\"#django.template.TemplateDoesNotExist\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This exception is raised when a template cannot be found.</p>\n</dd></dl>\n\n<dl class=\"py exception\">\n<dt class=\"sig sig-object py\" id=\"django.template.TemplateSyntaxError\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">exception</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">TemplateSyntaxError</span></span><a class=\"heading-anchor\" href=\"#django.template.TemplateSyntaxError\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This exception is raised when a template was found but contains errors.</p>\n</dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">Template</span></code> objects returned by <code class=\"docutils literal notranslate\"><span class=\"pre\">get_template()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">select_template()</span></code>\nmust provide a <code class=\"docutils literal notranslate\"><span class=\"pre\">render()</span></code> method with the following signature:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.template.backends.base.Template.render\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Template.</span></span><span class=\"sig-name descname\"><span class=\"pre\">render</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">context</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\">request</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.backends.base.Template.render\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Renders this template with a given context.</p>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">context</span></code> is provided, it must be 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>. If it isn’t\nprovided, the engine will render the template with an empty context.</p>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">request</span></code> is provided, it must be an <a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a>.\nThen the engine must make it, as well as the CSRF token, available in the\ntemplate. How this is achieved is up to each backend.</p>\n</dd></dl>\n\n<p>Here’s an example of the search algorithm. For this example the\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> setting is:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">TEMPLATES</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;BACKEND&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;django.template.backends.django.DjangoTemplates&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;DIRS&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n            <span class=\"s1\">&#39;/home/html/example.com&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;/home/html/default&#39;</span><span class=\"p\">,</span>\n        <span class=\"p\">],</span>\n    <span class=\"p\">},</span>\n    <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;BACKEND&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;django.template.backends.jinja2.Jinja2&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;DIRS&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n            <span class=\"s1\">&#39;/home/html/jinja2&#39;</span><span class=\"p\">,</span>\n        <span class=\"p\">],</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>If you call <code class=\"docutils literal notranslate\"><span class=\"pre\">get_template('story_detail.html')</span></code>, here are the files Django\nwill look for, in order:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/example.com/story_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'django'</span></code> engine)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/default/story_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'django'</span></code> engine)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/jinja2/story_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'jinja2'</span></code> engine)</p></li>\n</ul>\n<p>If you call <code class=\"docutils literal notranslate\"><span class=\"pre\">select_template(['story_253_detail.html',</span> <span class=\"pre\">'story_detail.html'])</span></code>,\nhere’s what Django will look for:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/example.com/story_253_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'django'</span></code> engine)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/default/story_253_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'django'</span></code> engine)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/jinja2/story_253_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'jinja2'</span></code> engine)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/example.com/story_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'django'</span></code> engine)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/default/story_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'django'</span></code> engine)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/jinja2/story_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'jinja2'</span></code> engine)</p></li>\n</ul>\n<p>When Django finds a template that exists, it stops looking.</p>\n<aside class=\"admonition-tip admonition\">\n<p class=\"admonition-title\">Tip</p>\n<p>You can use <a class=\"reference internal\" href=\"#django.template.loader.select_template\" title=\"django.template.loader.select_template\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">select_template()</span></code></a> for flexible\ntemplate loading. For example, if you’ve written a news story and want\nsome stories to have custom templates, use something like\n<code class=\"docutils literal notranslate\"><span class=\"pre\">select_template(['story_%s_detail.html'</span> <span class=\"pre\">%</span> <span class=\"pre\">story.id,</span>\n<span class=\"pre\">'story_detail.html'])</span></code>. That’ll allow you to use a custom template for an\nindividual story, with a fallback template for stories that don’t have\ncustom templates.</p>\n</aside>\n<p>It’s possible – and preferable – to organize templates in subdirectories\ninside each directory containing templates. The convention is to make a\nsubdirectory for each Django app, with subdirectories within those\nsubdirectories as needed.</p>\n<p>Do this for your own sanity. Storing all templates in the root level of a\nsingle directory gets messy.</p>\n<p>To load a template that’s within a subdirectory, just use a slash, like so:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">get_template</span><span class=\"p\">(</span><span class=\"s1\">&#39;news/story_detail.html&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Using the same <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> option as above, this will attempt to load\nthe following templates:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/example.com/news/story_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'django'</span></code> engine)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/default/news/story_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'django'</span></code> engine)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/home/html/jinja2/news/story_detail.html</span></code> (<code class=\"docutils literal notranslate\"><span class=\"pre\">'jinja2'</span></code> engine)</p></li>\n</ul>\n<p>In addition, to cut down on the repetitive nature of loading and rendering\ntemplates, Django provides a shortcut function which automates the process.</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.template.loader.render_to_string\">\n<span class=\"sig-name descname\"><span class=\"pre\">render_to_string</span></span><span class=\"sig-paren\">(</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\">context</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\">context_instance</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">_context_instance_undefined</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</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\">using</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.template.loader.render_to_string\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><code class=\"docutils literal notranslate\"><span class=\"pre\">render_to_string()</span></code> loads a template like <a class=\"reference internal\" href=\"#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> and\ncalls its <code class=\"docutils literal notranslate\"><span class=\"pre\">render()</span></code> method immediately. It takes the following\narguments.</p>\n<dl>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code></dt><dd><p>The name of the template to load and render. If it’s a list of template\nnames, Django uses <a class=\"reference internal\" href=\"#django.template.loader.select_template\" title=\"django.template.loader.select_template\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">select_template()</span></code></a> instead of\n<a class=\"reference internal\" href=\"#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> to find the template.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">context</span></code></dt><dd><p>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> to be used as the template’s context for rendering.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.8\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">context</span></code> argument used to be called <code class=\"docutils literal notranslate\"><span class=\"pre\">dictionary</span></code>. That name\nis deprecated in Django 1.8 and will be removed in Django 1.10.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">context</span></code> is now optional. An empty context will be used if it\nisn’t provided.</p>\n</aside>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">context_instance</span></code></dt><dd><p>An instance of <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#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 subclass (e.g., an\ninstance of <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RequestContext</span></code></a>) to use as the\ntemplate’s context.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"1.8\">\n<p class=\"version-note-title\">Deprecated since Django 1.8</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.8: </span>The <code class=\"docutils literal notranslate\"><span class=\"pre\">context_instance</span></code> argument is deprecated. Use <code class=\"docutils literal notranslate\"><span class=\"pre\">context</span></code> and\nif needed <code class=\"docutils literal notranslate\"><span class=\"pre\">request</span></code>.</p>\n</aside>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">request</span></code></dt><dd><p>An optional <a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> that will be available\nduring the template’s rendering process.</p>\n<aside class=\"version-note version-added\" data-version=\"1.8\">\n<p class=\"version-note-title\">New in Django 1.8</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">request</span></code> argument was added.</p>\n</aside>\n</dd>\n</dl>\n</dd></dl>\n\n<p>See also the <a class=\"reference internal\" href=\"/en/1.8/topics/http/shortcuts/#django.shortcuts.render\" title=\"django.shortcuts.render\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">render()</span></code></a> and\n<a class=\"reference internal\" href=\"/en/1.8/topics/http/shortcuts/#django.shortcuts.render_to_response\" title=\"django.shortcuts.render_to_response\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">render_to_response()</span></code></a> shortcuts, which call\n<a class=\"reference internal\" href=\"#django.template.loader.render_to_string\" title=\"django.template.loader.render_to_string\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">render_to_string()</span></code></a> and feed the result into an\n<a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> suitable for returning from a view.</p>\n<p>Finally, you can use configured engines directly:</p>\n<dl class=\"py data\">\n<dt class=\"sig sig-object py\" id=\"django.template.loader.engines\">\n<span class=\"sig-name descname\"><span class=\"pre\">engines</span></span><a class=\"heading-anchor\" href=\"#django.template.loader.engines\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Template engines are available in <code class=\"docutils literal notranslate\"><span class=\"pre\">django.template.engines</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">engines</span>\n\n<span class=\"n\">django_engine</span> <span class=\"o\">=</span> <span class=\"n\">engines</span><span class=\"p\">[</span><span class=\"s1\">&#39;django&#39;</span><span class=\"p\">]</span>\n<span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">django_engine</span><span class=\"o\">.</span><span class=\"n\">from_string</span><span class=\"p\">(</span><span class=\"s2\">&quot;Hello {{ name }}!&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>The lookup key — <code class=\"docutils literal notranslate\"><span class=\"pre\">'django'</span></code> in this example — is the engine’s\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a>.</p>\n</dd></dl>\n\n</section>\n<section id=\"module-django.template.backends.django\">\n<span id=\"built-in-backends\"></span><span id=\"module-django.template.backends\"></span><h3>Built-in backends<a class=\"heading-anchor\" href=\"#module-django.template.backends.django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.template.backends.django.DjangoTemplates\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">DjangoTemplates</span></span><a class=\"heading-anchor\" href=\"#django.template.backends.django.DjangoTemplates\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Set <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">BACKEND</span></code></a> to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'django.template.backends.django.DjangoTemplates'</span></code> to configure a Django\ntemplate engine.</p>\n<p>When <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APP_DIRS</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">DjangoTemplates</span></code>\nengines look for templates in the <code class=\"docutils literal notranslate\"><span class=\"pre\">templates</span></code> subdirectory of installed\napplications. This generic name was kept for backwards-compatibility.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">DjangoTemplates</span></code> engines accept the following <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a>:</p>\n<ul>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'allowed_include_roots'</span></code>: a list of strings representing allowed prefixes\nfor the <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">ssi</span> <span class=\"pre\">%}</span></code> template tag. This is a security measure, so that\ntemplate authors can’t access files that they shouldn’t be accessing.</p>\n<p>For example, if <code class=\"docutils literal notranslate\"><span class=\"pre\">'allowed_include_roots'</span></code> is <code class=\"docutils literal notranslate\"><span class=\"pre\">['/home/html',</span>\n<span class=\"pre\">'/var/www']</span></code>, then <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">ssi</span> <span class=\"pre\">/home/html/foo.txt</span> <span class=\"pre\">%}</span></code> would work, but <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span>\n<span class=\"pre\">ssi</span> <span class=\"pre\">/etc/passwd</span> <span class=\"pre\">%}</span></code> wouldn’t.</p>\n<p>It defaults to an empty list.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"1.8\">\n<p class=\"version-note-title\">Deprecated since Django 1.8</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.8: </span><code class=\"docutils literal notranslate\"><span class=\"pre\">allowed_include_roots</span></code> is deprecated because the {% ssi %} tag is\ndeprecated.</p>\n</aside>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'context_processors'</span></code>: a list of dotted Python paths to callables that\nare used to populate the context when a template is rendered with a request.\nThese callables take a request object as their argument and return a\n<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>\n<p>It defaults to an empty list.</p>\n<p>See <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#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>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'debug'</span></code>: a boolean that turns on/off template debug mode. If it is\n<code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, the fancy error page will display a detailed report for any\nexception raised during template rendering. This report contains the\nrelevant snippet of the template with the appropriate line highlighted.</p>\n<p>It defaults to the value of the <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> setting.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'loaders'</span></code>: a list of dotted Python paths to template loader classes.\nEach <code class=\"docutils literal notranslate\"><span class=\"pre\">Loader</span></code> class knows how to import templates from a particular\nsource. Optionally, a tuple can be used instead of a string. The first item\nin the tuple should be the <code class=\"docutils literal notranslate\"><span class=\"pre\">Loader</span></code> class name, and subsequent items are\npassed to the <code class=\"docutils literal notranslate\"><span class=\"pre\">Loader</span></code> during initialization.</p>\n<p>The default depends on the values of <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DIRS</span></code></a> and\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APP_DIRS</span></code></a>.</p>\n<p>See <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#template-loaders\"><span class=\"std std-ref\">Loader types</span></a> for details.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'string_if_invalid'</span></code>: the output, as a string, that the template system\nshould use for invalid (e.g. misspelled) variables.</p>\n<p>It defaults to an empty string.</p>\n<p>See <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#invalid-template-variables\"><span class=\"std std-ref\">How invalid variables are handled</span></a> for details.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'file_charset'</span></code>: the charset used to read template files on disk.</p>\n<p>It defaults to the value of <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-FILE_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FILE_CHARSET</span></code></a>.</p>\n</li>\n</ul>\n<dl class=\"py class\" id=\"module-django.template.backends.jinja2\">\n<dt class=\"sig sig-object py\" id=\"django.template.backends.jinja2.Jinja2\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">Jinja2</span></span><a class=\"heading-anchor\" href=\"#django.template.backends.jinja2.Jinja2\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Requires <a class=\"reference external\" href=\"http://jinja.pocoo.org/\">Jinja2</a> to be installed:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>Jinja2\n</code></pre></div>\n<p>Set <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">BACKEND</span></code></a> to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'django.template.backends.jinja2.Jinja2'</span></code> to configure a <a class=\"reference external\" href=\"http://jinja.pocoo.org/\">Jinja2</a> engine.</p>\n<p>When <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APP_DIRS</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">Jinja2</span></code> engines\nlook for templates in the <code class=\"docutils literal notranslate\"><span class=\"pre\">jinja2</span></code> subdirectory of installed applications.</p>\n<p>The most important entry in <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a> is\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'environment'</span></code>. It’s a dotted Python path to a callable returning a Jinja2\nenvironment. It defaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">'jinja2.Environment'</span></code>. Django invokes that\ncallable and passes other options as keyword arguments. Furthermore, Django\nadds defaults that differ from Jinja2’s for a few options:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'autoescape'</span></code>: <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'loader'</span></code>: a loader configured for <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DIRS</span></code></a> and\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APP_DIRS</span></code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'auto_reload'</span></code>: <code class=\"docutils literal notranslate\"><span class=\"pre\">settings.DEBUG</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'undefined'</span></code>: <code class=\"docutils literal notranslate\"><span class=\"pre\">DebugUndefined</span> <span class=\"pre\">if</span> <span class=\"pre\">settings.DEBUG</span> <span class=\"pre\">else</span> <span class=\"pre\">Undefined</span></code></p></li>\n</ul>\n<p>The default configuration is purposefully kept to a minimum. The <code class=\"docutils literal notranslate\"><span class=\"pre\">Jinja2</span></code>\nbackend doesn’t create a Django-flavored environment. It doesn’t know about\nDjango context processors, filters, and tags. In order to use Django-specific\nAPIs, you must configure them into the environment.</p>\n<p>For example, you can create <code class=\"docutils literal notranslate\"><span class=\"pre\">myproject/jinja2.py</span></code> with this content:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">__future__</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">absolute_import</span>  <span class=\"c1\"># Python 2 only</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.staticfiles.storage</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">staticfiles_storage</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.urlresolvers</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">reverse</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">jinja2</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Environment</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">environment</span><span class=\"p\">(</span><span class=\"o\">**</span><span class=\"n\">options</span><span class=\"p\">):</span>\n    <span class=\"n\">env</span> <span class=\"o\">=</span> <span class=\"n\">Environment</span><span class=\"p\">(</span><span class=\"o\">**</span><span class=\"n\">options</span><span class=\"p\">)</span>\n    <span class=\"n\">env</span><span class=\"o\">.</span><span class=\"n\">globals</span><span class=\"o\">.</span><span class=\"n\">update</span><span class=\"p\">({</span>\n        <span class=\"s1\">&#39;static&#39;</span><span class=\"p\">:</span> <span class=\"n\">staticfiles_storage</span><span class=\"o\">.</span><span class=\"n\">url</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;url&#39;</span><span class=\"p\">:</span> <span class=\"n\">reverse</span><span class=\"p\">,</span>\n    <span class=\"p\">})</span>\n    <span class=\"k\">return</span> <span class=\"n\">env</span>\n</code></pre></div>\n<p>and set the <code class=\"docutils literal notranslate\"><span class=\"pre\">'environment'</span></code> option to <code class=\"docutils literal notranslate\"><span class=\"pre\">'myproject.jinja2.environment'</span></code>.</p>\n<p>Then you could use the following constructs in Jinja2 templates:</p>\n<div class=\"code-block\" data-language=\"html+jinja\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Html Jinja</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Html Jinja code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">img</span> <span class=\"na\">src</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">static</span><span class=\"o\">(</span><span class=\"s1\">&#39;path/to/company-logo.png&#39;</span><span class=\"o\">)</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span> <span class=\"na\">alt</span><span class=\"o\">=</span><span class=\"s\">&quot;Company Logo&quot;</span><span class=\"p\">&gt;</span>\n\n<span class=\"p\">&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">url</span><span class=\"o\">(</span><span class=\"s1\">&#39;admin:index&#39;</span><span class=\"o\">)</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>Administration<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>The concepts of tags and filters exist both in the Django template language\nand in Jinja2 but they’re used differently. Since Jinja2 supports passing\narguments to callables in templates, many features that require a template tag\nor filter in Django templates can be achieved simply by calling a function in\nJinja2 templates, as shown in the example above. Jinja2’s global namespace\nremoves the need for template context processors. The Django template language\ndoesn’t have an equivalent of Jinja2 tests.</p>\n</section>\n<section id=\"custom-backends\">\n<h3>Custom backends<a class=\"heading-anchor\" href=\"#custom-backends\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Here’s how to implement a custom template backend in order to use another\ntemplate system. A template backend is a class that inherits\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.template.backends.base.BaseEngine</span></code>. It must implement\n<code class=\"docutils literal notranslate\"><span class=\"pre\">get_template()</span></code> and optionally <code class=\"docutils literal notranslate\"><span class=\"pre\">from_string()</span></code>. Here’s an example for a\nfictional <code class=\"docutils literal notranslate\"><span class=\"pre\">foobar</span></code> template library:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">TemplateDoesNotExist</span><span class=\"p\">,</span> <span class=\"n\">TemplateSyntaxError</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template.backends.base</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">BaseEngine</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template.backends.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">csrf_input_lazy</span><span class=\"p\">,</span> <span class=\"n\">csrf_token_lazy</span>\n\n<span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">foobar</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">FooBar</span><span class=\"p\">(</span><span class=\"n\">BaseEngine</span><span class=\"p\">):</span>\n\n    <span class=\"c1\"># Name of the subdirectory containing the templates for this engine</span>\n    <span class=\"c1\"># inside an installed application.</span>\n    <span class=\"n\">app_dirname</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;foobar&#39;</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">params</span><span class=\"p\">):</span>\n        <span class=\"n\">params</span> <span class=\"o\">=</span> <span class=\"n\">params</span><span class=\"o\">.</span><span class=\"n\">copy</span><span class=\"p\">()</span>\n        <span class=\"n\">options</span> <span class=\"o\">=</span> <span class=\"n\">params</span><span class=\"o\">.</span><span class=\"n\">pop</span><span class=\"p\">(</span><span class=\"s1\">&#39;OPTIONS&#39;</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">copy</span><span class=\"p\">()</span>\n        <span class=\"nb\">super</span><span class=\"p\">(</span><span class=\"n\">FooBar</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"n\">params</span><span class=\"p\">)</span>\n\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">engine</span> <span class=\"o\">=</span> <span class=\"n\">foobar</span><span class=\"o\">.</span><span class=\"n\">Engine</span><span class=\"p\">(</span><span class=\"o\">**</span><span class=\"n\">options</span><span class=\"p\">)</span>\n\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">from_string</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">template_code</span><span class=\"p\">):</span>\n        <span class=\"k\">try</span><span class=\"p\">:</span>\n          <span class=\"k\">return</span> <span class=\"n\">Template</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">engine</span><span class=\"o\">.</span><span class=\"n\">from_string</span><span class=\"p\">(</span><span class=\"n\">template_code</span><span class=\"p\">))</span>\n        <span class=\"k\">except</span> <span class=\"n\">foobar</span><span class=\"o\">.</span><span class=\"n\">TemplateCompilationFailed</span> <span class=\"k\">as</span> <span class=\"n\">exc</span><span class=\"p\">:</span>\n            <span class=\"k\">raise</span> <span class=\"n\">TemplateSyntaxError</span><span class=\"p\">(</span><span class=\"n\">exc</span><span class=\"o\">.</span><span class=\"n\">args</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">get_template</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">template_name</span><span class=\"p\">):</span>\n        <span class=\"k\">try</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"n\">Template</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">engine</span><span class=\"o\">.</span><span class=\"n\">get_template</span><span class=\"p\">(</span><span class=\"n\">template_name</span><span class=\"p\">))</span>\n        <span class=\"k\">except</span> <span class=\"n\">foobar</span><span class=\"o\">.</span><span class=\"n\">TemplateNotFound</span> <span class=\"k\">as</span> <span class=\"n\">exc</span><span class=\"p\">:</span>\n            <span class=\"k\">raise</span> <span class=\"n\">TemplateDoesNotExist</span><span class=\"p\">(</span><span class=\"n\">exc</span><span class=\"o\">.</span><span class=\"n\">args</span><span class=\"p\">)</span>\n        <span class=\"k\">except</span> <span class=\"n\">foobar</span><span class=\"o\">.</span><span class=\"n\">TemplateCompilationFailed</span> <span class=\"k\">as</span> <span class=\"n\">exc</span><span class=\"p\">:</span>\n            <span class=\"k\">raise</span> <span class=\"n\">TemplateSyntaxError</span><span class=\"p\">(</span><span class=\"n\">exc</span><span class=\"o\">.</span><span class=\"n\">args</span><span class=\"p\">)</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Template</span><span class=\"p\">(</span><span class=\"nb\">object</span><span class=\"p\">):</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">template</span><span class=\"p\">):</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">template</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">render</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">context</span><span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"n\">request</span><span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        <span class=\"k\">if</span> <span class=\"n\">context</span> <span class=\"ow\">is</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n            <span class=\"n\">context</span> <span class=\"o\">=</span> <span class=\"p\">{}</span>\n        <span class=\"k\">if</span> <span class=\"n\">request</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n            <span class=\"n\">context</span><span class=\"p\">[</span><span class=\"s1\">&#39;request&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">request</span>\n            <span class=\"n\">context</span><span class=\"p\">[</span><span class=\"s1\">&#39;csrf_input&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">csrf_input_lazy</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">)</span>\n            <span class=\"n\">context</span><span class=\"p\">[</span><span class=\"s1\">&#39;csrf_token&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">csrf_token_lazy</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</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>\n</code></pre></div>\n<p>See <a class=\"reference external\" href=\"https://github.com/django/deps/blob/master/final/0182-multiple-template-engines.rst\">DEP 182</a> for more information.</p>\n</section>\n</section>\n<section id=\"the-django-template-language\">\n<span id=\"template-language-intro\"></span><h2>The Django template language<a class=\"heading-anchor\" href=\"#the-django-template-language\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"syntax\">\n<h3>Syntax<a class=\"heading-anchor\" href=\"#syntax\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"admonition-about-this-section admonition\">\n<p class=\"admonition-title\">About this section</p>\n<p>This is an overview of the Django template language’s syntax. For details\nsee the <a class=\"reference internal\" href=\"/en/1.8/ref/templates/language/\"><span class=\"doc\">language syntax reference</span></a>.</p>\n</aside>\n<p>A Django template is simply a text document or a Python string marked-up using\nthe Django template language. Some constructs are recognized and interpreted\nby the template engine. The main ones are variables and tags.</p>\n<p>A template is rendered with a context. Rendering replaces variables with their\nvalues, which are looked up in the context, and executes tags. Everything else\nis output as is.</p>\n<p>The syntax of the Django template language involves four constructs.</p>\n<section id=\"variables\">\n<h4>Variables<a class=\"heading-anchor\" href=\"#variables\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>A variable outputs a value from the context, which is a dict-like object\nmapping keys to values.</p>\n<p>Variables are surrounded by <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">}}</span></code> like this:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code>My first name is <span class=\"cp\">{{</span> <span class=\"nv\">first_name</span> <span class=\"cp\">}}</span>. My last name is <span class=\"cp\">{{</span> <span class=\"nv\">last_name</span> <span class=\"cp\">}}</span>.\n</code></pre></div>\n<p>With a context of <code class=\"docutils literal notranslate\"><span class=\"pre\">{'first_name':</span> <span class=\"pre\">'John',</span> <span class=\"pre\">'last_name':</span> <span class=\"pre\">'Doe'}</span></code>, this\ntemplate renders to:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code>My first name is John. My last name is Doe.\n</code></pre></div>\n<p>Dictionary lookup, attribute lookup and list-index lookups are implemented\nwith a dot notation:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">my_dict.key</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{{</span> <span class=\"nv\">my_object.attribute</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{{</span> <span class=\"nv\">my_list.0</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If a variable resolves to a callable, the template system will call it with no\narguments and use its result instead of the callable.</p>\n</section>\n<section id=\"tags\">\n<h4>Tags<a class=\"heading-anchor\" href=\"#tags\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Tags provide arbitrary logic in the rendering process.</p>\n<p>This definition is deliberately vague. For example, a tag can output content,\nserve as a control structure e.g. an “if” statement or a “for” loop, grab\ncontent from a database, or even enable access to other template tags.</p>\n<p>Tags are surrounded by <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">%}</span></code> like this:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">csrf_token</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Most tags accept arguments:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">cycle</span> <span class=\"s1\">&#39;odd&#39;</span> <span class=\"s1\">&#39;even&#39;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Some tags require beginning and ending tags:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">user.is_authenticated</span> <span class=\"cp\">%}</span>Hello, <span class=\"cp\">{{</span> <span class=\"nv\">user.username</span> <span class=\"cp\">}}</span>.<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>A <a class=\"reference internal\" href=\"/en/1.8/ref/templates/builtins/#ref-templates-builtins-tags\"><span class=\"std std-ref\">reference of built-in tags</span></a> is\navailable as well as <a class=\"reference internal\" href=\"/en/1.8/howto/custom-template-tags/#howto-writing-custom-template-tags\"><span class=\"std std-ref\">instructions for writing custom tags</span></a>.</p>\n</section>\n<section id=\"filters\">\n<h4>Filters<a class=\"heading-anchor\" href=\"#filters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Filters transform the values of variables and tag arguments.</p>\n<p>They look like this:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">django</span><span class=\"o\">|</span><span class=\"nf\">title</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>With a context of <code class=\"docutils literal notranslate\"><span class=\"pre\">{'django':</span> <span class=\"pre\">'the</span> <span class=\"pre\">web</span> <span class=\"pre\">framework</span> <span class=\"pre\">for</span> <span class=\"pre\">perfectionists</span> <span class=\"pre\">with</span>\n<span class=\"pre\">deadlines'}</span></code>, this template renders to:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code>The Web Framework For Perfectionists With Deadlines\n</code></pre></div>\n<p>Some filters take an argument:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">my_date</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;Y-m-d&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>A <a class=\"reference internal\" href=\"/en/1.8/ref/templates/builtins/#ref-templates-builtins-filters\"><span class=\"std std-ref\">reference of built-in filters</span></a> is\navailable as well as <a class=\"reference internal\" href=\"/en/1.8/howto/custom-template-tags/#howto-writing-custom-template-filters\"><span class=\"std std-ref\">instructions for writing custom filters</span></a>.</p>\n</section>\n<section id=\"comments\">\n<h4>Comments<a class=\"heading-anchor\" href=\"#comments\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Comments look like this:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"c\">{# this won&#39;t be rendered #}</span>\n</code></pre></div>\n<p>A <a class=\"reference internal\" href=\"/en/1.8/ref/templates/builtins/#std-templatetag-comment\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">comment</span> <span class=\"pre\">%}</span></code></a> tag provides multi-line comments.</p>\n</section>\n</section>\n<section id=\"components\">\n<h3>Components<a class=\"heading-anchor\" href=\"#components\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"admonition-about-this-section admonition\">\n<p class=\"admonition-title\">About this section</p>\n<p>This is an overview of the Django template language’s APIs. For details\nsee the <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/\"><span class=\"doc\">API reference</span></a>.</p>\n</aside>\n<section id=\"engine\">\n<h4>Engine<a class=\"heading-anchor\" href=\"#engine\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.template.Engine</span></code></a> encapsulates an instance of the Django\ntemplate system. The main reason for instantiating an\n<a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Engine</span></code></a> directly is to use the Django template\nlanguage outside of a Django project.</p>\n<p><a class=\"reference internal\" href=\"#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.template.backends.django.DjangoTemplates</span></code></a> is a thin wrapper\nadapting <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.template.Engine</span></code></a> to Django’s template backend API.</p>\n</section>\n<section id=\"template\">\n<h4>Template<a class=\"heading-anchor\" href=\"#template\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#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> represents a compiled template.\nTemplates are obtained with <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.Engine.get_template\" title=\"django.template.Engine.get_template\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Engine.get_template()</span></code></a> or <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.Engine.from_string\" title=\"django.template.Engine.from_string\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Engine.from_string()</span></code></a></p>\n<p>Likewise <code class=\"docutils literal notranslate\"><span class=\"pre\">django.template.backends.django.Template</span></code> is a thin wrapper\nadapting <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#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> to the common template API.</p>\n</section>\n<section id=\"context\">\n<h4>Context<a class=\"heading-anchor\" href=\"#context\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.Context\" title=\"django.template.Context\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.template.Context</span></code></a> holds some metadata in addition to the\ncontext data. It is passed to <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.Template.render\" title=\"django.template.Template.render\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Template.render()</span></code></a> for rendering a template.</p>\n<p><a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.template.RequestContext</span></code></a> is a subclass of\n<a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.Context\" title=\"django.template.Context\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Context</span></code></a> that stores the current\n<a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> and runs template context processors.</p>\n<p>The common API doesn’t have an equivalent concept. Context data is passed in a\nplain <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> and the current <a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> is passed\nseparately if needed.</p>\n</section>\n<section id=\"loaders\">\n<h4>Loaders<a class=\"heading-anchor\" href=\"#loaders\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Template loaders are responsible for locating templates, loading them, and\nreturning <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Template</span></code></a> objects.</p>\n<p>Django provides several <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#template-loaders\"><span class=\"std std-ref\">built-in template loaders</span></a>\nand supports <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#custom-template-loaders\"><span class=\"std std-ref\">custom template loaders</span></a>.</p>\n</section>\n<section id=\"context-processors\">\n<h4>Context processors<a class=\"heading-anchor\" href=\"#context-processors\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Context processors are functions that receive the current\n<a class=\"reference internal\" href=\"/en/1.8/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> as an 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\ndata to be added to the rendering context.</p>\n<p>Their main use is to add common data shared by all templates to the context\nwithout repeating code in every view.</p>\n<p>Django provides many <a class=\"reference internal\" href=\"/en/1.8/ref/templates/api/#context-processors\"><span class=\"std std-ref\">built-in context processors</span></a>.\nImplementing a custom context processor is as simple as defining a function.</p>\n</section>\n</section>\n</section>","rootId":"module-django.template","toc":[{"title":"Support for template engines","anchor":"support-for-template-engines","children":[{"title":"Configuration","anchor":"configuration","children":[]},{"title":"Usage","anchor":"usage","children":[]},{"title":"Built-in backends","anchor":"module-django.template.backends.django","children":[]},{"title":"Custom backends","anchor":"custom-backends","children":[]}]},{"title":"The Django template language","anchor":"the-django-template-language","children":[{"title":"Syntax","anchor":"syntax","children":[{"title":"Variables","anchor":"variables","children":[]},{"title":"Tags","anchor":"tags","children":[]},{"title":"Filters","anchor":"filters","children":[]},{"title":"Comments","anchor":"comments","children":[]}]},{"title":"Components","anchor":"components","children":[{"title":"Engine","anchor":"engine","children":[]},{"title":"Template","anchor":"template","children":[]},{"title":"Context","anchor":"context","children":[]},{"title":"Loaders","anchor":"loaders","children":[]},{"title":"Context processors","anchor":"context-processors","children":[]}]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/en/1.8/topics/"}],"prev":{"docname":"topics/forms/media","title":"Form Assets (the Media class)","url":"/en/1.8/topics/forms/media/"},"next":{"docname":"topics/class-based-views/index","title":"Class-based views","url":"/en/1.8/topics/class-based-views/"},"formats":{"html":"/en/1.8/topics/templates/","markdown":"/en/1.8/topics/templates.md","json":"/en/1.8/topics/templates.json"},"source":"https://github.com/django/django/blob/stable/1.8.x/docs/topics/templates.txt","official":"https://docs.djangoproject.com/en/1.8/topics/templates/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en"]}