{"title":"Built-in template tags and filters","version":"2.0","locale":"zh-hans","docname":"ref/templates/builtins","url":"/zh-hans/2.0/ref/templates/builtins/","canonical":"https://djangodocs.dev/zh-hans/2.0/ref/templates/builtins/","summary":"This document describes Django's built-in template tags and filters. It is recommended that you use the automatic documentation , if available, as this will also…","html":"<h1>Built-in template tags and filters<a class=\"heading-anchor\" href=\"#built-in-template-tags-and-filters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document describes Django's built-in template tags and filters. It is\nrecommended that you use the <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/contrib/admin/admindocs/\"><span class=\"doc\">automatic documentation</span></a>, if available, as this will also include\ndocumentation for any custom tags or filters installed.</p>\n<section id=\"built-in-tag-reference\">\n<span id=\"ref-templates-builtins-tags\"></span><h2>Built-in tag reference<a class=\"heading-anchor\" href=\"#built-in-tag-reference\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"autoescape\">\n<span id=\"std-templatetag-autoescape\"></span><h3><code class=\"docutils literal notranslate\">autoescape</code><a class=\"heading-anchor\" href=\"#autoescape\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Controls the current auto-escaping behavior. This tag takes either <code class=\"docutils literal notranslate\">on</code> or\n<code class=\"docutils literal notranslate\">off</code> as an argument and that determines whether auto-escaping is in effect\ninside the block. The block is closed with an <code class=\"docutils literal notranslate\">endautoescape</code> ending tag.</p>\n<p>When auto-escaping is in effect, all variable content has HTML escaping applied\nto it before placing the result into the output (but after any filters have\nbeen applied). This is equivalent to manually applying the <a class=\"reference internal\" href=\"#std-templatefilter-escape\"><code class=\"xref std std-tfilter docutils literal notranslate\">escape</code></a>\nfilter to each variable.</p>\n<p>The only exceptions are variables that are already marked as &quot;safe&quot; from\nescaping, either by the code that populated the variable, or because it has had\nthe <a class=\"reference internal\" href=\"#std-templatefilter-safe\"><code class=\"xref std std-tfilter docutils literal notranslate\">safe</code></a> or <a class=\"reference internal\" href=\"#std-templatefilter-escape\"><code class=\"xref std std-tfilter docutils literal notranslate\">escape</code></a> filters applied.</p>\n<p>Sample usage:</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\">autoescape</span> <span class=\"nv\">on</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">body</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endautoescape</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"block\">\n<span id=\"std-templatetag-block\"></span><h3><code class=\"docutils literal notranslate\">block</code><a class=\"heading-anchor\" href=\"#block\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Defines a block that can be overridden by child templates. See\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/templates/language/#template-inheritance\"><span class=\"std std-ref\">Template inheritance</span></a> for more information.</p>\n</section>\n<section id=\"comment\">\n<span id=\"std-templatetag-comment\"></span><h3><code class=\"docutils literal notranslate\">comment</code><a class=\"heading-anchor\" href=\"#comment\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Ignores everything between <code class=\"docutils literal notranslate\">{% comment %}</code> and <code class=\"docutils literal notranslate\">{% endcomment %}</code>.\nAn optional note may be inserted in the first tag. For example, this is\nuseful when commenting out code for documenting why the code was disabled.</p>\n<p>Sample usage:</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=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>Rendered text with <span class=\"cp\">{{</span> <span class=\"nv\">pub_date</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;c&quot;</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">comment</span> <span class=\"s2\">&quot;Optional note&quot;</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>Commented out text with <span class=\"cp\">{{</span> <span class=\"nv\">create_date</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;c&quot;</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endcomment</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">comment</code> tags cannot be nested.</p>\n</section>\n<section id=\"csrf-token\">\n<span id=\"std-templatetag-csrf_token\"></span><h3><code class=\"docutils literal notranslate\">csrf_token</code><a class=\"heading-anchor\" href=\"#csrf-token\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>This tag is used for CSRF protection, as described in the documentation for\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/csrf/\"><span class=\"doc\">Cross Site Request Forgeries</span></a>.</p>\n</section>\n<section id=\"cycle\">\n<span id=\"std-templatetag-cycle\"></span><h3><code class=\"docutils literal notranslate\">cycle</code><a class=\"heading-anchor\" href=\"#cycle\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Produces one of its arguments each time this tag is encountered. The first\nargument is produced on the first encounter, the second argument on the second\nencounter, and so forth. Once all arguments are exhausted, the tag cycles to\nthe first argument and produces it again.</p>\n<p>This tag is particularly useful in a loop:</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\">for</span> <span class=\"nv\">o</span> <span class=\"k\">in</span> <span class=\"nv\">some_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">tr</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">cycle</span> <span class=\"s1\">&#39;row1&#39;</span> <span class=\"s1\">&#39;row2&#39;</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n        ...\n    <span class=\"p\">&lt;/</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The first iteration produces HTML that refers to class <code class=\"docutils literal notranslate\">row1</code>, the second to\n<code class=\"docutils literal notranslate\">row2</code>, the third to <code class=\"docutils literal notranslate\">row1</code> again, and so on for each iteration of the\nloop.</p>\n<p>You can use variables, too. For example, if you have two template variables,\n<code class=\"docutils literal notranslate\">rowvalue1</code> and <code class=\"docutils literal notranslate\">rowvalue2</code>, you can alternate between their values like\nthis:</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\">for</span> <span class=\"nv\">o</span> <span class=\"k\">in</span> <span class=\"nv\">some_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">tr</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">cycle</span> <span class=\"nv\">rowvalue1</span> <span class=\"nv\">rowvalue2</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n        ...\n    <span class=\"p\">&lt;/</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Variables included in the cycle will be escaped.  You can disable auto-escaping\nwith:</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\">for</span> <span class=\"nv\">o</span> <span class=\"k\">in</span> <span class=\"nv\">some_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">tr</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">autoescape</span> <span class=\"nv\">off</span> <span class=\"cp\">%}{%</span> <span class=\"k\">cycle</span> <span class=\"nv\">rowvalue1</span> <span class=\"nv\">rowvalue2</span> <span class=\"cp\">%}{%</span> <span class=\"k\">endautoescape</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n        ...\n    <span class=\"p\">&lt;/</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>You can mix variables and strings:</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\">for</span> <span class=\"nv\">o</span> <span class=\"k\">in</span> <span class=\"nv\">some_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">tr</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">cycle</span> <span class=\"s1\">&#39;row1&#39;</span> <span class=\"nv\">rowvalue2</span> <span class=\"s1\">&#39;row3&#39;</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n        ...\n    <span class=\"p\">&lt;/</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>In some cases you might want to refer to the current value of a cycle\nwithout advancing to the next value. To do this,\njust give the <code class=\"docutils literal notranslate\">{% cycle %}</code> tag a name, using &quot;as&quot;, 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\">cycle</span> <span class=\"s1\">&#39;row1&#39;</span> <span class=\"s1\">&#39;row2&#39;</span> <span class=\"k\">as</span> <span class=\"nv\">rowcolors</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>From then on, you can insert the current value of the cycle wherever you'd like\nin your template by referencing the cycle name as a context variable. If you\nwant to move the cycle to the next value independently of the original\n<code class=\"docutils literal notranslate\">cycle</code> tag, you can use another <code class=\"docutils literal notranslate\">cycle</code> tag and specify the name of the\nvariable. So, the following template:</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=\"p\">&lt;</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">td</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">cycle</span> <span class=\"s1\">&#39;row1&#39;</span> <span class=\"s1\">&#39;row2&#39;</span> <span class=\"k\">as</span> <span class=\"nv\">rowcolors</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>...<span class=\"p\">&lt;/</span><span class=\"nt\">td</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">td</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">rowcolors</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>...<span class=\"p\">&lt;/</span><span class=\"nt\">td</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">td</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">cycle</span> <span class=\"nv\">rowcolors</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>...<span class=\"p\">&lt;/</span><span class=\"nt\">td</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">td</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">rowcolors</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>...<span class=\"p\">&lt;/</span><span class=\"nt\">td</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>would output:</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=\"p\">&lt;</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">td</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;row1&quot;</span><span class=\"p\">&gt;</span>...<span class=\"p\">&lt;/</span><span class=\"nt\">td</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">td</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;row1&quot;</span><span class=\"p\">&gt;</span>...<span class=\"p\">&lt;/</span><span class=\"nt\">td</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">td</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;row2&quot;</span><span class=\"p\">&gt;</span>...<span class=\"p\">&lt;/</span><span class=\"nt\">td</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">td</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;row2&quot;</span><span class=\"p\">&gt;</span>...<span class=\"p\">&lt;/</span><span class=\"nt\">td</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>You can use any number of values in a <code class=\"docutils literal notranslate\">cycle</code> tag, separated by spaces.\nValues enclosed in single quotes (<code class=\"docutils literal notranslate\">'</code>) or double quotes (<code class=\"docutils literal notranslate\">&quot;</code>) are treated\nas string literals, while values without quotes are treated as template\nvariables.</p>\n<p>By default, when you use the <code class=\"docutils literal notranslate\">as</code> keyword with the cycle tag, the\nusage of <code class=\"docutils literal notranslate\">{% cycle %}</code> that initiates the cycle will itself produce\nthe first value in the cycle. This could be a problem if you want to\nuse the value in a nested loop or an included template. If you only want\nto declare the cycle but not produce the first value, you can add a\n<code class=\"docutils literal notranslate\">silent</code> keyword as the last keyword in the tag. For example:</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\">for</span> <span class=\"nv\">obj</span> <span class=\"k\">in</span> <span class=\"nv\">some_list</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">cycle</span> <span class=\"s1\">&#39;row1&#39;</span> <span class=\"s1\">&#39;row2&#39;</span> <span class=\"k\">as</span> <span class=\"nv\">rowcolors</span> <span class=\"nv\">silent</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">tr</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">rowcolors</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{%</span> <span class=\"k\">include</span> <span class=\"s2\">&quot;subtemplate.html&quot;</span> <span class=\"cp\">%}</span><span class=\"p\">&lt;/</span><span class=\"nt\">tr</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>This will output a list of <code class=\"docutils literal notranslate\">&lt;tr&gt;</code> elements with <code class=\"docutils literal notranslate\">class</code>\nalternating between <code class=\"docutils literal notranslate\">row1</code> and <code class=\"docutils literal notranslate\">row2</code>. The subtemplate will have\naccess to <code class=\"docutils literal notranslate\">rowcolors</code> in its context and the value will match the class\nof the <code class=\"docutils literal notranslate\">&lt;tr&gt;</code> that encloses it. If the <code class=\"docutils literal notranslate\">silent</code> keyword were to be\nomitted, <code class=\"docutils literal notranslate\">row1</code> and <code class=\"docutils literal notranslate\">row2</code> would be emitted as normal text, outside the\n<code class=\"docutils literal notranslate\">&lt;tr&gt;</code> element.</p>\n<p>When the silent keyword is used on a cycle definition, the silence\nautomatically applies to all subsequent uses of that specific cycle tag.\nThe following template would output <em>nothing</em>, even though the second\ncall to <code class=\"docutils literal notranslate\">{% cycle %}</code> doesn't specify <code class=\"docutils literal notranslate\">silent</code>:</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;row1&#39;</span> <span class=\"s1\">&#39;row2&#39;</span> <span class=\"k\">as</span> <span class=\"nv\">rowcolors</span> <span class=\"nv\">silent</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">cycle</span> <span class=\"nv\">rowcolors</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>You can use the <a class=\"reference internal\" href=\"#std-templatetag-resetcycle\"><code class=\"xref std std-ttag docutils literal notranslate\">resetcycle</code></a> tag to make a <code class=\"docutils literal notranslate\">{% cycle %}</code> tag restart\nfrom its first value when it's next encountered.</p>\n</section>\n<section id=\"debug\">\n<span id=\"std-templatetag-debug\"></span><h3><code class=\"docutils literal notranslate\">debug</code><a class=\"heading-anchor\" href=\"#debug\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Outputs a whole load of debugging information, including the current context\nand imported modules.</p>\n</section>\n<section id=\"extends\">\n<span id=\"std-templatetag-extends\"></span><h3><code class=\"docutils literal notranslate\">extends</code><a class=\"heading-anchor\" href=\"#extends\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Signals that this template extends a parent template.</p>\n<p>This tag can be used in two ways:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">{% extends &quot;base.html&quot; %}</code> (with quotes) uses the literal value\n<code class=\"docutils literal notranslate\">&quot;base.html&quot;</code> as the name of the parent template to extend.</p></li>\n<li><p><code class=\"docutils literal notranslate\">{% extends variable %}</code> uses the value of <code class=\"docutils literal notranslate\">variable</code>. If the variable\nevaluates to a string, Django will use that string as the name of the\nparent template. If the variable evaluates to a <code class=\"docutils literal notranslate\">Template</code> object,\nDjango will use that object as the parent template.</p></li>\n</ul>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/templates/language/#template-inheritance\"><span class=\"std std-ref\">Template inheritance</span></a> for more information.</p>\n<p>Normally the template name is relative to the template loader's root directory.\nA string argument may also be a relative path starting with <code class=\"docutils literal notranslate\">./</code> or <code class=\"docutils literal notranslate\">../</code>.\nFor example, assume the following directory structure:</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>dir1/\n    template.html\n    base2.html\n    my/\n        base3.html\nbase1.html\n</code></pre></div>\n<p>In <code class=\"docutils literal notranslate\">template.html</code>, the following paths would be valid:</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\">extends</span> <span class=\"s2\">&quot;./base2.html&quot;</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">extends</span> <span class=\"s2\">&quot;../base1.html&quot;</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">extends</span> <span class=\"s2\">&quot;./my/base3.html&quot;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"filter\">\n<span id=\"std-templatetag-filter\"></span><h3><code class=\"docutils literal notranslate\">filter</code><a class=\"heading-anchor\" href=\"#filter\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Filters the contents of the block through one or more filters. Multiple\nfilters can be specified with pipes and filters can have arguments, just as\nin variable syntax.</p>\n<p>Note that the block includes <em>all</em> the text between the <code class=\"docutils literal notranslate\">filter</code> and\n<code class=\"docutils literal notranslate\">endfilter</code> tags.</p>\n<p>Sample usage:</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\">filter</span> <span class=\"nf\">force_escape</span><span class=\"o\">|</span><span class=\"nf\">lower</span> <span class=\"cp\">%}</span>\n    This text will be HTML-escaped, and will appear in all lowercase.\n<span class=\"cp\">{%</span> <span class=\"k\">endfilter</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The <a class=\"reference internal\" href=\"#std-templatefilter-escape\"><code class=\"xref std std-tfilter docutils literal notranslate\">escape</code></a> and <a class=\"reference internal\" href=\"#std-templatefilter-safe\"><code class=\"xref std std-tfilter docutils literal notranslate\">safe</code></a> filters are not acceptable\narguments. Instead, use the <a class=\"reference internal\" href=\"#std-templatetag-autoescape\"><code class=\"xref std std-ttag docutils literal notranslate\">autoescape</code></a> tag to manage autoescaping\nfor blocks of template code.</p>\n</aside>\n</section>\n<section id=\"firstof\">\n<span id=\"std-templatetag-firstof\"></span><h3><code class=\"docutils literal notranslate\">firstof</code><a class=\"heading-anchor\" href=\"#firstof\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Outputs the first argument variable that is not <code class=\"docutils literal notranslate\">False</code>. Outputs nothing if\nall the passed variables are <code class=\"docutils literal notranslate\">False</code>.</p>\n<p>Sample usage:</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\">firstof</span> <span class=\"nv\">var1</span> <span class=\"nv\">var2</span> <span class=\"nv\">var3</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>This is equivalent 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><span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">var1</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">var1</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">elif</span> <span class=\"nv\">var2</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">var2</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">elif</span> <span class=\"nv\">var3</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">var3</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>You can also use a literal string as a fallback value in case all\npassed variables are False:</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\">firstof</span> <span class=\"nv\">var1</span> <span class=\"nv\">var2</span> <span class=\"nv\">var3</span> <span class=\"s2\">&quot;fallback value&quot;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>This tag auto-escapes variable values. You can disable auto-escaping with:</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\">autoescape</span> <span class=\"nv\">off</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">firstof</span> <span class=\"nv\">var1</span> <span class=\"nv\">var2</span> <span class=\"nv\">var3</span> <span class=\"s2\">&quot;&lt;strong&gt;fallback value&lt;/strong&gt;&quot;</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endautoescape</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Or if only some variables should be escaped, you can use:</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\">firstof</span> <span class=\"nv\">var1</span> <span class=\"nv\">var2</span><span class=\"o\">|</span><span class=\"nf\">safe</span> <span class=\"nv\">var3</span> <span class=\"s2\">&quot;&lt;strong&gt;fallback value&lt;/strong&gt;&quot;</span><span class=\"o\">|</span><span class=\"nf\">safe</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>You can use the syntax <code class=\"docutils literal notranslate\">{% firstof var1 var2 var3 as value %}</code> to store the\noutput inside a variable.</p>\n</section>\n<section id=\"for\">\n<span id=\"std-templatetag-for\"></span><h3><code class=\"docutils literal notranslate\">for</code><a class=\"heading-anchor\" href=\"#for\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Loops over each item in an array, making the item available in a context\nvariable. For example, to display a list of athletes provided in\n<code class=\"docutils literal notranslate\">athlete_list</code>:</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=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">athlete</span> <span class=\"k\">in</span> <span class=\"nv\">athlete_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">athlete.name</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>You can loop over a list in reverse by using\n<code class=\"docutils literal notranslate\">{% for obj in list reversed %}</code>.</p>\n<p>If you need to loop over a list of lists, you can unpack the values\nin each sublist into individual variables. For example, if your context\ncontains a list of (x,y) coordinates called <code class=\"docutils literal notranslate\">points</code>, you could use the\nfollowing to output the list of points:</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\">for</span> <span class=\"nv\">x</span><span class=\"o\">,</span> <span class=\"nv\">y</span> <span class=\"k\">in</span> <span class=\"nv\">points</span> <span class=\"cp\">%}</span>\n    There is a point at <span class=\"cp\">{{</span> <span class=\"nv\">x</span> <span class=\"cp\">}}</span>,<span class=\"cp\">{{</span> <span class=\"nv\">y</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>This can also be useful if you need to access the items in a dictionary.\nFor example, if your context contained a dictionary <code class=\"docutils literal notranslate\">data</code>, the following\nwould display the keys and values of the dictionary:</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\">for</span> <span class=\"nv\">key</span><span class=\"o\">,</span> <span class=\"nv\">value</span> <span class=\"k\">in</span> <span class=\"nv\">data.items</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">key</span> <span class=\"cp\">}}</span>: <span class=\"cp\">{{</span> <span class=\"nv\">value</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Keep in mind that for the dot operator, dictionary key lookup takes precedence\nover method lookup. Therefore if the <code class=\"docutils literal notranslate\">data</code> dictionary contains a key named\n<code class=\"docutils literal notranslate\">'items'</code>, <code class=\"docutils literal notranslate\">data.items</code> will return <code class=\"docutils literal notranslate\">data['items']</code> instead of\n<code class=\"docutils literal notranslate\">data.items()</code>. Avoid adding keys that are named like dictionary methods if\nyou want to use those methods in a template (<code class=\"docutils literal notranslate\">items</code>, <code class=\"docutils literal notranslate\">values</code>, <code class=\"docutils literal notranslate\">keys</code>,\netc.). Read more about the lookup order of the dot operator in the\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/templates/language/#template-variables\"><span class=\"std std-ref\">documentation of template variables</span></a>.</p>\n<p>The for loop sets a number of variables available within the loop:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Variable</p></th>\n<th class=\"head\"><p>Description</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">forloop.counter</code></p></td>\n<td><p>The current iteration of the loop (1-indexed)</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">forloop.counter0</code></p></td>\n<td><p>The current iteration of the loop (0-indexed)</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">forloop.revcounter</code></p></td>\n<td><p>The number of iterations from the end of the\nloop (1-indexed)</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">forloop.revcounter0</code></p></td>\n<td><p>The number of iterations from the end of the\nloop (0-indexed)</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">forloop.first</code></p></td>\n<td><p>True if this is the first time through the loop</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">forloop.last</code></p></td>\n<td><p>True if this is the last time through the loop</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">forloop.parentloop</code></p></td>\n<td><p>For nested loops, this is the loop surrounding\nthe current one</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n</section>\n<section id=\"for-empty\">\n<h3><code class=\"docutils literal notranslate\">for</code> ... <code class=\"docutils literal notranslate\">empty</code><a class=\"heading-anchor\" href=\"#for-empty\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The <code class=\"docutils literal notranslate\">for</code> tag can take an optional <code class=\"docutils literal notranslate\">{% empty %}</code> clause whose text is\ndisplayed if the given array is empty or could not be found:</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=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">athlete</span> <span class=\"k\">in</span> <span class=\"nv\">athlete_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">athlete.name</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">empty</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>Sorry, no athletes in this list.<span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>The above is equivalent to -- but shorter, cleaner, and possibly faster\nthan -- the following:</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=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n  <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">athlete_list</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">athlete</span> <span class=\"k\">in</span> <span class=\"nv\">athlete_list</span> <span class=\"cp\">%}</span>\n      <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">athlete.name</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n  <span class=\"cp\">{%</span> <span class=\"k\">else</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>Sorry, no athletes in this list.<span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n  <span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"if\">\n<span id=\"std-templatetag-if\"></span><h3><code class=\"docutils literal notranslate\">if</code><a class=\"heading-anchor\" href=\"#if\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The <code class=\"docutils literal notranslate\">{% if %}</code> tag evaluates a variable, and if that variable is &quot;true&quot; (i.e.\nexists, is not empty, and is not a false boolean value) the contents of the\nblock are output:</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\">athlete_list</span> <span class=\"cp\">%}</span>\n    Number of athletes: <span class=\"cp\">{{</span> <span class=\"nv\">athlete_list</span><span class=\"o\">|</span><span class=\"nf\">length</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">elif</span> <span class=\"nv\">athlete_in_locker_room_list</span> <span class=\"cp\">%}</span>\n    Athletes should be out of the locker room soon!\n<span class=\"cp\">{%</span> <span class=\"k\">else</span> <span class=\"cp\">%}</span>\n    No athletes.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>In the above, if <code class=\"docutils literal notranslate\">athlete_list</code> is not empty, the number of athletes will be\ndisplayed by the <code class=\"docutils literal notranslate\">{{ athlete_list|length }}</code> variable.</p>\n<p>As you can see, the <code class=\"docutils literal notranslate\">if</code> tag may take one or several <code class=\"docutils literal notranslate\">{% elif %}</code>\nclauses, as well as an <code class=\"docutils literal notranslate\">{% else %}</code> clause that will be displayed if all\nprevious conditions fail. These clauses are optional.</p>\n<section id=\"boolean-operators\">\n<h4>Boolean operators<a class=\"heading-anchor\" href=\"#boolean-operators\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference internal\" href=\"#std-templatetag-if\"><code class=\"xref std std-ttag docutils literal notranslate\">if</code></a> tags may use <code class=\"docutils literal notranslate\">and</code>, <code class=\"docutils literal notranslate\">or</code> or <code class=\"docutils literal notranslate\">not</code> to test a number of\nvariables or to negate a given variable:</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\">athlete_list</span> <span class=\"k\">and</span> <span class=\"nv\">coach_list</span> <span class=\"cp\">%}</span>\n    Both athletes and coaches are available.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"k\">not</span> <span class=\"nv\">athlete_list</span> <span class=\"cp\">%}</span>\n    There are no athletes.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">athlete_list</span> <span class=\"k\">or</span> <span class=\"nv\">coach_list</span> <span class=\"cp\">%}</span>\n    There are some athletes or some coaches.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"k\">not</span> <span class=\"nv\">athlete_list</span> <span class=\"k\">or</span> <span class=\"nv\">coach_list</span> <span class=\"cp\">%}</span>\n    There are no athletes or there are some coaches.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">athlete_list</span> <span class=\"k\">and</span> <span class=\"k\">not</span> <span class=\"nv\">coach_list</span> <span class=\"cp\">%}</span>\n    There are some athletes and absolutely no coaches.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Use of both <code class=\"docutils literal notranslate\">and</code> and <code class=\"docutils literal notranslate\">or</code> clauses within the same tag is allowed, with\n<code class=\"docutils literal notranslate\">and</code> having higher precedence than <code class=\"docutils literal notranslate\">or</code> e.g.:</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\">athlete_list</span> <span class=\"k\">and</span> <span class=\"nv\">coach_list</span> <span class=\"k\">or</span> <span class=\"nv\">cheerleader_list</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>will be interpreted like:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">if</span> <span class=\"p\">(</span>athlete_list <span class=\"ow\">and</span> coach_list<span class=\"p\">)</span> <span class=\"ow\">or</span> cheerleader_list\n</code></pre></div>\n<p>Use of actual parentheses in the <a class=\"reference internal\" href=\"#std-templatetag-if\"><code class=\"xref std std-ttag docutils literal notranslate\">if</code></a> tag is invalid syntax. If you need\nthem to indicate precedence, you should use nested <a class=\"reference internal\" href=\"#std-templatetag-if\"><code class=\"xref std std-ttag docutils literal notranslate\">if</code></a> tags.</p>\n<p><a class=\"reference internal\" href=\"#std-templatetag-if\"><code class=\"xref std std-ttag docutils literal notranslate\">if</code></a> tags may also use the operators <code class=\"docutils literal notranslate\">==</code>, <code class=\"docutils literal notranslate\">!=</code>, <code class=\"docutils literal notranslate\">&lt;</code>, <code class=\"docutils literal notranslate\">&gt;</code>,\n<code class=\"docutils literal notranslate\">&lt;=</code>, <code class=\"docutils literal notranslate\">&gt;=</code>, <code class=\"docutils literal notranslate\">in</code>, <code class=\"docutils literal notranslate\">not in</code>, <code class=\"docutils literal notranslate\">is</code>, and <code class=\"docutils literal notranslate\">is not</code> which work as\nfollows:</p>\n<section id=\"operator\">\n<h5><code class=\"docutils literal notranslate\">==</code> operator<a class=\"heading-anchor\" href=\"#operator\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>Equality. Example:</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\">somevar</span> <span class=\"o\">==</span> <span class=\"s2\">&quot;x&quot;</span> <span class=\"cp\">%}</span>\n  This appears if variable somevar equals the string &quot;x&quot;\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"id1\">\n<h5><code class=\"docutils literal notranslate\">!=</code> operator<a class=\"heading-anchor\" href=\"#id1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>Inequality. Example:</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\">somevar</span> <span class=\"o\">!=</span> <span class=\"s2\">&quot;x&quot;</span> <span class=\"cp\">%}</span>\n  This appears if variable somevar does not equal the string &quot;x&quot;,\n  or if somevar is not found in the context\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"id2\">\n<h5><code class=\"docutils literal notranslate\">&lt;</code> operator<a class=\"heading-anchor\" href=\"#id2\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>Less than. Example:</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\">somevar</span> <span class=\"o\">&lt;</span> <span class=\"m\">100</span> <span class=\"cp\">%}</span>\n  This appears if variable somevar is less than 100.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"id3\">\n<h5><code class=\"docutils literal notranslate\">&gt;</code> operator<a class=\"heading-anchor\" href=\"#id3\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>Greater than. Example:</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\">somevar</span> <span class=\"o\">&gt;</span> <span class=\"m\">0</span> <span class=\"cp\">%}</span>\n  This appears if variable somevar is greater than 0.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"id4\">\n<h5><code class=\"docutils literal notranslate\">&lt;=</code> operator<a class=\"heading-anchor\" href=\"#id4\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>Less than or equal to. Example:</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\">somevar</span> <span class=\"o\">&lt;=</span> <span class=\"m\">100</span> <span class=\"cp\">%}</span>\n  This appears if variable somevar is less than 100 or equal to 100.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"id5\">\n<h5><code class=\"docutils literal notranslate\">&gt;=</code> operator<a class=\"heading-anchor\" href=\"#id5\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>Greater than or equal to. Example:</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\">somevar</span> <span class=\"o\">&gt;=</span> <span class=\"m\">1</span> <span class=\"cp\">%}</span>\n  This appears if variable somevar is greater than 1 or equal to 1.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"in-operator\">\n<h5><code class=\"docutils literal notranslate\">in</code> operator<a class=\"heading-anchor\" href=\"#in-operator\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>Contained within. This operator is supported by many Python containers to test\nwhether the given value is in the container. The following are some examples\nof how <code class=\"docutils literal notranslate\">x in y</code> will be interpreted:</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=\"s2\">&quot;bc&quot;</span> <span class=\"k\">in</span> <span class=\"s2\">&quot;abcdef&quot;</span> <span class=\"cp\">%}</span>\n  This appears since &quot;bc&quot; is a substring of &quot;abcdef&quot;\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"s2\">&quot;hello&quot;</span> <span class=\"k\">in</span> <span class=\"nv\">greetings</span> <span class=\"cp\">%}</span>\n  If greetings is a list or set, one element of which is the string\n  &quot;hello&quot;, this will appear.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">user</span> <span class=\"k\">in</span> <span class=\"nv\">users</span> <span class=\"cp\">%}</span>\n  If users is a QuerySet, this will appear if user is an\n  instance that belongs to the QuerySet.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"not-in-operator\">\n<h5><code class=\"docutils literal notranslate\">not in</code> operator<a class=\"heading-anchor\" href=\"#not-in-operator\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>Not contained within. This is the negation of the <code class=\"docutils literal notranslate\">in</code> operator.</p>\n</section>\n<section id=\"is-operator\">\n<h5><code class=\"docutils literal notranslate\">is</code> operator<a class=\"heading-anchor\" href=\"#is-operator\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>Object identity. Tests if two values are the same object. Example:</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\">somevar</span> <span class=\"k\">is</span> <span class=\"nf\">True</span> <span class=\"cp\">%}</span>\n  This appears if and only if somevar is True.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">somevar</span> <span class=\"k\">is</span> <span class=\"nf\">None</span> <span class=\"cp\">%}</span>\n  This appears if somevar is None, or if somevar is not found in the context.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"is-not-operator\">\n<h5><code class=\"docutils literal notranslate\">is not</code> operator<a class=\"heading-anchor\" href=\"#is-not-operator\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>Negated object identity. Tests if two values are not the same object. This is\nthe negation of the <code class=\"docutils literal notranslate\">is</code> operator. Example:</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\">somevar</span> <span class=\"k\">is</span> <span class=\"k\">not</span> <span class=\"nf\">True</span> <span class=\"cp\">%}</span>\n  This appears if somevar is not True, or if somevar is not found in the\n  context.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">somevar</span> <span class=\"k\">is</span> <span class=\"k\">not</span> <span class=\"nf\">None</span> <span class=\"cp\">%}</span>\n  This appears if and only if somevar is not None.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\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>You can also use filters in the <a class=\"reference internal\" href=\"#std-templatetag-if\"><code class=\"xref std std-ttag docutils literal notranslate\">if</code></a> expression. For example:</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\">messages</span><span class=\"o\">|</span><span class=\"nf\">length</span> <span class=\"o\">&gt;=</span> <span class=\"m\">100</span> <span class=\"cp\">%}</span>\n   You have lots of messages today!\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"complex-expressions\">\n<h4>Complex expressions<a class=\"heading-anchor\" href=\"#complex-expressions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>All of the above can be combined to form complex expressions. For such\nexpressions, it can be important to know how the operators are grouped when the\nexpression is evaluated - that is, the precedence rules. The precedence of the\noperators, from lowest to highest, is as follows:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">or</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">and</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">not</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">in</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">==</code>, <code class=\"docutils literal notranslate\">!=</code>, <code class=\"docutils literal notranslate\">&lt;</code>, <code class=\"docutils literal notranslate\">&gt;</code>, <code class=\"docutils literal notranslate\">&lt;=</code>, <code class=\"docutils literal notranslate\">&gt;=</code></p></li>\n</ul>\n<p>(This follows Python exactly). So, for example, the following complex\n<a class=\"reference internal\" href=\"#std-templatetag-if\"><code class=\"xref std std-ttag docutils literal notranslate\">if</code></a> tag:</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\">a</span> <span class=\"o\">==</span> <span class=\"nv\">b</span> <span class=\"k\">or</span> <span class=\"nv\">c</span> <span class=\"o\">==</span> <span class=\"nv\">d</span> <span class=\"k\">and</span> <span class=\"nv\">e</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>...will be interpreted as:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"p\">(</span>a <span class=\"o\">==</span> b<span class=\"p\">)</span> <span class=\"ow\">or</span> <span class=\"p\">((</span>c <span class=\"o\">==</span> d<span class=\"p\">)</span> <span class=\"ow\">and</span> e<span class=\"p\">)</span>\n</code></pre></div>\n<p>If you need different precedence, you will need to use nested <a class=\"reference internal\" href=\"#std-templatetag-if\"><code class=\"xref std std-ttag docutils literal notranslate\">if</code></a> tags.\nSometimes that is better for clarity anyway, for the sake of those who do not\nknow the precedence rules.</p>\n<p>The comparison operators cannot be 'chained' like in Python or in mathematical\nnotation. For example, instead of using:</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\">a</span> <span class=\"o\">&gt;</span> <span class=\"nv\">b</span> <span class=\"o\">&gt;</span> <span class=\"nv\">c</span> <span class=\"cp\">%}</span>  (WRONG)\n</code></pre></div>\n<p>you should use:</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\">a</span> <span class=\"o\">&gt;</span> <span class=\"nv\">b</span> <span class=\"k\">and</span> <span class=\"nv\">b</span> <span class=\"o\">&gt;</span> <span class=\"nv\">c</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"ifequal-and-ifnotequal\">\n<h3><code class=\"docutils literal notranslate\">ifequal</code> and <code class=\"docutils literal notranslate\">ifnotequal</code><a class=\"heading-anchor\" href=\"#ifequal-and-ifnotequal\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><code class=\"docutils literal notranslate\">{% ifequal a b %} ... {% endifequal %}</code> is an obsolete way to write\n<code class=\"docutils literal notranslate\">{% if a == b %} ... {% endif %}</code>. Likewise, <code class=\"docutils literal notranslate\">{% ifnotequal a b %} ...\n{% endifnotequal %}</code> is superseded by <code class=\"docutils literal notranslate\">{% if a != b %} ... {% endif %}</code>.\nThe <code class=\"docutils literal notranslate\">ifequal</code> and <code class=\"docutils literal notranslate\">ifnotequal</code> tags will be deprecated in a future release.</p>\n</section>\n<section id=\"ifchanged\">\n<span id=\"std-templatetag-ifchanged\"></span><h3><code class=\"docutils literal notranslate\">ifchanged</code><a class=\"heading-anchor\" href=\"#ifchanged\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Check if a value has changed from the last iteration of a loop.</p>\n<p>The <code class=\"docutils literal notranslate\">{% ifchanged %}</code> block tag is used within a loop. It has two possible\nuses.</p>\n<ol class=\"arabic\">\n<li><p>Checks its own rendered contents against its previous state and only\ndisplays the content if it has changed. For example, this displays a list of\ndays, only displaying the month if it changes:</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=\"p\">&lt;</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>Archive for <span class=\"cp\">{{</span> <span class=\"nv\">year</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">date</span> <span class=\"k\">in</span> <span class=\"nv\">days</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">ifchanged</span> <span class=\"cp\">%}</span><span class=\"p\">&lt;</span><span class=\"nt\">h3</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">date</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;F&quot;</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">h3</span><span class=\"p\">&gt;</span><span class=\"cp\">{%</span> <span class=\"k\">endifchanged</span> <span class=\"cp\">%}</span>\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\">date</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;M/d&quot;</span><span class=\"o\">|</span><span class=\"nf\">lower</span> <span class=\"cp\">}}</span><span class=\"s\">/&quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">date</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;j&quot;</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</li>\n<li><p>If given one or more variables, check whether any variable has changed.\nFor example, the following shows the date every time it changes, while\nshowing the hour if either the hour or the date has changed:</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\">for</span> <span class=\"nv\">date</span> <span class=\"k\">in</span> <span class=\"nv\">days</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">ifchanged</span> <span class=\"nv\">date.date</span> <span class=\"cp\">%}</span> <span class=\"cp\">{{</span> <span class=\"nv\">date.date</span> <span class=\"cp\">}}</span> <span class=\"cp\">{%</span> <span class=\"k\">endifchanged</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">ifchanged</span> <span class=\"nv\">date.hour</span> <span class=\"nv\">date.date</span> <span class=\"cp\">%}</span>\n        <span class=\"cp\">{{</span> <span class=\"nv\">date.hour</span> <span class=\"cp\">}}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">endifchanged</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</li>\n</ol>\n<p>The <code class=\"docutils literal notranslate\">ifchanged</code> tag can also take an optional <code class=\"docutils literal notranslate\">{% else %}</code> clause that\nwill be displayed if the value has not changed:</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\">for</span> <span class=\"nv\">match</span> <span class=\"k\">in</span> <span class=\"nv\">matches</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">style</span><span class=\"o\">=</span><span class=\"s\">&quot;background-color:</span>\n<span class=\"s\">        </span><span class=\"cp\">{%</span> <span class=\"k\">ifchanged</span> <span class=\"nv\">match.ballot_id</span> <span class=\"cp\">%}</span>\n<span class=\"s\">            </span><span class=\"cp\">{%</span> <span class=\"k\">cycle</span> <span class=\"s2\">&quot;red&quot;</span> <span class=\"s2\">&quot;blue&quot;</span> <span class=\"cp\">%}</span>\n<span class=\"s\">        </span><span class=\"cp\">{%</span> <span class=\"k\">else</span> <span class=\"cp\">%}</span>\n<span class=\"s\">            gray</span>\n<span class=\"s\">        </span><span class=\"cp\">{%</span> <span class=\"k\">endifchanged</span> <span class=\"cp\">%}</span>\n<span class=\"s\">    &quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">match</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"include\">\n<span id=\"std-templatetag-include\"></span><h3><code class=\"docutils literal notranslate\">include</code><a class=\"heading-anchor\" href=\"#include\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Loads a template and renders it with the current context. This is a way of\n&quot;including&quot; other templates within a template.</p>\n<p>The template name can either be a variable or a hard-coded (quoted) string,\nin either single or double quotes.</p>\n<p>This example includes the contents of the template <code class=\"docutils literal notranslate\">&quot;foo/bar.html&quot;</code>:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">include</span> <span class=\"s2\">&quot;foo/bar.html&quot;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Normally the template name is relative to the template loader's root directory.\nA string argument may also be a relative path starting with <code class=\"docutils literal notranslate\">./</code> or <code class=\"docutils literal notranslate\">../</code>\nas described in the <a class=\"reference internal\" href=\"#std-templatetag-extends\"><code class=\"xref std std-ttag docutils literal notranslate\">extends</code></a> tag.</p>\n<p>This example includes the contents of the template whose name is contained in\nthe variable <code class=\"docutils literal notranslate\">template_name</code>:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">include</span> <span class=\"nv\">template_name</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The variable may also be any object with a <code class=\"docutils literal notranslate\">render()</code> method that accepts a\ncontext. This allows you to reference a compiled <code class=\"docutils literal notranslate\">Template</code> in your context.</p>\n<p>An included template is rendered within the context of the template that\nincludes it. This example produces the output <code class=\"docutils literal notranslate\">&quot;Hello, John!&quot;</code>:</p>\n<ul>\n<li><p>Context: variable <code class=\"docutils literal notranslate\">person</code> is set to <code class=\"docutils literal notranslate\">&quot;John&quot;</code> and variable <code class=\"docutils literal notranslate\">greeting</code>\nis set to <code class=\"docutils literal notranslate\">&quot;Hello&quot;</code>.</p></li>\n<li><p>Template:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">include</span> <span class=\"s2\">&quot;name_snippet.html&quot;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</li>\n<li><p>The <code class=\"docutils literal notranslate\">name_snippet.html</code> template:</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\">greeting</span> <span class=\"cp\">}}</span>, <span class=\"cp\">{{</span> <span class=\"nv\">person</span><span class=\"o\">|</span><span class=\"nf\">default</span><span class=\"s2\">:&quot;friend&quot;</span> <span class=\"cp\">}}</span>!\n</code></pre></div>\n</li>\n</ul>\n<p>You can pass additional context to the template using keyword 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\">include</span> <span class=\"s2\">&quot;name_snippet.html&quot;</span> <span class=\"k\">with</span> <span class=\"nv\">person</span><span class=\"o\">=</span><span class=\"s2\">&quot;Jane&quot;</span> <span class=\"nv\">greeting</span><span class=\"o\">=</span><span class=\"s2\">&quot;Hello&quot;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>If you want to render the context only with the variables provided (or even\nno variables at all), use the <code class=\"docutils literal notranslate\">only</code> option. No other variables are\navailable to the included template:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">include</span> <span class=\"s2\">&quot;name_snippet.html&quot;</span> <span class=\"k\">with</span> <span class=\"nv\">greeting</span><span class=\"o\">=</span><span class=\"s2\">&quot;Hi&quot;</span> <span class=\"nv\">only</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>If the included template causes an exception while it's rendered (including\nif it's missing or has syntax errors), the behavior varies depending on the\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/templates/api/#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\">template engine's</code></a> <code class=\"docutils literal notranslate\">debug</code> option (if not\nset, this option defaults to the value of <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a>). When debug mode is\nturned on, an exception like <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/templates/#django.template.TemplateDoesNotExist\" title=\"django.template.TemplateDoesNotExist\"><code class=\"xref py py-exc docutils literal notranslate\">TemplateDoesNotExist</code></a> or\n<a class=\"reference internal\" href=\"/zh-hans/2.0/topics/templates/#django.template.TemplateSyntaxError\" title=\"django.template.TemplateSyntaxError\"><code class=\"xref py py-exc docutils literal notranslate\">TemplateSyntaxError</code></a> will be raised. When debug mode\nis turned off, <code class=\"docutils literal notranslate\">{% include %}</code> logs a warning to the <code class=\"docutils literal notranslate\">django.template</code>\nlogger with the exception that happens while rendering the included template\nand returns an empty string.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 1.11</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.11: </span>Silencing exceptions raised while rendering the <code class=\"docutils literal notranslate\">{% include %}</code> template\ntag is deprecated. In Django 2.1, the exception will be raised.</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The <a class=\"reference internal\" href=\"#std-templatetag-include\"><code class=\"xref std std-ttag docutils literal notranslate\">include</code></a> tag should be considered as an implementation of\n&quot;render this subtemplate and include the HTML&quot;, not as &quot;parse this\nsubtemplate and include its contents as if it were part of the parent&quot;.\nThis means that there is no shared state between included templates --\neach include is a completely independent rendering process.</p>\n<p>Blocks are evaluated <em>before</em> they are included. This means that a template\nthat includes blocks from another will contain blocks that have <em>already\nbeen evaluated and rendered</em> - not blocks that can be overridden by, for\nexample, an extending template.</p>\n</aside>\n</section>\n<section id=\"load\">\n<span id=\"std-templatetag-load\"></span><h3><code class=\"docutils literal notranslate\">load</code><a class=\"heading-anchor\" href=\"#load\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Loads a custom template tag set.</p>\n<p>For example, the following template would load all the tags and filters\nregistered in <code class=\"docutils literal notranslate\">somelibrary</code> and <code class=\"docutils literal notranslate\">otherlibrary</code> located in package\n<code class=\"docutils literal notranslate\">package</code>:</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\">load</span> <span class=\"nv\">somelibrary</span> <span class=\"nv\">package.otherlibrary</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>You can also selectively load individual filters or tags from a library, using\nthe <code class=\"docutils literal notranslate\">from</code> argument. In this example, the template tags/filters named <code class=\"docutils literal notranslate\">foo</code>\nand <code class=\"docutils literal notranslate\">bar</code> will be loaded from <code class=\"docutils literal notranslate\">somelibrary</code>:</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\">load</span> <span class=\"nv\">foo</span> <span class=\"nv\">bar</span> <span class=\"nv\">from</span> <span class=\"nv\">somelibrary</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.0/howto/custom-template-tags/\"><span class=\"doc\">Custom tag and filter libraries</span></a> for\nmore information.</p>\n</section>\n<section id=\"lorem\">\n<span id=\"std-templatetag-lorem\"></span><h3><code class=\"docutils literal notranslate\">lorem</code><a class=\"heading-anchor\" href=\"#lorem\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Displays random &quot;lorem ipsum&quot; Latin text. This is useful for providing sample\ndata in templates.</p>\n<p>Usage:</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\">lorem</span> <span class=\"o\">[</span><span class=\"nv\">count</span><span class=\"o\">]</span> <span class=\"o\">[</span><span class=\"nv\">method</span><span class=\"o\">]</span> <span class=\"o\">[</span><span class=\"nv\">random</span><span class=\"o\">]</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The <code class=\"docutils literal notranslate\">{% lorem %}</code> tag can be used with zero, one, two or three arguments.\nThe arguments are:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Argument</p></th>\n<th class=\"head\"><p>Description</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">count</code></p></td>\n<td><p>A number (or variable) containing the number of paragraphs or\nwords to generate (default is 1).</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">method</code></p></td>\n<td><p>Either <code class=\"docutils literal notranslate\">w</code> for words, <code class=\"docutils literal notranslate\">p</code> for HTML paragraphs or <code class=\"docutils literal notranslate\">b</code>\nfor plain-text paragraph blocks (default is <code class=\"docutils literal notranslate\">b</code>).</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">random</code></p></td>\n<td><p>The word <code class=\"docutils literal notranslate\">random</code>, which if given, does not use the common\nparagraph (&quot;Lorem ipsum dolor sit amet...&quot;) when generating\ntext.</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>Examples:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">{% lorem %}</code> will output the common &quot;lorem ipsum&quot; paragraph.</p></li>\n<li><p><code class=\"docutils literal notranslate\">{% lorem 3 p %}</code> will output the common &quot;lorem ipsum&quot; paragraph\nand two random paragraphs each wrapped in HTML <code class=\"docutils literal notranslate\">&lt;p&gt;</code> tags.</p></li>\n<li><p><code class=\"docutils literal notranslate\">{% lorem 2 w random %}</code> will output two random Latin words.</p></li>\n</ul>\n</section>\n<section id=\"now\">\n<span id=\"std-templatetag-now\"></span><h3><code class=\"docutils literal notranslate\">now</code><a class=\"heading-anchor\" href=\"#now\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Displays the current date and/or time, using a format according to the given\nstring. Such string can contain format specifiers characters as described\nin the <a class=\"reference internal\" href=\"#std-templatefilter-date\"><code class=\"xref std std-tfilter docutils literal notranslate\">date</code></a> filter section.</p>\n<p>Example:</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>It is <span class=\"cp\">{%</span> <span class=\"k\">now</span> <span class=\"s2\">&quot;jS F Y H:i&quot;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Note that you can backslash-escape a format string if you want to use the\n&quot;raw&quot; value. In this example, both &quot;o&quot; and &quot;f&quot; are backslash-escaped, because\notherwise each is a format string that displays the year and the time,\nrespectively:</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>It is the <span class=\"cp\">{%</span> <span class=\"k\">now</span> <span class=\"s2\">&quot;jS \\o\\f F&quot;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>This would display as &quot;It is the 4th of September&quot;.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The format passed can also be one of the predefined ones\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-DATE_FORMAT\"><code class=\"xref std std-setting docutils literal notranslate\">DATE_FORMAT</code></a>, <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-DATETIME_FORMAT\"><code class=\"xref std std-setting docutils literal notranslate\">DATETIME_FORMAT</code></a>,\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-SHORT_DATE_FORMAT\"><code class=\"xref std std-setting docutils literal notranslate\">SHORT_DATE_FORMAT</code></a> or <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-SHORT_DATETIME_FORMAT\"><code class=\"xref std std-setting docutils literal notranslate\">SHORT_DATETIME_FORMAT</code></a>.\nThe predefined formats may vary depending on the current locale and\nif <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/i18n/formatting/\"><span class=\"doc\">Format localization</span></a> is enabled, e.g.:</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>It is <span class=\"cp\">{%</span> <span class=\"k\">now</span> <span class=\"s2\">&quot;SHORT_DATETIME_FORMAT&quot;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</aside>\n<p>You can also use the syntax <code class=\"docutils literal notranslate\">{% now &quot;Y&quot; as current_year %}</code> to store the\noutput (as a string) inside a variable. This is useful if you want to use\n<code class=\"docutils literal notranslate\">{% now %}</code> inside a template tag like <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/i18n/translation/#std-templatetag-blocktrans\"><code class=\"xref std std-ttag docutils literal notranslate\">blocktrans</code></a> for example:</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\">now</span> <span class=\"s2\">&quot;Y&quot;</span> <span class=\"k\">as</span> <span class=\"nv\">current_year</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">blocktrans</span> <span class=\"cp\">%}</span>Copyright <span class=\"cp\">{{</span> <span class=\"nv\">current_year</span> <span class=\"cp\">}}{%</span> <span class=\"k\">endblocktrans</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"regroup\">\n<span id=\"std-templatetag-regroup\"></span><h3><code class=\"docutils literal notranslate\">regroup</code><a class=\"heading-anchor\" href=\"#regroup\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Regroups a list of alike objects by a common attribute.</p>\n<p>This complex tag is best illustrated by way of an example: say that <code class=\"docutils literal notranslate\">cities</code>\nis a list of cities represented by dictionaries containing <code class=\"docutils literal notranslate\">&quot;name&quot;</code>,\n<code class=\"docutils literal notranslate\">&quot;population&quot;</code>, and <code class=\"docutils literal notranslate\">&quot;country&quot;</code> keys:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code>cities <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Mumbai&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;population&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;19,000,000&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;country&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;India&#39;</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Calcutta&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;population&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;15,000,000&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;country&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;India&#39;</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;New York&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;population&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;20,000,000&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;country&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;USA&#39;</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Chicago&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;population&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;7,000,000&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;country&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;USA&#39;</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Tokyo&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;population&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;33,000,000&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;country&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Japan&#39;</span><span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>...and you'd like to display a hierarchical list that is ordered by country,\nlike this:</p>\n<ul class=\"simple\">\n<li><p>India</p>\n<ul>\n<li><p>Mumbai: 19,000,000</p></li>\n<li><p>Calcutta: 15,000,000</p></li>\n</ul>\n</li>\n<li><p>USA</p>\n<ul>\n<li><p>New York: 20,000,000</p></li>\n<li><p>Chicago: 7,000,000</p></li>\n</ul>\n</li>\n<li><p>Japan</p>\n<ul>\n<li><p>Tokyo: 33,000,000</p></li>\n</ul>\n</li>\n</ul>\n<p>You can use the <code class=\"docutils literal notranslate\">{% regroup %}</code> tag to group the list of cities by country.\nThe following snippet of template code would accomplish 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\">regroup</span> <span class=\"nv\">cities</span> <span class=\"nv\">by</span> <span class=\"nv\">country</span> <span class=\"k\">as</span> <span class=\"nv\">country_list</span> <span class=\"cp\">%}</span>\n\n<span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">country</span> <span class=\"k\">in</span> <span class=\"nv\">country_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">country.grouper</span> <span class=\"cp\">}}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">city</span> <span class=\"k\">in</span> <span class=\"nv\">country.list</span> <span class=\"cp\">%}</span>\n          <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">city.name</span> <span class=\"cp\">}}</span>: <span class=\"cp\">{{</span> <span class=\"nv\">city.population</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>Let's walk through this example. <code class=\"docutils literal notranslate\">{% regroup %}</code> takes three arguments: the\nlist you want to regroup, the attribute to group by, and the name of the\nresulting list. Here, we're regrouping the <code class=\"docutils literal notranslate\">cities</code> list by the <code class=\"docutils literal notranslate\">country</code>\nattribute and calling the result <code class=\"docutils literal notranslate\">country_list</code>.</p>\n<p><code class=\"docutils literal notranslate\">{% regroup %}</code> produces a list (in this case, <code class=\"docutils literal notranslate\">country_list</code>) of\n<strong>group objects</strong>. Group objects are instances of\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/collections.html#collections.namedtuple\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">namedtuple()</code></a> with two fields:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">grouper</code> -- the item that was grouped by (e.g., the string &quot;India&quot; or\n&quot;Japan&quot;).</p></li>\n<li><p><code class=\"docutils literal notranslate\">list</code> -- a list of all items in this group (e.g., a list of all cities\nwith country='India').</p></li>\n</ul>\n<aside class=\"version-note version-changed\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 1.11</p><p>The group object was changed from a dictionary to a\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/collections.html#collections.namedtuple\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">namedtuple()</code></a>.</p>\n</aside>\n<p>Because <code class=\"docutils literal notranslate\">{% regroup %}</code> produces <a class=\"reference external\" href=\"https://docs.python.org/3/library/collections.html#collections.namedtuple\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">namedtuple()</code></a> objects,\nyou can also write the previous example as:</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\">regroup</span> <span class=\"nv\">cities</span> <span class=\"nv\">by</span> <span class=\"nv\">country</span> <span class=\"k\">as</span> <span class=\"nv\">country_list</span> <span class=\"cp\">%}</span>\n\n<span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">country</span><span class=\"o\">,</span> <span class=\"nv\">local_cities</span> <span class=\"k\">in</span> <span class=\"nv\">country_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">country</span> <span class=\"cp\">}}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">city</span> <span class=\"k\">in</span> <span class=\"nv\">local_cities</span> <span class=\"cp\">%}</span>\n          <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">city.name</span> <span class=\"cp\">}}</span>: <span class=\"cp\">{{</span> <span class=\"nv\">city.population</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>Note that <code class=\"docutils literal notranslate\">{% regroup %}</code> does not order its input! Our example relies on\nthe fact that the <code class=\"docutils literal notranslate\">cities</code> list was ordered by <code class=\"docutils literal notranslate\">country</code> in the first place.\nIf the <code class=\"docutils literal notranslate\">cities</code> list did <em>not</em> order its members by <code class=\"docutils literal notranslate\">country</code>, the\nregrouping would naively display more than one group for a single country. For\nexample, say the <code class=\"docutils literal notranslate\">cities</code> list was set to this (note that the countries are not\ngrouped together):</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code>cities <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Mumbai&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;population&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;19,000,000&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;country&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;India&#39;</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;New York&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;population&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;20,000,000&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;country&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;USA&#39;</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Calcutta&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;population&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;15,000,000&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;country&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;India&#39;</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Chicago&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;population&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;7,000,000&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;country&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;USA&#39;</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Tokyo&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;population&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;33,000,000&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;country&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Japan&#39;</span><span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>With this input for <code class=\"docutils literal notranslate\">cities</code>, the example <code class=\"docutils literal notranslate\">{% regroup %}</code> template code\nabove would result in the following output:</p>\n<ul class=\"simple\">\n<li><p>India</p>\n<ul>\n<li><p>Mumbai: 19,000,000</p></li>\n</ul>\n</li>\n<li><p>USA</p>\n<ul>\n<li><p>New York: 20,000,000</p></li>\n</ul>\n</li>\n<li><p>India</p>\n<ul>\n<li><p>Calcutta: 15,000,000</p></li>\n</ul>\n</li>\n<li><p>USA</p>\n<ul>\n<li><p>Chicago: 7,000,000</p></li>\n</ul>\n</li>\n<li><p>Japan</p>\n<ul>\n<li><p>Tokyo: 33,000,000</p></li>\n</ul>\n</li>\n</ul>\n<p>The easiest solution to this gotcha is to make sure in your view code that the\ndata is ordered according to how you want to display it.</p>\n<p>Another solution is to sort the data in the template using the\n<a class=\"reference internal\" href=\"#std-templatefilter-dictsort\"><code class=\"xref std std-tfilter docutils literal notranslate\">dictsort</code></a> filter, if your data is in a list of dictionaries:</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\">regroup</span> <span class=\"nv\">cities</span><span class=\"o\">|</span><span class=\"nf\">dictsort</span><span class=\"s2\">:&quot;country&quot;</span> <span class=\"nv\">by</span> <span class=\"nv\">country</span> <span class=\"k\">as</span> <span class=\"nv\">country_list</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<section id=\"grouping-on-other-properties\">\n<h4>Grouping on other properties<a class=\"heading-anchor\" href=\"#grouping-on-other-properties\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Any valid template lookup is a legal grouping attribute for the regroup\ntag, including methods, attributes, dictionary keys and list items. For\nexample, if the &quot;country&quot; field is a foreign key to a class with\nan attribute &quot;description,&quot; you could use:</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\">regroup</span> <span class=\"nv\">cities</span> <span class=\"nv\">by</span> <span class=\"nv\">country.description</span> <span class=\"k\">as</span> <span class=\"nv\">country_list</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Or, if <code class=\"docutils literal notranslate\">country</code> is a field with <code class=\"docutils literal notranslate\">choices</code>, it will have a\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/models/instances/#django.db.models.Model.get_FOO_display\" title=\"django.db.models.Model.get_FOO_display\"><code class=\"xref py py-meth docutils literal notranslate\">get_FOO_display()</code></a> method available as an\nattribute, allowing  you to group on the display string rather than the\n<code class=\"docutils literal notranslate\">choices</code> key:</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\">regroup</span> <span class=\"nv\">cities</span> <span class=\"nv\">by</span> <span class=\"nv\">get_country_display</span> <span class=\"k\">as</span> <span class=\"nv\">country_list</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">{{ country.grouper }}</code> will now display the value fields from the\n<code class=\"docutils literal notranslate\">choices</code> set rather than the keys.</p>\n</section>\n</section>\n<section id=\"resetcycle\">\n<span id=\"std-templatetag-resetcycle\"></span><h3><code class=\"docutils literal notranslate\">resetcycle</code><a class=\"heading-anchor\" href=\"#resetcycle\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"version-note version-added\" data-version=\"1.11\" role=\"note\">\n<p class=\"version-note-title\">New in Django 1.11</p></aside>\n<p>Resets a previous <a class=\"reference internal\" href=\"#cycle\">cycle</a> so that it restarts from its first item at its next\nencounter. Without arguments, <code class=\"docutils literal notranslate\">{% resetcycle %}</code> will reset the last\n<code class=\"docutils literal notranslate\">{% cycle %}</code> defined in the template.</p>\n<p>Example usage:</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\">for</span> <span class=\"nv\">coach</span> <span class=\"k\">in</span> <span class=\"nv\">coach_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">coach.name</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">athlete</span> <span class=\"k\">in</span> <span class=\"nv\">coach.athlete_set.all</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">p</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><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><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">athlete.name</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">resetcycle</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>This example would return this HTML:</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=\"p\">&lt;</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>José Mourinho<span class=\"p\">&lt;/</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">p</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;odd&quot;</span><span class=\"p\">&gt;</span>Thibaut Courtois<span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">p</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;even&quot;</span><span class=\"p\">&gt;</span>John Terry<span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">p</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;odd&quot;</span><span class=\"p\">&gt;</span>Eden Hazard<span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n\n<span class=\"p\">&lt;</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>Carlo Ancelotti<span class=\"p\">&lt;/</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">p</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;odd&quot;</span><span class=\"p\">&gt;</span>Manuel Neuer<span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">p</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;even&quot;</span><span class=\"p\">&gt;</span>Thomas Müller<span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>Notice how the first block ends with <code class=\"docutils literal notranslate\">class=&quot;odd&quot;</code> and the new one starts\nwith <code class=\"docutils literal notranslate\">class=&quot;odd&quot;</code>. Without the <code class=\"docutils literal notranslate\">{% resetcycle %}</code> tag, the second block\nwould start with <code class=\"docutils literal notranslate\">class=&quot;even&quot;</code>.</p>\n<p>You can also reset named cycle 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\">for</span> <span class=\"nv\">item</span> <span class=\"k\">in</span> <span class=\"nv\">list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">p</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><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=\"k\">as</span> <span class=\"nv\">stripe</span> <span class=\"cp\">%}</span><span class=\"s\"> </span><span class=\"cp\">{%</span> <span class=\"k\">cycle</span> <span class=\"s1\">&#39;major&#39;</span> <span class=\"s1\">&#39;minor&#39;</span> <span class=\"s1\">&#39;minor&#39;</span> <span class=\"s1\">&#39;minor&#39;</span> <span class=\"s1\">&#39;minor&#39;</span> <span class=\"k\">as</span> <span class=\"nv\">tick</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{{</span> <span class=\"nv\">item.data</span> <span class=\"cp\">}}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">ifchanged</span> <span class=\"nv\">item.category</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">item.category</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"k\">not</span> <span class=\"nb\">forloop</span><span class=\"nv\">.first</span> <span class=\"cp\">%}{%</span> <span class=\"k\">resetcycle</span> <span class=\"nv\">tick</span> <span class=\"cp\">%}{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">endifchanged</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>In this example, we have both the alternating odd/even rows and a &quot;major&quot; row\nevery fifth row. Only the five-row cycle is reset when a category changes.</p>\n</section>\n<section id=\"spaceless\">\n<span id=\"std-templatetag-spaceless\"></span><h3><code class=\"docutils literal notranslate\">spaceless</code><a class=\"heading-anchor\" href=\"#spaceless\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Removes whitespace between HTML tags. This includes tab\ncharacters and newlines.</p>\n<p>Example usage:</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\">spaceless</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;foo/&quot;</span><span class=\"p\">&gt;</span>Foo<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endspaceless</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>This example would return this HTML:</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=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;foo/&quot;</span><span class=\"p\">&gt;</span>Foo<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>Only space between <em>tags</em> is removed -- not space between tags and text. In\nthis example, the space around <code class=\"docutils literal notranslate\">Hello</code> won't be stripped:</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\">spaceless</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">strong</span><span class=\"p\">&gt;</span>\n        Hello\n    <span class=\"p\">&lt;/</span><span class=\"nt\">strong</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endspaceless</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"templatetag\">\n<span id=\"std-templatetag-templatetag\"></span><h3><code class=\"docutils literal notranslate\">templatetag</code><a class=\"heading-anchor\" href=\"#templatetag\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Outputs one of the syntax characters used to compose template tags.</p>\n<p>Since the template system has no concept of &quot;escaping&quot;, to display one of the\nbits used in template tags, you must use the <code class=\"docutils literal notranslate\">{% templatetag %}</code> tag.</p>\n<p>The argument tells which template bit to output:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Argument</p></th>\n<th class=\"head\"><p>Outputs</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">openblock</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{%</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">closeblock</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">%}</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">openvariable</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">closevariable</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">}}</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">openbrace</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">closebrace</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">}</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">opencomment</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{#</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">closecomment</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">#}</code></p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>Sample usage:</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\">templatetag</span> <span class=\"nv\">openblock</span> <span class=\"cp\">%}</span> url &#39;entry_list&#39; <span class=\"cp\">{%</span> <span class=\"k\">templatetag</span> <span class=\"nv\">closeblock</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"url\">\n<span id=\"std-templatetag-url\"></span><h3><code class=\"docutils literal notranslate\">url</code><a class=\"heading-anchor\" href=\"#url\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns an absolute path reference (a URL without the domain name) matching a\ngiven view and optional parameters. Any special characters in the resulting\npath will be encoded using <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/utils/#django.utils.encoding.iri_to_uri\" title=\"django.utils.encoding.iri_to_uri\"><code class=\"xref py py-func docutils literal notranslate\">iri_to_uri()</code></a>.</p>\n<p>This is a way to output links without violating the DRY principle by having to\nhard-code URLs in your templates:</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\">url</span> <span class=\"s1\">&#39;some-url-name&#39;</span> <span class=\"nv\">v1</span> <span class=\"nv\">v2</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The first argument is a <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/http/urls/#naming-url-patterns\"><span class=\"std std-ref\">URL pattern name</span></a>. It can\nbe a quoted literal or any other context variable. Additional arguments are\noptional and should be space-separated values that will be used as arguments in\nthe URL. The example above shows passing positional arguments. Alternatively\nyou may use keyword syntax:</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\">url</span> <span class=\"s1\">&#39;some-url-name&#39;</span> <span class=\"nv\">arg1</span><span class=\"o\">=</span><span class=\"nv\">v1</span> <span class=\"nv\">arg2</span><span class=\"o\">=</span><span class=\"nv\">v2</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Do not mix both positional and keyword syntax in a single call. All arguments\nrequired by the URLconf should be present.</p>\n<p>For example, suppose you have a view, <code class=\"docutils literal notranslate\">app_views.client</code>, whose URLconf\ntakes a client ID (here, <code class=\"docutils literal notranslate\">client()</code> is a method inside the views file\n<code class=\"docutils literal notranslate\">app_views.py</code>). The URLconf line might look like this:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code>path<span class=\"p\">(</span><span class=\"s1\">&#39;client/&lt;int:id&gt;/&#39;</span><span class=\"p\">,</span> app_views<span class=\"o\">.</span>client<span class=\"p\">,</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;app-views-client&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>If this app's URLconf is included into the project's URLconf under a path\nsuch as this:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code>path<span class=\"p\">(</span><span class=\"s1\">&#39;clients/&#39;</span><span class=\"p\">,</span> include<span class=\"p\">(</span><span class=\"s1\">&#39;project_name.app_name.urls&#39;</span><span class=\"p\">))</span>\n</code></pre></div>\n<p>...then, in a template, you can create a link to this view 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\">url</span> <span class=\"s1\">&#39;app-views-client&#39;</span> <span class=\"nv\">client.id</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The template tag will output the string <code class=\"docutils literal notranslate\">/clients/client/123/</code>.</p>\n<p>Note that if the URL you're reversing doesn't exist, you'll get an\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/exceptions/#django.urls.NoReverseMatch\" title=\"django.urls.NoReverseMatch\"><code class=\"xref py py-exc docutils literal notranslate\">NoReverseMatch</code></a> exception raised, which will cause your\nsite to display an error page.</p>\n<p>If you'd like to retrieve a URL without displaying it, you can use a slightly\ndifferent call:</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\">url</span> <span class=\"s1\">&#39;some-url-name&#39;</span> <span class=\"nv\">arg</span> <span class=\"nv\">arg2</span> <span class=\"k\">as</span> <span class=\"nv\">the_url</span> <span class=\"cp\">%}</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\">the_url</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>I&#39;m linking to <span class=\"cp\">{{</span> <span class=\"nv\">the_url</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>The scope of the variable created by the  <code class=\"docutils literal notranslate\">as var</code> syntax is the\n<code class=\"docutils literal notranslate\">{% block %}</code> in which the <code class=\"docutils literal notranslate\">{% url %}</code> tag appears.</p>\n<p>This <code class=\"docutils literal notranslate\">{% url ... as var %}</code> syntax will <em>not</em> cause an error if the view is\nmissing. In practice you'll use this to link to views that are optional:</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\">url</span> <span class=\"s1\">&#39;some-url-name&#39;</span> <span class=\"k\">as</span> <span class=\"nv\">the_url</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">the_url</span> <span class=\"cp\">%}</span>\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\">the_url</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>Link to optional stuff<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>If you'd like to retrieve a namespaced URL, specify the fully qualified name:</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\">url</span> <span class=\"s1\">&#39;myapp:view-name&#39;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>This will follow the normal <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/http/urls/#topics-http-reversing-url-namespaces\"><span class=\"std std-ref\">namespaced URL resolution strategy</span></a>, including using any hints provided\nby the context as to the current application.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Don't forget to put quotes around the URL pattern <code class=\"docutils literal notranslate\">name</code>, otherwise the\nvalue will be interpreted as a context variable!</p>\n</aside>\n</section>\n<section id=\"verbatim\">\n<span id=\"std-templatetag-verbatim\"></span><h3><code class=\"docutils literal notranslate\">verbatim</code><a class=\"heading-anchor\" href=\"#verbatim\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Stops the template engine from rendering the contents of this block tag.</p>\n<p>A common use is to allow a JavaScript template layer that collides with\nDjango's syntax. For example:</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\">verbatim</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span><span class=\"k\">if</span> <span class=\"nv\">dying</span><span class=\"cp\">}}</span>Still alive.<span class=\"cp\">{{</span><span class=\"o\">/</span><span class=\"k\">if</span><span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endverbatim</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>You can also designate a specific closing tag, allowing the use of\n<code class=\"docutils literal notranslate\">{% endverbatim %}</code> as part of the unrendered contents:</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\">verbatim</span> <span class=\"nv\">myblock</span> <span class=\"cp\">%}</span>\n    Avoid template rendering via the <span class=\"cp\">{%</span> <span class=\"k\">verbatim</span> <span class=\"cp\">%}{%</span> <span class=\"k\">endverbatim</span> <span class=\"cp\">%}</span> block.\n<span class=\"cp\">{%</span> <span class=\"k\">endverbatim</span> <span class=\"nv\">myblock</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"widthratio\">\n<span id=\"std-templatetag-widthratio\"></span><h3><code class=\"docutils literal notranslate\">widthratio</code><a class=\"heading-anchor\" href=\"#widthratio\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>For creating bar charts and such, this tag calculates the ratio of a given\nvalue to a maximum value, and then applies that ratio to a constant.</p>\n<p>For example:</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=\"p\">&lt;</span><span class=\"nt\">img</span> <span class=\"na\">src</span><span class=\"o\">=</span><span class=\"s\">&quot;bar.png&quot;</span> <span class=\"na\">alt</span><span class=\"o\">=</span><span class=\"s\">&quot;Bar&quot;</span>\n     <span class=\"na\">height</span><span class=\"o\">=</span><span class=\"s\">&quot;10&quot;</span> <span class=\"na\">width</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">widthratio</span> <span class=\"nv\">this_value</span> <span class=\"nv\">max_value</span> <span class=\"nv\">max_width</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span> <span class=\"p\">/&gt;</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">this_value</code> is 175, <code class=\"docutils literal notranslate\">max_value</code> is 200, and <code class=\"docutils literal notranslate\">max_width</code> is 100, the\nimage in the above example will be 88 pixels wide\n(because 175/200 = .875; .875 * 100 = 87.5 which is rounded up to 88).</p>\n<p>In some cases you might want to capture the result of <code class=\"docutils literal notranslate\">widthratio</code> in a\nvariable. It can be useful, for instance, in a <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/i18n/translation/#std-templatetag-blocktrans\"><code class=\"xref std std-ttag docutils literal notranslate\">blocktrans</code></a> 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\">widthratio</span> <span class=\"nv\">this_value</span> <span class=\"nv\">max_value</span> <span class=\"nv\">max_width</span> <span class=\"k\">as</span> <span class=\"nv\">width</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">blocktrans</span> <span class=\"cp\">%}</span>The width is: <span class=\"cp\">{{</span> <span class=\"nv\">width</span> <span class=\"cp\">}}{%</span> <span class=\"k\">endblocktrans</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"with\">\n<span id=\"std-templatetag-with\"></span><h3><code class=\"docutils literal notranslate\">with</code><a class=\"heading-anchor\" href=\"#with\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Caches a complex variable under a simpler name. This is useful when accessing\nan &quot;expensive&quot; method (e.g., one that hits the database) multiple times.</p>\n<p>For example:</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\">with</span> <span class=\"nv\">total</span><span class=\"o\">=</span><span class=\"nv\">business.employees.count</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">total</span> <span class=\"cp\">}}</span> employee<span class=\"cp\">{{</span> <span class=\"nv\">total</span><span class=\"o\">|</span><span class=\"nf\">pluralize</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endwith</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The populated variable (in the example above, <code class=\"docutils literal notranslate\">total</code>) is only available\nbetween the <code class=\"docutils literal notranslate\">{% with %}</code> and <code class=\"docutils literal notranslate\">{% endwith %}</code> tags.</p>\n<p>You can assign more than one context variable:</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\">with</span> <span class=\"nv\">alpha</span><span class=\"o\">=</span><span class=\"m\">1</span> <span class=\"nv\">beta</span><span class=\"o\">=</span><span class=\"m\">2</span> <span class=\"cp\">%}</span>\n    ...\n<span class=\"cp\">{%</span> <span class=\"k\">endwith</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The previous more verbose format is still supported:\n<code class=\"docutils literal notranslate\">{% with business.employees.count as total %}</code></p>\n</aside>\n</section>\n</section>\n<section id=\"built-in-filter-reference\">\n<span id=\"ref-templates-builtins-filters\"></span><h2>Built-in filter reference<a class=\"heading-anchor\" href=\"#built-in-filter-reference\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"add\">\n<span id=\"std-templatefilter-add\"></span><h3><code class=\"docutils literal notranslate\">add</code><a class=\"heading-anchor\" href=\"#add\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Adds the argument to the value.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">add</span><span class=\"s2\">:&quot;2&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">4</code>, then the output will be <code class=\"docutils literal notranslate\">6</code>.</p>\n<p>This filter will first try to coerce both values to integers. If this fails,\nit'll attempt to add the values together anyway. This will work on some data\ntypes (strings, list, etc.) and fail on others. If it fails, the result will\nbe an empty string.</p>\n<p>For example, if we have:</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\">first</span><span class=\"o\">|</span><span class=\"nf\">add</span><span class=\"o\">:</span><span class=\"nv\">second</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>and <code class=\"docutils literal notranslate\">first</code> is <code class=\"docutils literal notranslate\">[1, 2, 3]</code> and <code class=\"docutils literal notranslate\">second</code> is <code class=\"docutils literal notranslate\">[4, 5, 6]</code>, then the\noutput will be <code class=\"docutils literal notranslate\">[1, 2, 3, 4, 5, 6]</code>.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Strings that can be coerced to integers will be <strong>summed</strong>, not\nconcatenated, as in the first example above.</p>\n</aside>\n</section>\n<section id=\"addslashes\">\n<span id=\"std-templatefilter-addslashes\"></span><h3><code class=\"docutils literal notranslate\">addslashes</code><a class=\"heading-anchor\" href=\"#addslashes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Adds slashes before quotes. Useful for escaping strings in CSV, for example.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">addslashes</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;I'm using Django&quot;</code>, the output will be\n<code class=\"docutils literal notranslate\">&quot;I\\'m using Django&quot;</code>.</p>\n</section>\n<section id=\"capfirst\">\n<span id=\"std-templatefilter-capfirst\"></span><h3><code class=\"docutils literal notranslate\">capfirst</code><a class=\"heading-anchor\" href=\"#capfirst\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Capitalizes the first character of the value. If the first character is not\na letter, this filter has no effect.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">capfirst</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;django&quot;</code>, the output will be <code class=\"docutils literal notranslate\">&quot;Django&quot;</code>.</p>\n</section>\n<section id=\"center\">\n<span id=\"std-templatefilter-center\"></span><h3><code class=\"docutils literal notranslate\">center</code><a class=\"heading-anchor\" href=\"#center\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Centers the value in a field of a given width.</p>\n<p>For example:</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>&quot;<span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">center</span><span class=\"s2\">:&quot;15&quot;</span> <span class=\"cp\">}}</span>&quot;\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;Django&quot;</code>, the output will be <code class=\"docutils literal notranslate\">&quot;&#160;&#160;&#160;&#160; Django&#160;&#160;&#160; &quot;</code>.</p>\n</section>\n<section id=\"cut\">\n<span id=\"std-templatefilter-cut\"></span><h3><code class=\"docutils literal notranslate\">cut</code><a class=\"heading-anchor\" href=\"#cut\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Removes all values of arg from the given string.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">cut</span><span class=\"s2\">:&quot; &quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;String with spaces&quot;</code>, the output will be\n<code class=\"docutils literal notranslate\">&quot;Stringwithspaces&quot;</code>.</p>\n</section>\n<section id=\"date\">\n<span id=\"std-templatefilter-date\"></span><h3><code class=\"docutils literal notranslate\">date</code><a class=\"heading-anchor\" href=\"#date\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Formats a date according to the given format.</p>\n<p>Uses a similar format as PHP's <code class=\"docutils literal notranslate\">date()</code> function (<a class=\"reference external\" href=\"https://php.net/date\">https://php.net/date</a>)\nwith some differences.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>These format characters are not used in Django outside of templates. They\nwere designed to be compatible with PHP to ease transitioning for designers.</p>\n</aside>\n<p id=\"date-and-time-formatting-specifiers\">Available format strings:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Format character</p></th>\n<th class=\"head\"><p>Description</p></th>\n<th class=\"head\"><p>Example output</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p>a</p></td>\n<td><p><code class=\"docutils literal notranslate\">'a.m.'</code> or <code class=\"docutils literal notranslate\">'p.m.'</code> (Note that\nthis is slightly different than PHP's\noutput, because this includes periods\nto match Associated Press style.)</p></td>\n<td><p><code class=\"docutils literal notranslate\">'a.m.'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>A</p></td>\n<td><p><code class=\"docutils literal notranslate\">'AM'</code> or <code class=\"docutils literal notranslate\">'PM'</code>.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'AM'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>b</p></td>\n<td><p>Month, textual, 3 letters, lowercase.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'jan'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>B</p></td>\n<td><p>Not implemented.</p></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p>c</p></td>\n<td><p>ISO 8601 format. (Note: unlike others\nformatters, such as &quot;Z&quot;, &quot;O&quot; or &quot;r&quot;,\nthe &quot;c&quot; formatter will not add timezone\noffset if value is a naive datetime\n(see <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.tzinfo\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">datetime.tzinfo</code></a>).</p></td>\n<td><p><code class=\"docutils literal notranslate\">2008-01-02T10:30:00.000123+02:00</code>,\nor <code class=\"docutils literal notranslate\">2008-01-02T10:30:00.000123</code> if the datetime is naive</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>d</p></td>\n<td><p>Day of the month, 2 digits with\nleading zeros.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'01'</code> to <code class=\"docutils literal notranslate\">'31'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>D</p></td>\n<td><p>Day of the week, textual, 3 letters.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'Fri'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>e</p></td>\n<td><p>Timezone name. Could be in any format,\nor might return an empty string,\ndepending on the datetime.</p></td>\n<td><p><code class=\"docutils literal notranslate\">''</code>, <code class=\"docutils literal notranslate\">'GMT'</code>, <code class=\"docutils literal notranslate\">'-500'</code>, <code class=\"docutils literal notranslate\">'US/Eastern'</code>, etc.</p></td>\n</tr>\n<tr class=\"row-even\"><td><p>E</p></td>\n<td><p>Month, locale specific alternative\nrepresentation usually used for long\ndate representation.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'listopada'</code> (for Polish locale, as opposed to <code class=\"docutils literal notranslate\">'Listopad'</code>)</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>f</p></td>\n<td><p>Time, in 12-hour hours and minutes,\nwith minutes left off if they're zero.\nProprietary extension.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'1'</code>, <code class=\"docutils literal notranslate\">'1:30'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>F</p></td>\n<td><p>Month, textual, long.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'January'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>g</p></td>\n<td><p>Hour, 12-hour format without leading\nzeros.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'1'</code> to <code class=\"docutils literal notranslate\">'12'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>G</p></td>\n<td><p>Hour, 24-hour format without leading\nzeros.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'0'</code> to <code class=\"docutils literal notranslate\">'23'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>h</p></td>\n<td><p>Hour, 12-hour format.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'01'</code> to <code class=\"docutils literal notranslate\">'12'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>H</p></td>\n<td><p>Hour, 24-hour format.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'00'</code> to <code class=\"docutils literal notranslate\">'23'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>i</p></td>\n<td><p>Minutes.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'00'</code> to <code class=\"docutils literal notranslate\">'59'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>I</p></td>\n<td><p>Daylight Savings Time, whether it's\nin effect or not.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'1'</code> or <code class=\"docutils literal notranslate\">'0'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>j</p></td>\n<td><p>Day of the month without leading\nzeros.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'1'</code> to <code class=\"docutils literal notranslate\">'31'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>l</p></td>\n<td><p>Day of the week, textual, long.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'Friday'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>L</p></td>\n<td><p>Boolean for whether it's a leap year.</p></td>\n<td><p><code class=\"docutils literal notranslate\">True</code> or <code class=\"docutils literal notranslate\">False</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>m</p></td>\n<td><p>Month, 2 digits with leading zeros.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'01'</code> to <code class=\"docutils literal notranslate\">'12'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>M</p></td>\n<td><p>Month, textual, 3 letters.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'Jan'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>n</p></td>\n<td><p>Month without leading zeros.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'1'</code> to <code class=\"docutils literal notranslate\">'12'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>N</p></td>\n<td><p>Month abbreviation in Associated Press\nstyle. Proprietary extension.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'Jan.'</code>, <code class=\"docutils literal notranslate\">'Feb.'</code>, <code class=\"docutils literal notranslate\">'March'</code>, <code class=\"docutils literal notranslate\">'May'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>o</p></td>\n<td><p>ISO-8601 week-numbering year,\ncorresponding to the ISO-8601 week\nnumber (W) which uses leap weeks. See Y\nfor the more common year format.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'1999'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>O</p></td>\n<td><p>Difference to Greenwich time in hours.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'+0200'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>P</p></td>\n<td><p>Time, in 12-hour hours, minutes and\n'a.m.'/'p.m.', with minutes left off\nif they're zero and the special-case\nstrings 'midnight' and 'noon' if\nappropriate. Proprietary extension.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'1 a.m.'</code>, <code class=\"docutils literal notranslate\">'1:30 p.m.'</code>, <code class=\"docutils literal notranslate\">'midnight'</code>, <code class=\"docutils literal notranslate\">'noon'</code>, <code class=\"docutils literal notranslate\">'12:30 p.m.'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>r</p></td>\n<td><p><span class=\"target\" id=\"index-0\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc5322.html\"><strong>RFC 5322</strong></a> formatted date.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'Thu, 21 Dec 2000 16:01:07 +0200'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>s</p></td>\n<td><p>Seconds, 2 digits with leading zeros.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'00'</code> to <code class=\"docutils literal notranslate\">'59'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>S</p></td>\n<td><p>English ordinal suffix for day of the\nmonth, 2 characters.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'st'</code>, <code class=\"docutils literal notranslate\">'nd'</code>, <code class=\"docutils literal notranslate\">'rd'</code> or <code class=\"docutils literal notranslate\">'th'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>t</p></td>\n<td><p>Number of days in the given month.</p></td>\n<td><p><code class=\"docutils literal notranslate\">28</code> to <code class=\"docutils literal notranslate\">31</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>T</p></td>\n<td><p>Time zone of this machine.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'EST'</code>, <code class=\"docutils literal notranslate\">'MDT'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>u</p></td>\n<td><p>Microseconds.</p></td>\n<td><p><code class=\"docutils literal notranslate\">000000</code> to <code class=\"docutils literal notranslate\">999999</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>U</p></td>\n<td><p>Seconds since the Unix Epoch\n(January 1 1970 00:00:00 UTC).</p></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p>w</p></td>\n<td><p>Day of the week, digits without\nleading zeros.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'0'</code> (Sunday) to <code class=\"docutils literal notranslate\">'6'</code> (Saturday)</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>W</p></td>\n<td><p>ISO-8601 week number of year, with\nweeks starting on Monday.</p></td>\n<td><p><code class=\"docutils literal notranslate\">1</code>, <code class=\"docutils literal notranslate\">53</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>y</p></td>\n<td><p>Year, 2 digits.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'99'</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>Y</p></td>\n<td><p>Year, 4 digits.</p></td>\n<td><p><code class=\"docutils literal notranslate\">'1999'</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p>z</p></td>\n<td><p>Day of the year.</p></td>\n<td><p><code class=\"docutils literal notranslate\">0</code> to <code class=\"docutils literal notranslate\">365</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>Z</p></td>\n<td><p>Time zone offset in seconds. The\noffset for timezones west of UTC is\nalways negative, and for those east of\nUTC is always positive.</p></td>\n<td><p><code class=\"docutils literal notranslate\">-43200</code> to <code class=\"docutils literal notranslate\">43200</code></p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;D d M Y&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is a <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">datetime</code></a> object (e.g., the result of\n<code class=\"docutils literal notranslate\">datetime.datetime.now()</code>), the output will be the string\n<code class=\"docutils literal notranslate\">'Wed 09 Jan 2008'</code>.</p>\n<p>The format passed can be one of the predefined ones <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-DATE_FORMAT\"><code class=\"xref std std-setting docutils literal notranslate\">DATE_FORMAT</code></a>,\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-DATETIME_FORMAT\"><code class=\"xref std std-setting docutils literal notranslate\">DATETIME_FORMAT</code></a>, <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-SHORT_DATE_FORMAT\"><code class=\"xref std std-setting docutils literal notranslate\">SHORT_DATE_FORMAT</code></a> or\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-SHORT_DATETIME_FORMAT\"><code class=\"xref std std-setting docutils literal notranslate\">SHORT_DATETIME_FORMAT</code></a>, or a custom format that uses the format\nspecifiers shown in the table above. Note that predefined formats may vary\ndepending on the current locale.</p>\n<p>Assuming that <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-USE_L10N\"><code class=\"xref std std-setting docutils literal notranslate\">USE_L10N</code></a> is <code class=\"docutils literal notranslate\">True</code> and <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-LANGUAGE_CODE\"><code class=\"xref std std-setting docutils literal notranslate\">LANGUAGE_CODE</code></a> is,\nfor example, <code class=\"docutils literal notranslate\">&quot;es&quot;</code>, then for:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;SHORT_DATE_FORMAT&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>the output would be the string <code class=\"docutils literal notranslate\">&quot;09/01/2008&quot;</code> (the <code class=\"docutils literal notranslate\">&quot;SHORT_DATE_FORMAT&quot;</code>\nformat specifier for the <code class=\"docutils literal notranslate\">es</code> locale as shipped with Django is <code class=\"docutils literal notranslate\">&quot;d/m/Y&quot;</code>).</p>\n<p>When used without a format string, the <code class=\"docutils literal notranslate\">DATE_FORMAT</code> format specifier is\nused. Assuming the same settings as the previous example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">date</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>outputs <code class=\"docutils literal notranslate\">9 de Enero de 2008</code> (the <code class=\"docutils literal notranslate\">DATE_FORMAT</code> format specifier for the\n<code class=\"docutils literal notranslate\">es</code> locale is <code class=\"docutils literal notranslate\">r'j \\d\\e F \\d\\e Y'</code>.</p>\n<p>You can combine <code class=\"docutils literal notranslate\">date</code> with the <a class=\"reference internal\" href=\"#std-templatefilter-time\"><code class=\"xref std std-tfilter docutils literal notranslate\">time</code></a> filter to render a full\nrepresentation of a <code class=\"docutils literal notranslate\">datetime</code> value. E.g.:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;D d M Y&quot;</span> <span class=\"cp\">}}</span> <span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">time</span><span class=\"s2\">:&quot;H:i&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n</section>\n<section id=\"default\">\n<span id=\"std-templatefilter-default\"></span><h3><code class=\"docutils literal notranslate\">default</code><a class=\"heading-anchor\" href=\"#default\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If value evaluates to <code class=\"docutils literal notranslate\">False</code>, uses the given default. Otherwise, uses the\nvalue.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">default</span><span class=\"s2\">:&quot;nothing&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;&quot;</code> (the empty string), the output will be <code class=\"docutils literal notranslate\">nothing</code>.</p>\n</section>\n<section id=\"default-if-none\">\n<span id=\"std-templatefilter-default_if_none\"></span><h3><code class=\"docutils literal notranslate\">default_if_none</code><a class=\"heading-anchor\" href=\"#default-if-none\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If (and only if) value is <code class=\"docutils literal notranslate\">None</code>, uses the given default. Otherwise, uses the\nvalue.</p>\n<p>Note that if an empty string is given, the default value will <em>not</em> be used.\nUse the <a class=\"reference internal\" href=\"#std-templatefilter-default\"><code class=\"xref std std-tfilter docutils literal notranslate\">default</code></a> filter if you want to fallback for empty strings.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">default_if_none</span><span class=\"s2\">:&quot;nothing&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">None</code>, the output will be <code class=\"docutils literal notranslate\">nothing</code>.</p>\n</section>\n<section id=\"dictsort\">\n<span id=\"std-templatefilter-dictsort\"></span><h3><code class=\"docutils literal notranslate\">dictsort</code><a class=\"heading-anchor\" href=\"#dictsort\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Takes a list of dictionaries and returns that list sorted by the key given in\nthe argument.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">dictsort</span><span class=\"s2\">:&quot;name&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"p\">[</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;zed&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;age&#39;</span><span class=\"p\">:</span> <span class=\"mi\">19</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;amy&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;age&#39;</span><span class=\"p\">:</span> <span class=\"mi\">22</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;joe&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;age&#39;</span><span class=\"p\">:</span> <span class=\"mi\">31</span><span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>then the output would be:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"p\">[</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;amy&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;age&#39;</span><span class=\"p\">:</span> <span class=\"mi\">22</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;joe&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;age&#39;</span><span class=\"p\">:</span> <span class=\"mi\">31</span><span class=\"p\">},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;zed&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;age&#39;</span><span class=\"p\">:</span> <span class=\"mi\">19</span><span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>You can also do more complicated things like:</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\">for</span> <span class=\"nv\">book</span> <span class=\"k\">in</span> <span class=\"nv\">books</span><span class=\"o\">|</span><span class=\"nf\">dictsort</span><span class=\"s2\">:&quot;author.age&quot;</span> <span class=\"cp\">%}</span>\n    * <span class=\"cp\">{{</span> <span class=\"nv\">book.title</span> <span class=\"cp\">}}</span> (<span class=\"cp\">{{</span> <span class=\"nv\">book.author.name</span> <span class=\"cp\">}}</span>)\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">books</code> is:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"p\">[</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;title&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;1984&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;author&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;George&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;age&#39;</span><span class=\"p\">:</span> <span class=\"mi\">45</span><span class=\"p\">}},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;title&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Timequake&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;author&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Kurt&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;age&#39;</span><span class=\"p\">:</span> <span class=\"mi\">75</span><span class=\"p\">}},</span>\n    <span class=\"p\">{</span><span class=\"s1\">&#39;title&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Alice&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;author&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Lewis&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;age&#39;</span><span class=\"p\">:</span> <span class=\"mi\">33</span><span class=\"p\">}},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>then the output would be:</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>* Alice (Lewis)\n* 1984 (George)\n* Timequake (Kurt)\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">dictsort</code> can also order a list of lists (or any other object implementing\n<code class=\"docutils literal notranslate\">__getitem__()</code>) by elements at specified index. For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">dictsort</span><span class=\"o\">:</span><span class=\"m\">0</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"p\">[</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;a&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;42&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;c&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;string&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;b&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;foo&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>then the output would be:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"p\">[</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;a&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;42&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;b&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;foo&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;c&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;string&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>You must pass the index as an integer rather than a string. The following\nproduce empty output:</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\">values</span><span class=\"o\">|</span><span class=\"nf\">dictsort</span><span class=\"s2\">:&quot;0&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n</section>\n<section id=\"dictsortreversed\">\n<span id=\"std-templatefilter-dictsortreversed\"></span><h3><code class=\"docutils literal notranslate\">dictsortreversed</code><a class=\"heading-anchor\" href=\"#dictsortreversed\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Takes a list of dictionaries and returns that list sorted in reverse order by\nthe key given in the argument. This works exactly the same as the above filter,\nbut the returned value will be in reverse order.</p>\n</section>\n<section id=\"divisibleby\">\n<span id=\"std-templatefilter-divisibleby\"></span><h3><code class=\"docutils literal notranslate\">divisibleby</code><a class=\"heading-anchor\" href=\"#divisibleby\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns <code class=\"docutils literal notranslate\">True</code> if the value is divisible by the argument.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">divisibleby</span><span class=\"s2\">:&quot;3&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">21</code>, the output would be <code class=\"docutils literal notranslate\">True</code>.</p>\n</section>\n<section id=\"escape\">\n<span id=\"std-templatefilter-escape\"></span><h3><code class=\"docutils literal notranslate\">escape</code><a class=\"heading-anchor\" href=\"#escape\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Escapes a string's HTML. Specifically, it makes these replacements:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">&lt;</code> is converted to <code class=\"docutils literal notranslate\">&amp;lt;</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">&gt;</code> is converted to <code class=\"docutils literal notranslate\">&amp;gt;</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">'</code> (single quote) is converted to <code class=\"docutils literal notranslate\">&amp;#39;</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">&quot;</code> (double quote) is converted to <code class=\"docutils literal notranslate\">&amp;quot;</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">&amp;</code> is converted to <code class=\"docutils literal notranslate\">&amp;amp;</code></p></li>\n</ul>\n<p>Applying <code class=\"docutils literal notranslate\">escape</code> to a variable that would normally have auto-escaping\napplied to the result will only result in one round of escaping being done. So\nit is safe to use this function even in auto-escaping environments. If you want\nmultiple escaping passes to be applied, use the <a class=\"reference internal\" href=\"#std-templatefilter-force_escape\"><code class=\"xref std std-tfilter docutils literal notranslate\">force_escape</code></a> filter.</p>\n<p>For example, you can apply <code class=\"docutils literal notranslate\">escape</code> to fields when <a class=\"reference internal\" href=\"#std-templatetag-autoescape\"><code class=\"xref std std-ttag docutils literal notranslate\">autoescape</code></a> is off:</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\">autoescape</span> <span class=\"nv\">off</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">title</span><span class=\"o\">|</span><span class=\"nf\">escape</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endautoescape</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"escapejs\">\n<span id=\"std-templatefilter-escapejs\"></span><h3><code class=\"docutils literal notranslate\">escapejs</code><a class=\"heading-anchor\" href=\"#escapejs\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Escapes characters for use in JavaScript strings. This does <em>not</em> make the\nstring safe for use in HTML or JavaScript template literals, but does protect\nyou from syntax errors when using templates to generate JavaScript/JSON.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">escapejs</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;testing\\r\\njavascript \\'string&quot; &lt;b&gt;escaping&lt;/b&gt;&quot;</code>,\nthe output will be <code class=\"docutils literal notranslate\">&quot;testing\\\\u000D\\\\u000Ajavascript \\\\u0027string\\\\u0022 \\\\u003Cb\\\\u003Eescaping\\\\u003C/b\\\\u003E&quot;</code>.</p>\n</section>\n<section id=\"filesizeformat\">\n<span id=\"std-templatefilter-filesizeformat\"></span><h3><code class=\"docutils literal notranslate\">filesizeformat</code><a class=\"heading-anchor\" href=\"#filesizeformat\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Formats the value like a 'human-readable' file size (i.e. <code class=\"docutils literal notranslate\">'13 KB'</code>,\n<code class=\"docutils literal notranslate\">'4.1 MB'</code>, <code class=\"docutils literal notranslate\">'102 bytes'</code>, etc.).</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">filesizeformat</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is 123456789, the output would be <code class=\"docutils literal notranslate\">117.7 MB</code>.</p>\n<aside class=\"admonition-file-sizes-and-si-units admonition\" role=\"note\">\n<p class=\"admonition-title\">File sizes and SI units</p>\n<p>Strictly speaking, <code class=\"docutils literal notranslate\">filesizeformat</code> does not conform to the International\nSystem of Units which recommends using KiB, MiB, GiB, etc. when byte sizes\nare calculated in powers of 1024 (which is the case here). Instead, Django\nuses traditional unit names (KB, MB, GB, etc.) corresponding to names that\nare more commonly used.</p>\n</aside>\n</section>\n<section id=\"first\">\n<span id=\"std-templatefilter-first\"></span><h3><code class=\"docutils literal notranslate\">first</code><a class=\"heading-anchor\" href=\"#first\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns the first item in a list.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">first</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is the list <code class=\"docutils literal notranslate\">['a', 'b', 'c']</code>, the output will be <code class=\"docutils literal notranslate\">'a'</code>.</p>\n</section>\n<section id=\"floatformat\">\n<span id=\"std-templatefilter-floatformat\"></span><h3><code class=\"docutils literal notranslate\">floatformat</code><a class=\"heading-anchor\" href=\"#floatformat\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When used without an argument, rounds a floating-point number to one decimal\nplace -- but only if there's a decimal part to be displayed. For example:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p><code class=\"docutils literal notranslate\">value</code></p></th>\n<th class=\"head\"><p>Template</p></th>\n<th class=\"head\"><p>Output</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">34.23234</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34.2</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">34.00000</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">34.26000</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34.3</code></p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>If used with a numeric integer argument, <code class=\"docutils literal notranslate\">floatformat</code> rounds a number to\nthat many decimal places. For example:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p><code class=\"docutils literal notranslate\">value</code></p></th>\n<th class=\"head\"><p>Template</p></th>\n<th class=\"head\"><p>Output</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">34.23234</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat:3 }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34.232</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">34.00000</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat:3 }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34.000</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">34.26000</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat:3 }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34.260</code></p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>Particularly useful is passing 0 (zero) as the argument which will round the\nfloat to the nearest integer.</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p><code class=\"docutils literal notranslate\">value</code></p></th>\n<th class=\"head\"><p>Template</p></th>\n<th class=\"head\"><p>Output</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">34.23234</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat:&quot;0&quot; }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">34.00000</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat:&quot;0&quot; }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">39.56000</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat:&quot;0&quot; }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">40</code></p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>If the argument passed to <code class=\"docutils literal notranslate\">floatformat</code> is negative, it will round a number\nto that many decimal places -- but only if there's a decimal part to be\ndisplayed. For example:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p><code class=\"docutils literal notranslate\">value</code></p></th>\n<th class=\"head\"><p>Template</p></th>\n<th class=\"head\"><p>Output</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">34.23234</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat:&quot;-3&quot; }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34.232</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">34.00000</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat:&quot;-3&quot; }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">34.26000</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">{{ value|floatformat:&quot;-3&quot; }}</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">34.260</code></p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>Using <code class=\"docutils literal notranslate\">floatformat</code> with no argument is equivalent to using <code class=\"docutils literal notranslate\">floatformat</code>\nwith an argument of <code class=\"docutils literal notranslate\">-1</code>.</p>\n</section>\n<section id=\"force-escape\">\n<span id=\"std-templatefilter-force_escape\"></span><h3><code class=\"docutils literal notranslate\">force_escape</code><a class=\"heading-anchor\" href=\"#force-escape\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Applies HTML escaping to a string (see the <a class=\"reference internal\" href=\"#std-templatefilter-escape\"><code class=\"xref std std-tfilter docutils literal notranslate\">escape</code></a> filter for\ndetails). This filter is applied <em>immediately</em> and returns a new, escaped\nstring. This is useful in the rare cases where you need multiple escaping or\nwant to apply other filters to the escaped results. Normally, you want to use\nthe <a class=\"reference internal\" href=\"#std-templatefilter-escape\"><code class=\"xref std std-tfilter docutils literal notranslate\">escape</code></a> filter.</p>\n<p>For example, if you want to catch the <code class=\"docutils literal notranslate\">&lt;p&gt;</code> HTML elements created by\nthe <a class=\"reference internal\" href=\"#std-templatefilter-linebreaks\"><code class=\"xref std std-tfilter docutils literal notranslate\">linebreaks</code></a> filter:</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\">autoescape</span> <span class=\"nv\">off</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">body</span><span class=\"o\">|</span><span class=\"nf\">linebreaks</span><span class=\"o\">|</span><span class=\"nf\">force_escape</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endautoescape</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"get-digit\">\n<span id=\"std-templatefilter-get_digit\"></span><h3><code class=\"docutils literal notranslate\">get_digit</code><a class=\"heading-anchor\" href=\"#get-digit\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Given a whole number, returns the requested digit, where 1 is the right-most\ndigit, 2 is the second-right-most digit, etc. Returns the original value for\ninvalid input (if input or argument is not an integer, or if argument is less\nthan 1). Otherwise, output is always an integer.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">get_digit</span><span class=\"s2\">:&quot;2&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">123456789</code>, the output will be <code class=\"docutils literal notranslate\">8</code>.</p>\n</section>\n<section id=\"iriencode\">\n<span id=\"std-templatefilter-iriencode\"></span><h3><code class=\"docutils literal notranslate\">iriencode</code><a class=\"heading-anchor\" href=\"#iriencode\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Converts an IRI (Internationalized Resource Identifier) to a string that is\nsuitable for including in a URL. This is necessary if you're trying to use\nstrings containing non-ASCII characters in a URL.</p>\n<p>It's safe to use this filter on a string that has already gone through the\n<a class=\"reference internal\" href=\"#std-templatefilter-urlencode\"><code class=\"xref std std-tfilter docutils literal notranslate\">urlencode</code></a> filter.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">iriencode</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;?test=1&amp;me=2&quot;</code>, the output will be <code class=\"docutils literal notranslate\">&quot;?test=1&amp;amp;me=2&quot;</code>.</p>\n</section>\n<section id=\"join\">\n<span id=\"std-templatefilter-join\"></span><h3><code class=\"docutils literal notranslate\">join</code><a class=\"heading-anchor\" href=\"#join\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Joins a list with a string, like Python's <code class=\"docutils literal notranslate\">str.join(list)</code></p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">join</span><span class=\"s2\">:&quot; // &quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is the list <code class=\"docutils literal notranslate\">['a', 'b', 'c']</code>, the output will be the string\n<code class=\"docutils literal notranslate\">&quot;a // b // c&quot;</code>.</p>\n</section>\n<section id=\"last\">\n<span id=\"std-templatefilter-last\"></span><h3><code class=\"docutils literal notranslate\">last</code><a class=\"heading-anchor\" href=\"#last\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns the last item in a list.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">last</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is the list <code class=\"docutils literal notranslate\">['a', 'b', 'c', 'd']</code>, the output will be the\nstring <code class=\"docutils literal notranslate\">&quot;d&quot;</code>.</p>\n</section>\n<section id=\"length\">\n<span id=\"std-templatefilter-length\"></span><h3><code class=\"docutils literal notranslate\">length</code><a class=\"heading-anchor\" href=\"#length\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns the length of the value. This works for both strings and lists.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">length</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">['a', 'b', 'c', 'd']</code> or <code class=\"docutils literal notranslate\">&quot;abcd&quot;</code>, the output will be\n<code class=\"docutils literal notranslate\">4</code>.</p>\n<p>The filter returns <code class=\"docutils literal notranslate\">0</code> for an undefined variable.</p>\n</section>\n<section id=\"length-is\">\n<span id=\"std-templatefilter-length_is\"></span><h3><code class=\"docutils literal notranslate\">length_is</code><a class=\"heading-anchor\" href=\"#length-is\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns <code class=\"docutils literal notranslate\">True</code> if the value's length is the argument, or <code class=\"docutils literal notranslate\">False</code> otherwise.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">length_is</span><span class=\"s2\">:&quot;4&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">['a', 'b', 'c', 'd']</code> or <code class=\"docutils literal notranslate\">&quot;abcd&quot;</code>, the output will be\n<code class=\"docutils literal notranslate\">True</code>.</p>\n</section>\n<section id=\"linebreaks\">\n<span id=\"std-templatefilter-linebreaks\"></span><h3><code class=\"docutils literal notranslate\">linebreaks</code><a class=\"heading-anchor\" href=\"#linebreaks\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Replaces line breaks in plain text with appropriate HTML; a single\nnewline becomes an HTML line break (<code class=\"docutils literal notranslate\">&lt;br /&gt;</code>) and a new line\nfollowed by a blank line becomes a paragraph break (<code class=\"docutils literal notranslate\">&lt;/p&gt;</code>).</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">linebreaks</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">Joel\\nis a slug</code>, the output will be <code class=\"docutils literal notranslate\">&lt;p&gt;Joel&lt;br /&gt;is a\nslug&lt;/p&gt;</code>.</p>\n</section>\n<section id=\"linebreaksbr\">\n<span id=\"std-templatefilter-linebreaksbr\"></span><h3><code class=\"docutils literal notranslate\">linebreaksbr</code><a class=\"heading-anchor\" href=\"#linebreaksbr\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Converts all newlines in a piece of plain text to HTML line breaks\n(<code class=\"docutils literal notranslate\">&lt;br /&gt;</code>).</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">linebreaksbr</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">Joel\\nis a slug</code>, the output will be <code class=\"docutils literal notranslate\">Joel&lt;br /&gt;is a\nslug</code>.</p>\n</section>\n<section id=\"linenumbers\">\n<span id=\"std-templatefilter-linenumbers\"></span><h3><code class=\"docutils literal notranslate\">linenumbers</code><a class=\"heading-anchor\" href=\"#linenumbers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Displays text with line numbers.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">linenumbers</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is:</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>one\ntwo\nthree\n</code></pre></div>\n<p>the output will be:</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>1. one\n2. two\n3. three\n</code></pre></div>\n</section>\n<section id=\"ljust\">\n<span id=\"std-templatefilter-ljust\"></span><h3><code class=\"docutils literal notranslate\">ljust</code><a class=\"heading-anchor\" href=\"#ljust\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Left-aligns the value in a field of a given width.</p>\n<p><strong>Argument:</strong> field size</p>\n<p>For example:</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>&quot;<span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">ljust</span><span class=\"s2\">:&quot;10&quot;</span> <span class=\"cp\">}}</span>&quot;\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">Django</code>, the output will be <code class=\"docutils literal notranslate\">&quot;Django&#160;&#160;&#160; &quot;</code>.</p>\n</section>\n<section id=\"lower\">\n<span id=\"std-templatefilter-lower\"></span><h3><code class=\"docutils literal notranslate\">lower</code><a class=\"heading-anchor\" href=\"#lower\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Converts a string into all lowercase.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">lower</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">Totally LOVING this Album!</code>, the output will be\n<code class=\"docutils literal notranslate\">totally loving this album!</code>.</p>\n</section>\n<section id=\"make-list\">\n<span id=\"std-templatefilter-make_list\"></span><h3><code class=\"docutils literal notranslate\">make_list</code><a class=\"heading-anchor\" href=\"#make-list\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns the value turned into a list. For a string, it's a list of characters.\nFor an integer, the argument is cast to a string before creating a list.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">make_list</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is the string <code class=\"docutils literal notranslate\">&quot;Joel&quot;</code>, the output would be the list\n<code class=\"docutils literal notranslate\">['J', 'o', 'e', 'l']</code>. If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">123</code>, the output will be the\nlist <code class=\"docutils literal notranslate\">['1', '2', '3']</code>.</p>\n</section>\n<section id=\"phone2numeric\">\n<span id=\"std-templatefilter-phone2numeric\"></span><h3><code class=\"docutils literal notranslate\">phone2numeric</code><a class=\"heading-anchor\" href=\"#phone2numeric\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Converts a phone number (possibly containing letters) to its numerical\nequivalent.</p>\n<p>The input doesn't have to be a valid phone number. This will happily convert\nany string.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">phone2numeric</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">800-COLLECT</code>, the output will be <code class=\"docutils literal notranslate\">800-2655328</code>.</p>\n</section>\n<section id=\"pluralize\">\n<span id=\"std-templatefilter-pluralize\"></span><h3><code class=\"docutils literal notranslate\">pluralize</code><a class=\"heading-anchor\" href=\"#pluralize\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns a plural suffix if the value is not 1. By default, this suffix is\n<code class=\"docutils literal notranslate\">'s'</code>.</p>\n<p>Example:</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>You have <span class=\"cp\">{{</span> <span class=\"nv\">num_messages</span> <span class=\"cp\">}}</span> message<span class=\"cp\">{{</span> <span class=\"nv\">num_messages</span><span class=\"o\">|</span><span class=\"nf\">pluralize</span> <span class=\"cp\">}}</span>.\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">num_messages</code> is <code class=\"docutils literal notranslate\">1</code>, the output will be <code class=\"docutils literal notranslate\">You have 1 message.</code>\nIf <code class=\"docutils literal notranslate\">num_messages</code> is <code class=\"docutils literal notranslate\">2</code>  the output will be <code class=\"docutils literal notranslate\">You have 2 messages.</code></p>\n<p>For words that require a suffix other than <code class=\"docutils literal notranslate\">'s'</code>, you can provide an alternate\nsuffix as a parameter to the filter.</p>\n<p>Example:</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>You have <span class=\"cp\">{{</span> <span class=\"nv\">num_walruses</span> <span class=\"cp\">}}</span> walrus<span class=\"cp\">{{</span> <span class=\"nv\">num_walruses</span><span class=\"o\">|</span><span class=\"nf\">pluralize</span><span class=\"s2\">:&quot;es&quot;</span> <span class=\"cp\">}}</span>.\n</code></pre></div>\n<p>For words that don't pluralize by simple suffix, you can specify both a\nsingular and plural suffix, separated by a comma.</p>\n<p>Example:</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>You have <span class=\"cp\">{{</span> <span class=\"nv\">num_cherries</span> <span class=\"cp\">}}</span> cherr<span class=\"cp\">{{</span> <span class=\"nv\">num_cherries</span><span class=\"o\">|</span><span class=\"nf\">pluralize</span><span class=\"s2\">:&quot;y,ies&quot;</span> <span class=\"cp\">}}</span>.\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>Use <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/i18n/translation/#std-templatetag-blocktrans\"><code class=\"xref std std-ttag docutils literal notranslate\">blocktrans</code></a> to pluralize translated strings.</p>\n</aside>\n</section>\n<section id=\"pprint\">\n<span id=\"std-templatefilter-pprint\"></span><h3><code class=\"docutils literal notranslate\">pprint</code><a class=\"heading-anchor\" href=\"#pprint\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>A wrapper around <a class=\"reference external\" href=\"https://docs.python.org/3/library/pprint.html#pprint.pprint\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">pprint.pprint()</code></a> -- for debugging, really.</p>\n</section>\n<section id=\"random\">\n<span id=\"std-templatefilter-random\"></span><h3><code class=\"docutils literal notranslate\">random</code><a class=\"heading-anchor\" href=\"#random\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns a random item from the given list.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">random</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is the list <code class=\"docutils literal notranslate\">['a', 'b', 'c', 'd']</code>, the output could be <code class=\"docutils literal notranslate\">&quot;b&quot;</code>.</p>\n</section>\n<section id=\"rjust\">\n<span id=\"std-templatefilter-rjust\"></span><h3><code class=\"docutils literal notranslate\">rjust</code><a class=\"heading-anchor\" href=\"#rjust\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Right-aligns the value in a field of a given width.</p>\n<p><strong>Argument:</strong> field size</p>\n<p>For example:</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>&quot;<span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">rjust</span><span class=\"s2\">:&quot;10&quot;</span> <span class=\"cp\">}}</span>&quot;\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">Django</code>, the output will be <code class=\"docutils literal notranslate\">&quot;&#160;&#160;&#160; Django&quot;</code>.</p>\n</section>\n<section id=\"safe\">\n<span id=\"std-templatefilter-safe\"></span><h3><code class=\"docutils literal notranslate\">safe</code><a class=\"heading-anchor\" href=\"#safe\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Marks a string as not requiring further HTML escaping prior to output. When\nautoescaping is off, this filter has no effect.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>If you are chaining filters, a filter applied after <code class=\"docutils literal notranslate\">safe</code> can\nmake the contents unsafe again. For example, the following code\nprints the variable as is, unescaped:</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\">var</span><span class=\"o\">|</span><span class=\"nf\">safe</span><span class=\"o\">|</span><span class=\"nf\">escape</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n</aside>\n</section>\n<section id=\"safeseq\">\n<span id=\"std-templatefilter-safeseq\"></span><h3><code class=\"docutils literal notranslate\">safeseq</code><a class=\"heading-anchor\" href=\"#safeseq\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Applies the <a class=\"reference internal\" href=\"#std-templatefilter-safe\"><code class=\"xref std std-tfilter docutils literal notranslate\">safe</code></a> filter to each element of a sequence. Useful in\nconjunction with other filters that operate on sequences, such as\n<a class=\"reference internal\" href=\"#std-templatefilter-join\"><code class=\"xref std std-tfilter docutils literal notranslate\">join</code></a>. For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">some_list</span><span class=\"o\">|</span><span class=\"nf\">safeseq</span><span class=\"o\">|</span><span class=\"nf\">join</span><span class=\"s2\">:&quot;, &quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>You couldn't use the <a class=\"reference internal\" href=\"#std-templatefilter-safe\"><code class=\"xref std std-tfilter docutils literal notranslate\">safe</code></a> filter directly in this case, as it would\nfirst convert the variable into a string, rather than working with the\nindividual elements of the sequence.</p>\n</section>\n<section id=\"slice\">\n<span id=\"std-templatefilter-slice\"></span><h3><code class=\"docutils literal notranslate\">slice</code><a class=\"heading-anchor\" href=\"#slice\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns a slice of the list.</p>\n<p>Uses the same syntax as Python's list slicing. See\n<a class=\"reference external\" href=\"http://www.diveintopython3.net/native-datatypes.html#slicinglists\">http://www.diveintopython3.net/native-datatypes.html#slicinglists</a>\nfor an introduction.</p>\n<p>Example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">some_list</span><span class=\"o\">|</span><span class=\"nf\">slice</span><span class=\"s2\">:&quot;:2&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">some_list</code> is <code class=\"docutils literal notranslate\">['a', 'b', 'c']</code>, the output will be <code class=\"docutils literal notranslate\">['a', 'b']</code>.</p>\n</section>\n<section id=\"slugify\">\n<span id=\"std-templatefilter-slugify\"></span><h3><code class=\"docutils literal notranslate\">slugify</code><a class=\"heading-anchor\" href=\"#slugify\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Converts to ASCII. Converts spaces to hyphens. Removes characters that aren't\nalphanumerics, underscores, or hyphens. Converts to lowercase. Also strips\nleading and trailing whitespace.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">slugify</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;Joel is a slug&quot;</code>, the output will be <code class=\"docutils literal notranslate\">&quot;joel-is-a-slug&quot;</code>.</p>\n</section>\n<section id=\"stringformat\">\n<span id=\"std-templatefilter-stringformat\"></span><h3><code class=\"docutils literal notranslate\">stringformat</code><a class=\"heading-anchor\" href=\"#stringformat\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Formats the variable according to the argument, a string formatting specifier.\nThis specifier uses the <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#old-string-formatting\" title=\"(in Python v3.14)\"><span>printf-style String Formatting</span></a> syntax, with the exception\nthat the leading &quot;%&quot; is dropped.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">stringformat</span><span class=\"s2\">:&quot;E&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">10</code>, the output will be <code class=\"docutils literal notranslate\">1.000000E+01</code>.</p>\n</section>\n<section id=\"striptags\">\n<span id=\"std-templatefilter-striptags\"></span><h3><code class=\"docutils literal notranslate\">striptags</code><a class=\"heading-anchor\" href=\"#striptags\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Makes all possible efforts to strip all [X]HTML tags.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">striptags</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;&lt;b&gt;Joel&lt;/b&gt; &lt;button&gt;is&lt;/button&gt; a &lt;span&gt;slug&lt;/span&gt;&quot;</code>, the\noutput will be <code class=\"docutils literal notranslate\">&quot;Joel is a slug&quot;</code>.</p>\n<aside class=\"admonition-no-safety-guarantee admonition\" role=\"note\">\n<p class=\"admonition-title\">No safety guarantee</p>\n<p>Note that <code class=\"docutils literal notranslate\">striptags</code> doesn't give any guarantee about its output being\nHTML safe, particularly with non valid HTML input. So <strong>NEVER</strong> apply the\n<code class=\"docutils literal notranslate\">safe</code> filter to a <code class=\"docutils literal notranslate\">striptags</code> output. If you are looking for something\nmore robust, you can use the <code class=\"docutils literal notranslate\">bleach</code> Python library, notably its\n<a class=\"reference external\" href=\"https://bleach.readthedocs.io/en/latest/clean.html\">clean</a> method.</p>\n</aside>\n</section>\n<section id=\"time\">\n<span id=\"std-templatefilter-time\"></span><h3><code class=\"docutils literal notranslate\">time</code><a class=\"heading-anchor\" href=\"#time\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Formats a time according to the given format.</p>\n<p>Given format can be the predefined one <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-TIME_FORMAT\"><code class=\"xref std std-setting docutils literal notranslate\">TIME_FORMAT</code></a>, or a custom\nformat, same as the <a class=\"reference internal\" href=\"#std-templatefilter-date\"><code class=\"xref std std-tfilter docutils literal notranslate\">date</code></a> filter. Note that the predefined format\nis locale-dependent.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">time</span><span class=\"s2\">:&quot;H:i&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is equivalent to <code class=\"docutils literal notranslate\">datetime.datetime.now()</code>, the output will be\nthe string <code class=\"docutils literal notranslate\">&quot;01:23&quot;</code>.</p>\n<p>Another example:</p>\n<p>Assuming that <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-USE_L10N\"><code class=\"xref std std-setting docutils literal notranslate\">USE_L10N</code></a> is <code class=\"docutils literal notranslate\">True</code> and <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-LANGUAGE_CODE\"><code class=\"xref std std-setting docutils literal notranslate\">LANGUAGE_CODE</code></a> is,\nfor example, <code class=\"docutils literal notranslate\">&quot;de&quot;</code>, then for:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">time</span><span class=\"s2\">:&quot;TIME_FORMAT&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>the output will be the string <code class=\"docutils literal notranslate\">&quot;01:23&quot;</code> (The <code class=\"docutils literal notranslate\">&quot;TIME_FORMAT&quot;</code> format\nspecifier for the <code class=\"docutils literal notranslate\">de</code> locale as shipped with Django is <code class=\"docutils literal notranslate\">&quot;H:i&quot;</code>).</p>\n<p>The <code class=\"docutils literal notranslate\">time</code> filter will only accept parameters in the format string that\nrelate to the time of day, not the date (for obvious reasons). If you need to\nformat a <code class=\"docutils literal notranslate\">date</code> value, use the <a class=\"reference internal\" href=\"#std-templatefilter-date\"><code class=\"xref std std-tfilter docutils literal notranslate\">date</code></a> filter instead (or along\n<code class=\"docutils literal notranslate\">time</code> if you need to render a full <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">datetime</code></a> value).</p>\n<p>There is one exception the above rule: When passed a <code class=\"docutils literal notranslate\">datetime</code> value with\nattached timezone information (a <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/i18n/timezones/#naive-vs-aware-datetimes\"><span class=\"std std-ref\">time-zone-aware</span></a> <code class=\"docutils literal notranslate\">datetime</code> instance) the <code class=\"docutils literal notranslate\">time</code> filter will\naccept the timezone-related <a class=\"reference internal\" href=\"#date-and-time-formatting-specifiers\"><span class=\"std std-ref\">format specifiers</span></a> <code class=\"docutils literal notranslate\">'e'</code>, <code class=\"docutils literal notranslate\">'O'</code> , <code class=\"docutils literal notranslate\">'T'</code> and <code class=\"docutils literal notranslate\">'Z'</code>.</p>\n<p>When used without a format string, the <code class=\"docutils literal notranslate\">TIME_FORMAT</code> format specifier is\nused:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">time</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>is the same as:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">time</span><span class=\"s2\">:&quot;TIME_FORMAT&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n</section>\n<section id=\"timesince\">\n<span id=\"std-templatefilter-timesince\"></span><h3><code class=\"docutils literal notranslate\">timesince</code><a class=\"heading-anchor\" href=\"#timesince\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Formats a date as the time since that date (e.g., &quot;4 days, 6 hours&quot;).</p>\n<p>Takes an optional argument that is a variable containing the date to use as\nthe comparison point (without the argument, the comparison point is <em>now</em>).\nFor example, if <code class=\"docutils literal notranslate\">blog_date</code> is a date instance representing midnight on 1\nJune 2006, and <code class=\"docutils literal notranslate\">comment_date</code> is a date instance for 08:00 on 1 June 2006,\nthen the following would return &quot;8 hours&quot;:</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\">blog_date</span><span class=\"o\">|</span><span class=\"nf\">timesince</span><span class=\"o\">:</span><span class=\"nv\">comment_date</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>Comparing offset-naive and offset-aware datetimes will return an empty string.</p>\n<p>Minutes is the smallest unit used, and &quot;0 minutes&quot; will be returned for any\ndate that is in the future relative to the comparison point.</p>\n</section>\n<section id=\"timeuntil\">\n<span id=\"std-templatefilter-timeuntil\"></span><h3><code class=\"docutils literal notranslate\">timeuntil</code><a class=\"heading-anchor\" href=\"#timeuntil\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Similar to <code class=\"docutils literal notranslate\">timesince</code>, except that it measures the time from now until the\ngiven date or datetime. For example, if today is 1 June 2006 and\n<code class=\"docutils literal notranslate\">conference_date</code> is a date instance holding 29 June 2006, then\n<code class=\"docutils literal notranslate\">{{ conference_date|timeuntil }}</code> will return &quot;4 weeks&quot;.</p>\n<p>Takes an optional argument that is a variable containing the date to use as\nthe comparison point (instead of <em>now</em>). If <code class=\"docutils literal notranslate\">from_date</code> contains 22 June\n2006, then the following will return &quot;1 week&quot;:</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\">conference_date</span><span class=\"o\">|</span><span class=\"nf\">timeuntil</span><span class=\"o\">:</span><span class=\"nv\">from_date</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>Comparing offset-naive and offset-aware datetimes will return an empty string.</p>\n<p>Minutes is the smallest unit used, and &quot;0 minutes&quot; will be returned for any\ndate that is in the past relative to the comparison point.</p>\n</section>\n<section id=\"title\">\n<span id=\"std-templatefilter-title\"></span><h3><code class=\"docutils literal notranslate\">title</code><a class=\"heading-anchor\" href=\"#title\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Converts a string into titlecase by making words start with an uppercase\ncharacter and the remaining characters lowercase. This tag makes no effort to\nkeep &quot;trivial words&quot; in lowercase.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">title</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;my FIRST post&quot;</code>, the output will be <code class=\"docutils literal notranslate\">&quot;My First Post&quot;</code>.</p>\n</section>\n<section id=\"truncatechars\">\n<span id=\"std-templatefilter-truncatechars\"></span><h3><code class=\"docutils literal notranslate\">truncatechars</code><a class=\"heading-anchor\" href=\"#truncatechars\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Truncates a string if it is longer than the specified number of characters.\nTruncated strings will end with a translatable ellipsis sequence (&quot;...&quot;).</p>\n<p><strong>Argument:</strong> Number of characters to truncate to</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">truncatechars</span><span class=\"o\">:</span><span class=\"m\">9</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;Joel is a slug&quot;</code>, the output will be <code class=\"docutils literal notranslate\">&quot;Joel i...&quot;</code>.</p>\n</section>\n<section id=\"truncatechars-html\">\n<span id=\"std-templatefilter-truncatechars_html\"></span><h3><code class=\"docutils literal notranslate\">truncatechars_html</code><a class=\"heading-anchor\" href=\"#truncatechars-html\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Similar to <a class=\"reference internal\" href=\"#std-templatefilter-truncatechars\"><code class=\"xref std std-tfilter docutils literal notranslate\">truncatechars</code></a>, except that it is aware of HTML tags. Any\ntags that are opened in the string and not closed before the truncation point\nare closed immediately after the truncation.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">truncatechars_html</span><span class=\"o\">:</span><span class=\"m\">9</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;&lt;p&gt;Joel is a slug&lt;/p&gt;&quot;</code>, the output will be\n<code class=\"docutils literal notranslate\">&quot;&lt;p&gt;Joel i...&lt;/p&gt;&quot;</code>.</p>\n<p>Newlines in the HTML content will be preserved.</p>\n</section>\n<section id=\"truncatewords\">\n<span id=\"std-templatefilter-truncatewords\"></span><h3><code class=\"docutils literal notranslate\">truncatewords</code><a class=\"heading-anchor\" href=\"#truncatewords\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Truncates a string after a certain number of words.</p>\n<p><strong>Argument:</strong> Number of words to truncate after</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">truncatewords</span><span class=\"o\">:</span><span class=\"m\">2</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;Joel is a slug&quot;</code>, the output will be <code class=\"docutils literal notranslate\">&quot;Joel is ...&quot;</code>.</p>\n<p>Newlines within the string will be removed.</p>\n</section>\n<section id=\"truncatewords-html\">\n<span id=\"std-templatefilter-truncatewords_html\"></span><h3><code class=\"docutils literal notranslate\">truncatewords_html</code><a class=\"heading-anchor\" href=\"#truncatewords-html\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Similar to <a class=\"reference internal\" href=\"#std-templatefilter-truncatewords\"><code class=\"xref std std-tfilter docutils literal notranslate\">truncatewords</code></a>, except that it is aware of HTML tags. Any\ntags that are opened in the string and not closed before the truncation point,\nare closed immediately after the truncation.</p>\n<p>This is less efficient than <a class=\"reference internal\" href=\"#std-templatefilter-truncatewords\"><code class=\"xref std std-tfilter docutils literal notranslate\">truncatewords</code></a>, so should only be used\nwhen it is being passed HTML text.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">truncatewords_html</span><span class=\"o\">:</span><span class=\"m\">2</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;&lt;p&gt;Joel is a slug&lt;/p&gt;&quot;</code>, the output will be\n<code class=\"docutils literal notranslate\">&quot;&lt;p&gt;Joel is ...&lt;/p&gt;&quot;</code>.</p>\n<p>Newlines in the HTML content will be preserved.</p>\n</section>\n<section id=\"unordered-list\">\n<span id=\"std-templatefilter-unordered_list\"></span><h3><code class=\"docutils literal notranslate\">unordered_list</code><a class=\"heading-anchor\" href=\"#unordered-list\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Recursively takes a self-nested list and returns an HTML unordered list --\nWITHOUT opening and closing &lt;ul&gt; tags.</p>\n<p>The list is assumed to be in the proper format. For example, if <code class=\"docutils literal notranslate\">var</code>\ncontains <code class=\"docutils literal notranslate\">['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]</code>, then\n<code class=\"docutils literal notranslate\">{{ var|unordered_list }}</code> would return:</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=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>States\n<span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>Kansas\n        <span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n                <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>Lawrence<span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n                <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>Topeka<span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>Illinois<span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"upper\">\n<span id=\"std-templatefilter-upper\"></span><h3><code class=\"docutils literal notranslate\">upper</code><a class=\"heading-anchor\" href=\"#upper\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Converts a string into all uppercase.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">upper</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;Joel is a slug&quot;</code>, the output will be <code class=\"docutils literal notranslate\">&quot;JOEL IS A SLUG&quot;</code>.</p>\n</section>\n<section id=\"urlencode\">\n<span id=\"std-templatefilter-urlencode\"></span><h3><code class=\"docutils literal notranslate\">urlencode</code><a class=\"heading-anchor\" href=\"#urlencode\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Escapes a value for use in a URL.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">urlencode</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;https://www.example.org/foo?a=b&amp;c=d&quot;</code>, the output will be\n<code class=\"docutils literal notranslate\">&quot;https%3A//www.example.org/foo%3Fa%3Db%26c%3Dd&quot;</code>.</p>\n<p>An optional argument containing the characters which should not be escaped can\nbe provided.</p>\n<p>If not provided, the '/' character is assumed safe. An empty string can be\nprovided when <em>all</em> characters should be escaped. For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">urlencode</span><span class=\"s2\">:&quot;&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;https://www.example.org/&quot;</code>, the output will be\n<code class=\"docutils literal notranslate\">&quot;https%3A%2F%2Fwww.example.org%2F&quot;</code>.</p>\n</section>\n<section id=\"urlize\">\n<span id=\"std-templatefilter-urlize\"></span><h3><code class=\"docutils literal notranslate\">urlize</code><a class=\"heading-anchor\" href=\"#urlize\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Converts URLs and email addresses in text into clickable links.</p>\n<p>This template tag works on links prefixed with <code class=\"docutils literal notranslate\">http://</code>, <code class=\"docutils literal notranslate\">https://</code>, or\n<code class=\"docutils literal notranslate\">www.</code>. For example, <code class=\"docutils literal notranslate\">https://goo.gl/aia1t</code> will get converted but\n<code class=\"docutils literal notranslate\">goo.gl/aia1t</code> won't.</p>\n<p>It also supports domain-only links ending in one of the original top level\ndomains (<code class=\"docutils literal notranslate\">.com</code>, <code class=\"docutils literal notranslate\">.edu</code>, <code class=\"docutils literal notranslate\">.gov</code>, <code class=\"docutils literal notranslate\">.int</code>, <code class=\"docutils literal notranslate\">.mil</code>, <code class=\"docutils literal notranslate\">.net</code>, and\n<code class=\"docutils literal notranslate\">.org</code>). For example, <code class=\"docutils literal notranslate\">djangoproject.com</code> gets converted.</p>\n<p>Links can have trailing punctuation (periods, commas, close-parens) and leading\npunctuation (opening parens), and <code class=\"docutils literal notranslate\">urlize</code> will still do the right thing.</p>\n<p>Links generated by <code class=\"docutils literal notranslate\">urlize</code> have a <code class=\"docutils literal notranslate\">rel=&quot;nofollow&quot;</code> attribute added\nto them.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">urlize</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;Check out www.djangoproject.com&quot;</code>, the output will be\n<code class=\"docutils literal notranslate\">&quot;Check out &lt;a href=&quot;http://www.djangoproject.com&quot;\nrel=&quot;nofollow&quot;&gt;www.djangoproject.com&lt;/a&gt;&quot;</code>.</p>\n<p>In addition to web links, <code class=\"docutils literal notranslate\">urlize</code> also converts email addresses into\n<code class=\"docutils literal notranslate\">mailto:</code> links. If <code class=\"docutils literal notranslate\">value</code> is\n<code class=\"docutils literal notranslate\">&quot;Send questions to foo&#64;example.com&quot;</code>, the output will be\n<code class=\"docutils literal notranslate\">&quot;Send questions to &lt;a href=&quot;mailto:foo&#64;example.com&quot;&gt;foo&#64;example.com&lt;/a&gt;&quot;</code>.</p>\n<p>The <code class=\"docutils literal notranslate\">urlize</code> filter also takes an optional parameter <code class=\"docutils literal notranslate\">autoescape</code>. If\n<code class=\"docutils literal notranslate\">autoescape</code> is <code class=\"docutils literal notranslate\">True</code>, the link text and URLs will be escaped using\nDjango's built-in <a class=\"reference internal\" href=\"#std-templatefilter-escape\"><code class=\"xref std std-tfilter docutils literal notranslate\">escape</code></a> filter. The default value for\n<code class=\"docutils literal notranslate\">autoescape</code> is <code class=\"docutils literal notranslate\">True</code>.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>If <code class=\"docutils literal notranslate\">urlize</code> is applied to text that already contains HTML markup,\nthings won't work as expected. Apply this filter only to plain text.</p>\n</aside>\n</section>\n<section id=\"urlizetrunc\">\n<span id=\"std-templatefilter-urlizetrunc\"></span><h3><code class=\"docutils literal notranslate\">urlizetrunc</code><a class=\"heading-anchor\" href=\"#urlizetrunc\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Converts URLs and email addresses into clickable links just like <a class=\"reference internal\" href=\"#urlize\">urlize</a>, but\ntruncates URLs longer than the given character limit.</p>\n<p><strong>Argument:</strong> Number of characters that link text should be truncated to,\nincluding the ellipsis that's added if truncation is necessary.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">urlizetrunc</span><span class=\"o\">:</span><span class=\"m\">15</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;Check out www.djangoproject.com&quot;</code>, the output would be\n<code class=\"docutils literal notranslate\">'Check out &lt;a href=&quot;http://www.djangoproject.com&quot;\nrel=&quot;nofollow&quot;&gt;www.djangopr...&lt;/a&gt;'</code>.</p>\n<p>As with <a class=\"reference internal\" href=\"#urlize\">urlize</a>, this filter should only be applied to plain text.</p>\n</section>\n<section id=\"wordcount\">\n<span id=\"std-templatefilter-wordcount\"></span><h3><code class=\"docutils literal notranslate\">wordcount</code><a class=\"heading-anchor\" href=\"#wordcount\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Returns the number of words.</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">wordcount</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">&quot;Joel is a slug&quot;</code>, the output will be <code class=\"docutils literal notranslate\">4</code>.</p>\n</section>\n<section id=\"wordwrap\">\n<span id=\"std-templatefilter-wordwrap\"></span><h3><code class=\"docutils literal notranslate\">wordwrap</code><a class=\"heading-anchor\" href=\"#wordwrap\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Wraps words at specified line length.</p>\n<p><strong>Argument:</strong> number of characters at which to wrap the text</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">wordwrap</span><span class=\"o\">:</span><span class=\"m\">5</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">value</code> is <code class=\"docutils literal notranslate\">Joel is a slug</code>, the output would be:</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>Joel\nis a\nslug\n</code></pre></div>\n</section>\n<section id=\"yesno\">\n<span id=\"std-templatefilter-yesno\"></span><h3><code class=\"docutils literal notranslate\">yesno</code><a class=\"heading-anchor\" href=\"#yesno\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Maps values for <code class=\"docutils literal notranslate\">True</code>, <code class=\"docutils literal notranslate\">False</code>, and (optionally) <code class=\"docutils literal notranslate\">None</code>, to the strings\n&quot;yes&quot;, &quot;no&quot;, &quot;maybe&quot;, or a custom mapping passed as a comma-separated list, and\nreturns one of those strings according to the value:</p>\n<p>For example:</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\">value</span><span class=\"o\">|</span><span class=\"nf\">yesno</span><span class=\"s2\">:&quot;yeah,no,maybe&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Value</p></th>\n<th class=\"head\"><p>Argument</p></th>\n<th class=\"head\"><p>Outputs</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">True</code></p></td>\n<td></td>\n<td><p><code class=\"docutils literal notranslate\">yes</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">True</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">&quot;yeah,no,maybe&quot;</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">yeah</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">False</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">&quot;yeah,no,maybe&quot;</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">no</code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">None</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">&quot;yeah,no,maybe&quot;</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">maybe</code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">None</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">&quot;yeah,no&quot;</code></p></td>\n<td><p><code class=\"docutils literal notranslate\">no</code> (converts <code class=\"docutils literal notranslate\">None</code> to <code class=\"docutils literal notranslate\">False</code>\nif no mapping for <code class=\"docutils literal notranslate\">None</code> is given)</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n</section>\n</section>\n<section id=\"internationalization-tags-and-filters\">\n<h2>Internationalization tags and filters<a class=\"heading-anchor\" href=\"#internationalization-tags-and-filters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django provides template tags and filters to control each aspect of\n<a class=\"reference internal\" href=\"/zh-hans/2.0/topics/i18n/\"><span class=\"doc\">internationalization</span></a> in templates. They allow for\ngranular control of translations, formatting, and time zone conversions.</p>\n<section id=\"i18n\">\n<h3><code class=\"docutils literal notranslate\">i18n</code><a class=\"heading-anchor\" href=\"#i18n\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>This library allows specifying translatable text in templates.\nTo enable it, set <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-USE_I18N\"><code class=\"xref std std-setting docutils literal notranslate\">USE_I18N</code></a> to <code class=\"docutils literal notranslate\">True</code>, then load it with\n<code class=\"docutils literal notranslate\">{% load i18n %}</code>.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/i18n/translation/#specifying-translation-strings-in-template-code\"><span class=\"std std-ref\">Internationalization: in template code</span></a>.</p>\n</section>\n<section id=\"l10n\">\n<h3><code class=\"docutils literal notranslate\">l10n</code><a class=\"heading-anchor\" href=\"#l10n\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>This library provides control over the localization of values in templates.\nYou only need to load the library using <code class=\"docutils literal notranslate\">{% load l10n %}</code>, but you'll often\nset <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-USE_L10N\"><code class=\"xref std std-setting docutils literal notranslate\">USE_L10N</code></a> to <code class=\"docutils literal notranslate\">True</code> so that localization is active by default.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/i18n/formatting/#topic-l10n-templates\"><span class=\"std std-ref\">Controlling localization in templates</span></a>.</p>\n</section>\n<section id=\"tz\">\n<h3><code class=\"docutils literal notranslate\">tz</code><a class=\"heading-anchor\" href=\"#tz\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>This library provides control over time zone conversions in templates.\nLike <code class=\"docutils literal notranslate\">l10n</code>, you only need to load the library using <code class=\"docutils literal notranslate\">{% load tz %}</code>,\nbut you'll usually also set <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\">USE_TZ</code></a> to <code class=\"docutils literal notranslate\">True</code> so that conversion\nto local time happens by default.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/i18n/timezones/#time-zones-in-templates\"><span class=\"std std-ref\">Time zone aware output in templates</span></a>.</p>\n</section>\n</section>\n<section id=\"other-tags-and-filters-libraries\">\n<h2>Other tags and filters libraries<a class=\"heading-anchor\" href=\"#other-tags-and-filters-libraries\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django comes with a couple of other template-tag libraries that you have to\nenable explicitly in your <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> setting and enable in your\ntemplate with the <a class=\"reference internal\" href=\"#std-templatetag-load\"><code class=\"xref std std-ttag docutils literal notranslate\">{% load %}</code></a> tag.</p>\n<section id=\"django-contrib-humanize\">\n<h3><code class=\"docutils literal notranslate\">django.contrib.humanize</code><a class=\"heading-anchor\" href=\"#django-contrib-humanize\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>A set of Django template filters useful for adding a &quot;human touch&quot; to data. See\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/contrib/humanize/\"><span class=\"doc\">django.contrib.humanize</span></a>.</p>\n</section>\n<section id=\"static\">\n<h3><code class=\"docutils literal notranslate\">static</code><a class=\"heading-anchor\" href=\"#static\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"std-templatetag-static\">\n<span id=\"id6\"></span><h4><code class=\"docutils literal notranslate\">static</code><a class=\"heading-anchor\" href=\"#std-templatetag-static\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>To link to static files that are saved in <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_ROOT</code></a> Django ships\nwith a <a class=\"reference internal\" href=\"#std-templatetag-static\"><code class=\"xref std std-ttag docutils literal notranslate\">static</code></a> template tag. If the <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.staticfiles</code></a>\napp is installed, the tag will serve files using <code class=\"docutils literal notranslate\">url()</code> method of the\nstorage specified by <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-STATICFILES_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\">STATICFILES_STORAGE</code></a>. For example:</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\">load</span> <span class=\"nv\">static</span> <span class=\"cp\">%}</span>\n<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=\"k\">static</span> <span class=\"s2\">&quot;images/hi.jpg&quot;</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span> <span class=\"na\">alt</span><span class=\"o\">=</span><span class=\"s\">&quot;Hi!&quot;</span> <span class=\"p\">/&gt;</span>\n</code></pre></div>\n<p>It is also able to consume standard context variables, e.g. assuming a\n<code class=\"docutils literal notranslate\">user_stylesheet</code> variable is passed to the template:</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\">load</span> <span class=\"nv\">static</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">link</span> <span class=\"na\">rel</span><span class=\"o\">=</span><span class=\"s\">&quot;stylesheet&quot;</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">static</span> <span class=\"nv\">user_stylesheet</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span> <span class=\"na\">type</span><span class=\"o\">=</span><span class=\"s\">&quot;text/css&quot;</span> <span class=\"na\">media</span><span class=\"o\">=</span><span class=\"s\">&quot;screen&quot;</span> <span class=\"p\">/&gt;</span>\n</code></pre></div>\n<p>If you'd like to retrieve a static URL without displaying it, you can use a\nslightly different call:</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\">load</span> <span class=\"nv\">static</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">static</span> <span class=\"s2\">&quot;images/hi.jpg&quot;</span> <span class=\"k\">as</span> <span class=\"nv\">myphoto</span> <span class=\"cp\">%}</span>\n<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\">myphoto</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">img</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<aside class=\"admonition-using-jinja2-templates admonition\" role=\"note\">\n<p class=\"admonition-title\">Using Jinja2 templates?</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/templates/#django.template.backends.jinja2.Jinja2\" title=\"django.template.backends.jinja2.Jinja2\"><code class=\"xref py py-class docutils literal notranslate\">Jinja2</code></a> for information on\nusing the <code class=\"docutils literal notranslate\">static</code> tag with Jinja2.</p>\n</aside>\n</section>\n<section id=\"get-static-prefix\">\n<span id=\"std-templatetag-get_static_prefix\"></span><h4><code class=\"docutils literal notranslate\">get_static_prefix</code><a class=\"heading-anchor\" href=\"#get-static-prefix\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>You should prefer the <a class=\"reference internal\" href=\"#std-templatetag-static\"><code class=\"xref std std-ttag docutils literal notranslate\">static</code></a> template tag, but if you need more control\nover exactly where and how <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_URL</code></a> is injected into the template,\nyou can use the <a class=\"reference internal\" href=\"#std-templatetag-get_static_prefix\"><code class=\"xref std std-ttag docutils literal notranslate\">get_static_prefix</code></a> template tag:</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\">load</span> <span class=\"nv\">static</span> <span class=\"cp\">%}</span>\n<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=\"k\">get_static_prefix</span> <span class=\"cp\">%}</span><span class=\"s\">images/hi.jpg&quot;</span> <span class=\"na\">alt</span><span class=\"o\">=</span><span class=\"s\">&quot;Hi!&quot;</span> <span class=\"p\">/&gt;</span>\n</code></pre></div>\n<p>There's also a second form you can use to avoid extra processing if you need\nthe value multiple times:</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\">load</span> <span class=\"nv\">static</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">get_static_prefix</span> <span class=\"k\">as</span> <span class=\"nv\">STATIC_PREFIX</span> <span class=\"cp\">%}</span>\n\n<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_PREFIX</span> <span class=\"cp\">}}</span><span class=\"s\">images/hi.jpg&quot;</span> <span class=\"na\">alt</span><span class=\"o\">=</span><span class=\"s\">&quot;Hi!&quot;</span> <span class=\"p\">/&gt;</span>\n<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_PREFIX</span> <span class=\"cp\">}}</span><span class=\"s\">images/hi2.jpg&quot;</span> <span class=\"na\">alt</span><span class=\"o\">=</span><span class=\"s\">&quot;Hello!&quot;</span> <span class=\"p\">/&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"get-media-prefix\">\n<span id=\"std-templatetag-get_media_prefix\"></span><h4><code class=\"docutils literal notranslate\">get_media_prefix</code><a class=\"heading-anchor\" href=\"#get-media-prefix\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Similar to the <a class=\"reference internal\" href=\"#std-templatetag-get_static_prefix\"><code class=\"xref std std-ttag docutils literal notranslate\">get_static_prefix</code></a>, <code class=\"docutils literal notranslate\">get_media_prefix</code> populates a\ntemplate variable with the media prefix <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_URL</code></a>, e.g.:</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\">load</span> <span class=\"nv\">static</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">body</span> <span class=\"na\">data-media-url</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">get_media_prefix</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>By storing the value in a data attribute, we ensure it's escaped appropriately\nif we want to use it in a JavaScript context.</p>\n</section>\n</section>\n</section>","rootId":"built-in-template-tags-and-filters","toc":[{"title":"Built-in tag reference","anchor":"built-in-tag-reference","children":[{"title":"autoescape","anchor":"autoescape","children":[]},{"title":"block","anchor":"block","children":[]},{"title":"comment","anchor":"comment","children":[]},{"title":"csrf_token","anchor":"csrf-token","children":[]},{"title":"cycle","anchor":"cycle","children":[]},{"title":"debug","anchor":"debug","children":[]},{"title":"extends","anchor":"extends","children":[]},{"title":"filter","anchor":"filter","children":[]},{"title":"firstof","anchor":"firstof","children":[]},{"title":"for","anchor":"for","children":[]},{"title":"for ... empty","anchor":"for-empty","children":[]},{"title":"if","anchor":"if","children":[{"title":"Boolean operators","anchor":"boolean-operators","children":[{"title":"== operator","anchor":"operator","children":[]},{"title":"!= operator","anchor":"id1","children":[]},{"title":"< operator","anchor":"id2","children":[]},{"title":"> operator","anchor":"id3","children":[]},{"title":"<= operator","anchor":"id4","children":[]},{"title":">= operator","anchor":"id5","children":[]},{"title":"in operator","anchor":"in-operator","children":[]},{"title":"not in operator","anchor":"not-in-operator","children":[]},{"title":"is operator","anchor":"is-operator","children":[]},{"title":"is not operator","anchor":"is-not-operator","children":[]}]},{"title":"Filters","anchor":"filters","children":[]},{"title":"Complex expressions","anchor":"complex-expressions","children":[]}]},{"title":"ifequal and ifnotequal","anchor":"ifequal-and-ifnotequal","children":[]},{"title":"ifchanged","anchor":"ifchanged","children":[]},{"title":"include","anchor":"include","children":[]},{"title":"load","anchor":"load","children":[]},{"title":"lorem","anchor":"lorem","children":[]},{"title":"now","anchor":"now","children":[]},{"title":"regroup","anchor":"regroup","children":[{"title":"Grouping on other properties","anchor":"grouping-on-other-properties","children":[]}]},{"title":"resetcycle","anchor":"resetcycle","children":[]},{"title":"spaceless","anchor":"spaceless","children":[]},{"title":"templatetag","anchor":"templatetag","children":[]},{"title":"url","anchor":"url","children":[]},{"title":"verbatim","anchor":"verbatim","children":[]},{"title":"widthratio","anchor":"widthratio","children":[]},{"title":"with","anchor":"with","children":[]}]},{"title":"Built-in filter reference","anchor":"built-in-filter-reference","children":[{"title":"add","anchor":"add","children":[]},{"title":"addslashes","anchor":"addslashes","children":[]},{"title":"capfirst","anchor":"capfirst","children":[]},{"title":"center","anchor":"center","children":[]},{"title":"cut","anchor":"cut","children":[]},{"title":"date","anchor":"date","children":[]},{"title":"default","anchor":"default","children":[]},{"title":"default_if_none","anchor":"default-if-none","children":[]},{"title":"dictsort","anchor":"dictsort","children":[]},{"title":"dictsortreversed","anchor":"dictsortreversed","children":[]},{"title":"divisibleby","anchor":"divisibleby","children":[]},{"title":"escape","anchor":"escape","children":[]},{"title":"escapejs","anchor":"escapejs","children":[]},{"title":"filesizeformat","anchor":"filesizeformat","children":[]},{"title":"first","anchor":"first","children":[]},{"title":"floatformat","anchor":"floatformat","children":[]},{"title":"force_escape","anchor":"force-escape","children":[]},{"title":"get_digit","anchor":"get-digit","children":[]},{"title":"iriencode","anchor":"iriencode","children":[]},{"title":"join","anchor":"join","children":[]},{"title":"last","anchor":"last","children":[]},{"title":"length","anchor":"length","children":[]},{"title":"length_is","anchor":"length-is","children":[]},{"title":"linebreaks","anchor":"linebreaks","children":[]},{"title":"linebreaksbr","anchor":"linebreaksbr","children":[]},{"title":"linenumbers","anchor":"linenumbers","children":[]},{"title":"ljust","anchor":"ljust","children":[]},{"title":"lower","anchor":"lower","children":[]},{"title":"make_list","anchor":"make-list","children":[]},{"title":"phone2numeric","anchor":"phone2numeric","children":[]},{"title":"pluralize","anchor":"pluralize","children":[]},{"title":"pprint","anchor":"pprint","children":[]},{"title":"random","anchor":"random","children":[]},{"title":"rjust","anchor":"rjust","children":[]},{"title":"safe","anchor":"safe","children":[]},{"title":"safeseq","anchor":"safeseq","children":[]},{"title":"slice","anchor":"slice","children":[]},{"title":"slugify","anchor":"slugify","children":[]},{"title":"stringformat","anchor":"stringformat","children":[]},{"title":"striptags","anchor":"striptags","children":[]},{"title":"time","anchor":"time","children":[]},{"title":"timesince","anchor":"timesince","children":[]},{"title":"timeuntil","anchor":"timeuntil","children":[]},{"title":"title","anchor":"title","children":[]},{"title":"truncatechars","anchor":"truncatechars","children":[]},{"title":"truncatechars_html","anchor":"truncatechars-html","children":[]},{"title":"truncatewords","anchor":"truncatewords","children":[]},{"title":"truncatewords_html","anchor":"truncatewords-html","children":[]},{"title":"unordered_list","anchor":"unordered-list","children":[]},{"title":"upper","anchor":"upper","children":[]},{"title":"urlencode","anchor":"urlencode","children":[]},{"title":"urlize","anchor":"urlize","children":[]},{"title":"urlizetrunc","anchor":"urlizetrunc","children":[]},{"title":"wordcount","anchor":"wordcount","children":[]},{"title":"wordwrap","anchor":"wordwrap","children":[]},{"title":"yesno","anchor":"yesno","children":[]}]},{"title":"Internationalization tags and filters","anchor":"internationalization-tags-and-filters","children":[{"title":"i18n","anchor":"i18n","children":[]},{"title":"l10n","anchor":"l10n","children":[]},{"title":"tz","anchor":"tz","children":[]}]},{"title":"Other tags and filters libraries","anchor":"other-tags-and-filters-libraries","children":[{"title":"django.contrib.humanize","anchor":"django-contrib-humanize","children":[]},{"title":"static","anchor":"static","children":[{"title":"static","anchor":"std-templatetag-static","children":[]},{"title":"get_static_prefix","anchor":"get-static-prefix","children":[]},{"title":"get_media_prefix","anchor":"get-media-prefix","children":[]}]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/zh-hans/2.0/ref/"},{"docname":"ref/templates/index","title":"Templates","url":"/zh-hans/2.0/ref/templates/"}],"prev":{"docname":"ref/templates/language","title":"The Django template language","url":"/zh-hans/2.0/ref/templates/language/"},"next":{"docname":"ref/templates/api","title":"The Django template language: for Python programmers","url":"/zh-hans/2.0/ref/templates/api/"},"formats":{"html":"/zh-hans/2.0/ref/templates/builtins/","markdown":"/zh-hans/2.0/ref/templates/builtins.md","json":"/zh-hans/2.0/ref/templates/builtins.json"},"source":"https://github.com/django/django/blob/stable/2.0.x/docs/ref/templates/builtins.txt","official":"https://docs.djangoproject.com/zh-hans/2.0/ref/templates/builtins/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}