<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Built-in template tags and filters — Django v1.2 documentation</title> <link rel="stylesheet" href="../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.2', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <link rel="top" title="Django v1.2 documentation" href="../../index.html" /> <link rel="up" title="Templates" href="index.html" /> <link rel="next" title="The Django template language: For Python programmers" href="api.html" /> <link rel="prev" title="Templates" href="index.html" /> <script type="text/javascript" src="../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "#"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django v1.2 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../index.html">Home</a> | <a title="Table of contents" href="../../contents.html">Table of contents</a> | <a title="Global index" href="../../genindex.html">Index</a> | <a title="Module index" href="../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="index.html" title="Templates">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="api.html" title="The Django template language: For Python programmers">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="ref-templates-builtins"> <div class="section" id="s-built-in-template-tags-and-filters"> <span id="built-in-template-tags-and-filters"></span><h1>Built-in template tags and filters<a class="headerlink" href="#built-in-template-tags-and-filters" title="Permalink to this headline">¶</a></h1> <p>This document describes Django’s built-in template tags and filters. It is recommended that you use the <a class="reference internal" href="../../topics/templates.html#template-built-in-reference"><em>automatic documentation</em></a>, if available, as this will also include documentation for any custom tags or filters installed.</p> <div class="section" id="s-built-in-tag-reference"> <span id="s-ref-templates-builtins-tags"></span><span id="built-in-tag-reference"></span><span id="ref-templates-builtins-tags"></span><h2>Built-in tag reference<a class="headerlink" href="#built-in-tag-reference" title="Permalink to this headline">¶</a></h2> <div class="section" id="s-autoescape"> <span id="s-std:templatetag-autoescape"></span><span id="autoescape"></span><span id="std:templatetag-autoescape"></span><h3>autoescape<a class="headerlink" href="#autoescape" title="Permalink to this headline">¶</a></h3> <div class="versionadded"> <span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div> <p>Control the current auto-escaping behavior. This tag takes either <tt class="docutils literal"><span class="pre">on</span></tt> or <tt class="docutils literal"><span class="pre">off</span></tt> as an argument and that determines whether auto-escaping is in effect inside the block. The block is closed with an <tt class="docutils literal"><span class="pre">endautoescape</span></tt> ending tag.</p> <p>When auto-escaping is in effect, all variable content has HTML escaping applied to it before placing the result into the output (but after any filters have been applied). This is equivalent to manually applying the <tt class="docutils literal"><span class="pre">escape</span></tt> filter to each variable.</p> <p>The only exceptions are variables that are already marked as “safe” from escaping, either by the code that populated the variable, or because it has had the <tt class="docutils literal"><span class="pre">safe</span></tt> or <tt class="docutils literal"><span class="pre">escape</span></tt> filters applied.</p> <p>Sample usage:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">on</span> <span class="cp">%}</span> <span class="cp">{{</span> <span class="nv">body</span> <span class="cp">}}</span> <span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-block"> <span id="s-std:templatetag-block"></span><span id="block"></span><span id="std:templatetag-block"></span><h3>block<a class="headerlink" href="#block" title="Permalink to this headline">¶</a></h3> <p>Define a block that can be overridden by child templates. See <a class="reference internal" href="../../topics/templates.html#template-inheritance"><em>Template inheritance</em></a> for more information.</p> </div> <div class="section" id="s-comment"> <span id="s-std:templatetag-comment"></span><span id="comment"></span><span id="std:templatetag-comment"></span><h3>comment<a class="headerlink" href="#comment" title="Permalink to this headline">¶</a></h3> <p>Ignore everything between <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">comment</span> <span class="pre">%}</span></tt> and <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">endcomment</span> <span class="pre">%}</span></tt></p> </div> <div class="section" id="s-csrf-token"> <span id="s-std:templatetag-csrf_token"></span><span id="csrf-token"></span><span id="std:templatetag-csrf_token"></span><h3>csrf_token<a class="headerlink" href="#csrf-token" title="Permalink to this headline">¶</a></h3> <div class="versionadded"> <span class="title">New in Django 1.1.2:</span> <a class="reference internal" href="../../releases/1.1.2.html"><em>Please, see the release notes</em></a></div> <p>In the Django 1.1.X series, this is a no-op tag that returns an empty string for future compatibility purposes. In Django 1.2 and later, it is used for CSRF protection, as described in the documentation for <a class="reference internal" href="../contrib/csrf.html"><em>Cross Site Request Forgeries</em></a>.</p> </div> <div class="section" id="s-cycle"> <span id="s-std:templatetag-cycle"></span><span id="cycle"></span><span id="std:templatetag-cycle"></span><h3>cycle<a class="headerlink" href="#cycle" title="Permalink to this headline">¶</a></h3> <div class="versionchanged"> <span class="title">Changed in Django 1.0:</span> Cycle among the given strings or variables each time this tag is encountered.</div> <p>Within a loop, cycles among the given strings each time through the loop:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> <span class="nt"><tr</span> <span class="na">class=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="s1">'row2'</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">></span> ... <span class="nt"></tr></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> </pre></div> </div> <p>You can use variables, too. For example, if you have two template variables, <tt class="docutils literal"><span class="pre">rowvalue1</span></tt> and <tt class="docutils literal"><span class="pre">rowvalue2</span></tt>, you can cycle between their values like this:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> <span class="nt"><tr</span> <span class="na">class=</span><span class="s">"</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">"</span><span class="nt">></span> ... <span class="nt"></tr></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> </pre></div> </div> <p>Yes, you can mix variables and strings:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> <span class="nt"><tr</span> <span class="na">class=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="nv">rowvalue2</span> <span class="s1">'row3'</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">></span> ... <span class="nt"></tr></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> </pre></div> </div> <p>In some cases you might want to refer to the next value of a cycle from outside of a loop. To do this, just give the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></tt> tag a name, using "as", like this:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="s1">'row2'</span> <span class="k">as</span> <span class="nv">rowcolors</span> <span class="cp">%}</span> </pre></div> </div> <p>From then on, you can insert the current value of the cycle wherever you'd like in your template:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><tr</span> <span class="na">class=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowcolors</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">></span>...<span class="nt"></tr></span> <span class="nt"><tr</span> <span class="na">class=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowcolors</span> <span class="cp">%}</span><span class="s">"</span><span class="nt">></span>...<span class="nt"></tr></span> </pre></div> </div> <p>You can use any number of values in a <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></tt> tag, separated by spaces. Values enclosed in single (<tt class="docutils literal"><span class="pre">'</span></tt>) or double quotes (<tt class="docutils literal"><span class="pre">"</span></tt>) are treated as string literals, while values without quotes are treated as template variables.</p> <p>Note that the variables included in the cycle will not be escaped. This is because template tags do not escape their content. Any HTML or Javascript code contained in the printed variable will be rendered as-is, which could potentially lead to security issues.</p> <p>If you need to escape the variables in the cycle, you must do so explicitly:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">filter</span> <span class="nf">force_escape</span> <span class="cp">%}</span> <span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">var1</span> <span class="nv">var2</span> <span class="nv">var3</span> <span class="cp">%}</span> <span class="cp">{%</span> <span class="k">endfilter</span> <span class="cp">%}</span> </pre></div> </div> <p>For backwards compatibility, the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></tt> tag supports the much inferior old syntax from previous Django versions. You shouldn't use this in any new projects, but for the sake of the people who are still using it, here's what it looks like:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">row1</span><span class="o">,</span><span class="nv">row2</span><span class="o">,</span><span class="nv">row3</span> <span class="cp">%}</span> </pre></div> </div> <p>In this syntax, each value gets interpreted as a literal string, and there's no way to specify variable values. Or literal commas. Or spaces. Did we mention you shouldn't use this syntax in any new projects?</p> </div> <div class="section" id="s-debug"> <span id="s-std:templatetag-debug"></span><span id="debug"></span><span id="std:templatetag-debug"></span><h3>debug<a class="headerlink" href="#debug" title="Permalink to this headline">¶</a></h3> <p>Output a whole load of debugging information, including the current context and imported modules.</p> </div> <div class="section" id="s-extends"> <span id="s-std:templatetag-extends"></span><span id="extends"></span><span id="std:templatetag-extends"></span><h3>extends<a class="headerlink" href="#extends" title="Permalink to this headline">¶</a></h3> <p>Signal that this template extends a parent template.</p> <p>This tag can be used in two ways:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">"base.html"</span> <span class="pre">%}</span></tt> (with quotes) uses the literal value <tt class="docutils literal"><span class="pre">"base.html"</span></tt> as the name of the parent template to extend.</li> <li><tt class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">variable</span> <span class="pre">%}</span></tt> uses the value of <tt class="docutils literal"><span class="pre">variable</span></tt>. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a <tt class="docutils literal"><span class="pre">Template</span></tt> object, Django will use that object as the parent template.</li> </ul> <p>See <a class="reference internal" href="../../topics/templates.html#template-inheritance"><em>Template inheritance</em></a> for more information.</p> </div> <div class="section" id="s-filter"> <span id="s-std:templatetag-filter"></span><span id="filter"></span><span id="std:templatetag-filter"></span><h3>filter<a class="headerlink" href="#filter" title="Permalink to this headline">¶</a></h3> <p>Filter the contents of the variable through variable filters.</p> <p>Filters can also be piped through each other, and they can have arguments -- just like in variable syntax.</p> <p>Sample usage:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> This text will be HTML-escaped, and will appear in all lowercase. <span class="cp">{%</span> <span class="k">endfilter</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-firstof"> <span id="s-std:templatetag-firstof"></span><span id="firstof"></span><span id="std:templatetag-firstof"></span><h3>firstof<a class="headerlink" href="#firstof" title="Permalink to this headline">¶</a></h3> <p>Outputs the first variable passed that is not False, without escaping.</p> <p>Outputs nothing if all the passed variables are False.</p> <p>Sample usage:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> </pre></div> </div> <p>This is equivalent to:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">var1</span> <span class="cp">%}</span> <span class="cp">{{</span> <span class="nv">var1</span><span class="o">|</span><span class="nf">safe</span> <span class="cp">}}</span> <span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}{%</span> <span class="k">if</span> <span class="nv">var2</span> <span class="cp">%}</span> <span class="cp">{{</span> <span class="nv">var2</span><span class="o">|</span><span class="nf">safe</span> <span class="cp">}}</span> <span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}{%</span> <span class="k">if</span> <span class="nv">var3</span> <span class="cp">%}</span> <span class="cp">{{</span> <span class="nv">var3</span><span class="o">|</span><span class="nf">safe</span> <span class="cp">}}</span> <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}{%</span> <span class="k">endif</span> <span class="cp">%}{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> <p>You can also use a literal string as a fallback value in case all passed variables are False:</p> <div class="highlight-html+django"><div class="highlight"><pre><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">"fallback value"</span> <span class="cp">%}</span> </pre></div> </div> <p>Note that the variables included in the firstof tag will not be escaped. This is because template tags do not escape their content. Any HTML or Javascript code contained in the printed variable will be rendered as-is, which could potentially lead to security issues.</p> <p>If you need to escape the variables in the firstof tag, you must do so explicitly:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">filter</span> <span class="nf">force_escape</span> <span class="cp">%}</span> <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">"fallback value"</span> <span class="cp">%}</span> <span class="cp">{%</span> <span class="k">endfilter</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-for"> <span id="s-std:templatetag-for"></span><span id="for"></span><span id="std:templatetag-for"></span><h3>for<a class="headerlink" href="#for" title="Permalink to this headline">¶</a></h3> <p>Loop over each item in an array. For example, to display a list of athletes provided in <tt class="docutils literal"><span class="pre">athlete_list</span></tt>:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><ul></span> <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> <span class="nt"><li></span><span class="cp">{{</span> <span class="nv">athlete.name</span> <span class="cp">}}</span><span class="nt"></li></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> <span class="nt"></ul></span> </pre></div> </div> <p>You can loop over a list in reverse by using <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">for</span> <span class="pre">obj</span> <span class="pre">in</span> <span class="pre">list</span> <span class="pre">reversed</span> <span class="pre">%}</span></tt>.</p> <div class="versionadded"> <span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div> <p>If you need to loop over a list of lists, you can unpack the values in each sub-list into individual variables. For example, if your context contains a list of (x,y) coordinates called <tt class="docutils literal"><span class="pre">points</span></tt>, you could use the following to output the list of points:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> 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> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> </pre></div> </div> <p>This can also be useful if you need to access the items in a dictionary. For example, if your context contained a dictionary <tt class="docutils literal"><span class="pre">data</span></tt>, the following would display the keys and values of the dictionary:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> <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> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> </pre></div> </div> <p>The for loop sets a number of variables available within the loop:</p> <table class="docutils"> <colgroup> <col width="35%" /> <col width="65%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">Variable</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr><td><tt class="docutils literal"><span class="pre">forloop.counter</span></tt></td> <td>The current iteration of the loop (1-indexed)</td> </tr> <tr><td><tt class="docutils literal"><span class="pre">forloop.counter0</span></tt></td> <td>The current iteration of the loop (0-indexed)</td> </tr> <tr><td><tt class="docutils literal"><span class="pre">forloop.revcounter</span></tt></td> <td>The number of iterations from the end of the loop (1-indexed)</td> </tr> <tr><td><tt class="docutils literal"><span class="pre">forloop.revcounter0</span></tt></td> <td>The number of iterations from the end of the loop (0-indexed)</td> </tr> <tr><td><tt class="docutils literal"><span class="pre">forloop.first</span></tt></td> <td>True if this is the first time through the loop</td> </tr> <tr><td><tt class="docutils literal"><span class="pre">forloop.last</span></tt></td> <td>True if this is the last time through the loop</td> </tr> <tr><td><tt class="docutils literal"><span class="pre">forloop.parentloop</span></tt></td> <td>For nested loops, this is the loop "above" the current one</td> </tr> </tbody> </table> <div class="section" id="s-for-empty"> <span id="for-empty"></span><h4>for ... empty<a class="headerlink" href="#for-empty" title="Permalink to this headline">¶</a></h4> <div class="versionadded"> <span class="title">New in Django 1.1:</span> <a class="reference internal" href="../../releases/1.1.html"><em>Please, see the release notes</em></a></div> <p>The <tt class="docutils literal"><span class="pre">for</span></tt> tag can take an optional <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">empty</span> <span class="pre">%}</span></tt> clause that will be displayed if the given array is empty or could not be found:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><ul></span> <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> <span class="nt"><li></span><span class="cp">{{</span> <span class="nv">athlete.name</span> <span class="cp">}}</span><span class="nt"></li></span> <span class="cp">{%</span> <span class="k">empty</span> <span class="cp">%}</span> <span class="nt"><li></span>Sorry, no athlete in this list!<span class="nt"></li></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> <span class="nt"><ul></span> </pre></div> </div> <p>The above is equivalent to -- but shorter, cleaner, and possibly faster than -- the following:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><ul></span> <span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="cp">%}</span> <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> <span class="nt"><li></span><span class="cp">{{</span> <span class="nv">athlete.name</span> <span class="cp">}}</span><span class="nt"></li></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> <span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span> <span class="nt"><li></span>Sorry, no athletes in this list.<span class="nt"></li></span> <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> <span class="nt"></ul></span> </pre></div> </div> </div> </div> <div class="section" id="s-if"> <span id="s-std:templatetag-if"></span><span id="if"></span><span id="std:templatetag-if"></span><h3>if<a class="headerlink" href="#if" title="Permalink to this headline">¶</a></h3> <p>The <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">%}</span></tt> tag evaluates a variable, and if that variable is "true" (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="cp">%}</span> 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> <span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span> No athletes. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> <p>In the above, if <tt class="docutils literal"><span class="pre">athlete_list</span></tt> is not empty, the number of athletes will be displayed by the <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">athlete_list|length</span> <span class="pre">}}</span></tt> variable.</p> <p>As you can see, the <tt class="docutils literal"><span class="pre">if</span></tt> tag can take an optional <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></tt> clause that will be displayed if the test fails.</p> <div class="section" id="s-boolean-operators"> <span id="boolean-operators"></span><h4>Boolean operators<a class="headerlink" href="#boolean-operators" title="Permalink to this headline">¶</a></h4> <p><tt class="docutils literal"><span class="pre">if</span></tt> tags may use <tt class="docutils literal"><span class="pre">and</span></tt>, <tt class="docutils literal"><span class="pre">or</span></tt> or <tt class="docutils literal"><span class="pre">not</span></tt> to test a number of variables or to negate a given variable:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> Both athletes and coaches are available. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> <span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">athlete_list</span> <span class="cp">%}</span> There are no athletes. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> <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> There are some athletes or some coaches. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> <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> There are no athletes or there are some coaches (OK, so writing English translations of boolean logic sounds stupid; it's not our fault). <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> <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> There are some athletes and absolutely no coaches. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> <div class="versionchanged"> <span class="title">Changed in Django 1.2:</span> <a class="reference internal" href="../../releases/1.2.html"><em>Please, see the release notes</em></a></div> <p>Use of both <tt class="docutils literal"><span class="pre">and</span></tt> and <tt class="docutils literal"><span class="pre">or</span></tt> clauses within the same tag is allowed, with <tt class="docutils literal"><span class="pre">and</span></tt> having higher precedence than <tt class="docutils literal"><span class="pre">or</span></tt> e.g.:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> </pre></div> </div> <p>will be interpreted like:</p> <div class="highlight-python"><pre>if (athlete_list and coach_list) or cheerleader_list</pre> </div> <p>Use of actual brackets in the <tt class="docutils literal"><span class="pre">if</span></tt> tag is invalid syntax. If you need them to indicate precedence, you should use nested <tt class="docutils literal"><span class="pre">if</span></tt> tags.</p> <div class="versionadded"> <span class="title">New in Django 1.2:</span> <a class="reference internal" href="../../releases/1.2.html"><em>Please, see the release notes</em></a></div> <p><tt class="docutils literal"><span class="pre">if</span></tt> tags may also use the operators <tt class="docutils literal"><span class="pre">==</span></tt>, <tt class="docutils literal"><span class="pre">!=</span></tt>, <tt class="docutils literal"><span class="pre"><</span></tt>, <tt class="docutils literal"><span class="pre">></span></tt>, <tt class="docutils literal"><span class="pre"><=</span></tt>, <tt class="docutils literal"><span class="pre">>=</span></tt> and <tt class="docutils literal"><span class="pre">in</span></tt> which work as follows:</p> </div> <div class="section" id="s-operator"> <span id="operator"></span><h4><tt class="docutils literal"><span class="pre">==</span></tt> operator<a class="headerlink" href="#operator" title="Permalink to this headline">¶</a></h4> <p>Equality. Example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">==</span> <span class="s2">"x"</span> <span class="cp">%}</span> This appears if variable somevar equals the string "x" <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-id1"> <span id="id1"></span><h4><tt class="docutils literal"><span class="pre">!=</span></tt> operator<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h4> <p>Inequality. Example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="p">!</span><span class="o">=</span> <span class="s2">"x"</span> <span class="cp">%}</span> This appears if variable somevar does not equal the string "x", or if somevar is not found in the context <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-id2"> <span id="id2"></span><h4><tt class="docutils literal"><span class="pre"><</span></tt> operator<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h4> <p>Less than. Example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o"><</span> <span class="m">100</span> <span class="cp">%}</span> This appears if variable somevar is less than 100. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-id3"> <span id="id3"></span><h4><tt class="docutils literal"><span class="pre">></span></tt> operator<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h4> <p>Greater than. Example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">></span> <span class="m">0</span> <span class="cp">%}</span> This appears if variable somevar is greater than 0. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-id4"> <span id="id4"></span><h4><tt class="docutils literal"><span class="pre"><=</span></tt> operator<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h4> <p>Less than or equal to. Example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o"><=</span> <span class="m">100</span> <span class="cp">%}</span> This appears if variable somevar is less than 100 or equal to 100. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-id5"> <span id="id5"></span><h4><tt class="docutils literal"><span class="pre">>=</span></tt> operator<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h4> <p>Greater than or equal to. Example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">>=</span> <span class="m">1</span> <span class="cp">%}</span> This appears if variable somevar is greater than 1 or equal to 1. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-in-operator"> <span id="in-operator"></span><h4><tt class="docutils literal"><span class="pre">in</span></tt> operator<a class="headerlink" href="#in-operator" title="Permalink to this headline">¶</a></h4> <p>Contained within. This operator is supported by many Python containers to test whether the given value is in the container. The following are some examples of how <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></tt> will be interpreted:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">if</span> <span class="s2">"bc"</span> <span class="k">in</span> <span class="s2">"abcdef"</span> <span class="cp">%}</span> This appears since "bc" is a substring of "abcdef" <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> <span class="cp">{%</span> <span class="k">if</span> <span class="s2">"hello"</span> <span class="k">in</span> <span class="nv">greetings</span> <span class="cp">%}</span> If greetings is a list or set, one element of which is the string "hello", this will appear. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> <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> If users is a QuerySet, this will appear if user is an instance that belongs to the QuerySet. <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> </div> </div> <div class="section" id="s-not-in-operator"> <span id="not-in-operator"></span><h3><tt class="docutils literal"><span class="pre">not</span> <span class="pre">in</span></tt> operator<a class="headerlink" href="#not-in-operator" title="Permalink to this headline">¶</a></h3> <p>Not contained within. This is the negation of the <tt class="docutils literal"><span class="pre">in</span></tt> operator.</p> <p>The comparison operators cannot be 'chained' like in Python or in mathematical notation. For example, instead of using:</p> <div class="highlight-html+django"><div class="highlight"><pre><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="o">></span> <span class="nv">c</span> <span class="cp">%}</span> (WRONG) </pre></div> </div> <p>you should use:</p> <div class="highlight-html+django"><div class="highlight"><pre><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">and</span> <span class="nv">b</span> <span class="o">></span> <span class="nv">c</span> <span class="cp">%}</span> </pre></div> </div> <div class="section" id="s-filters"> <span id="filters"></span><h4>Filters<a class="headerlink" href="#filters" title="Permalink to this headline">¶</a></h4> <p>You can also use filters in the <tt class="docutils literal"><span class="pre">if</span></tt> expression. For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><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">>=</span> <span class="m">100</span> <span class="cp">%}</span> You have lots of messages today! <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-complex-expressions"> <span id="complex-expressions"></span><h4>Complex expressions<a class="headerlink" href="#complex-expressions" title="Permalink to this headline">¶</a></h4> <p>All of the above can be combined to form complex expressions. For such expressions, it can be important to know how the operators are grouped when the expression is evaluated - that is, the precedence rules. The precedence of the operators, from lowest to highest, is as follows:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">or</span></tt></li> <li><tt class="docutils literal"><span class="pre">and</span></tt></li> <li><tt class="docutils literal"><span class="pre">not</span></tt></li> <li><tt class="docutils literal"><span class="pre">in</span></tt></li> <li><tt class="docutils literal"><span class="pre">==</span></tt>, <tt class="docutils literal"><span class="pre">!=</span></tt>, <tt class="docutils literal"><span class="pre"><</span></tt>, <tt class="docutils literal"><span class="pre">></span></tt>,``<=``, <tt class="docutils literal"><span class="pre">>=</span></tt></li> </ul> <p>(This follows Python exactly). So, for example, the following complex if tag:</p> <blockquote> {% if a == b or c == d and e %}</blockquote> <p>...will be interpreted as:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">)</span> <span class="ow">or</span> <span class="p">((</span><span class="n">c</span> <span class="o">==</span> <span class="n">d</span><span class="p">)</span> <span class="ow">and</span> <span class="n">e</span><span class="p">)</span> </pre></div> </div> <p>If you need different precedence, you will need to use nested if tags. Sometimes that is better for clarity anyway, for the sake of those who do not know the precedence rules.</p> </div> </div> <div class="section" id="s-ifchanged"> <span id="s-std:templatetag-ifchanged"></span><span id="ifchanged"></span><span id="std:templatetag-ifchanged"></span><h3>ifchanged<a class="headerlink" href="#ifchanged" title="Permalink to this headline">¶</a></h3> <p>Check if a value has changed from the last iteration of a loop.</p> <p>The 'ifchanged' block tag is used within a loop. It has two possible uses.</p> <ol class="arabic"> <li><p class="first">Checks its own rendered contents against its previous state and only displays the content if it has changed. For example, this displays a list of days, only displaying the month if it changes:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><h1></span>Archive for <span class="cp">{{</span> <span class="nv">year</span> <span class="cp">}}</span><span class="nt"></h1></span> <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> <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="cp">%}</span><span class="nt"><h3></span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"F"</span> <span class="cp">}}</span><span class="nt"></h3></span><span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span> <span class="nt"><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"M/d"</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">}}</span><span class="s">/"</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"j"</span> <span class="cp">}}</span><span class="nt"></a></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> </pre></div> </div> </li> <li><p class="first">If given a variable, check whether that variable has changed. For example, the following shows the date every time it changes, but only shows the hour if both the hour and the date has changed:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> <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> <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> <span class="cp">{{</span> <span class="nv">date.hour</span> <span class="cp">}}</span> <span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> </pre></div> </div> </li> </ol> <p>The <tt class="docutils literal"><span class="pre">ifchanged</span></tt> tag can also take an optional <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></tt> clause that will be displayed if the value has not changed:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> <span class="nt"><div</span> <span class="na">style=</span><span class="s">"background-color:</span> <span class="s"> </span><span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">match.ballot_id</span> <span class="cp">%}</span><span class="s"></span> <span class="s"> </span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s2">"red"</span> <span class="s2">"blue"</span> <span class="cp">%}</span><span class="s"></span> <span class="s"> </span><span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span><span class="s"></span> <span class="s"> grey</span> <span class="s"> </span><span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span><span class="s"></span> <span class="s"> "</span><span class="nt">></span><span class="cp">{{</span> <span class="nv">match</span> <span class="cp">}}</span><span class="nt"></div></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-ifequal"> <span id="s-std:templatetag-ifequal"></span><span id="ifequal"></span><span id="std:templatetag-ifequal"></span><h3>ifequal<a class="headerlink" href="#ifequal" title="Permalink to this headline">¶</a></h3> <p>Output the contents of the block if the two arguments equal each other.</p> <p>Example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">ifequal</span> <span class="nv">user.id</span> <span class="nv">comment.user_id</span> <span class="cp">%}</span> ... <span class="cp">{%</span> <span class="k">endifequal</span> <span class="cp">%}</span> </pre></div> </div> <p>As in the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">%}</span></tt> tag, an <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></tt> clause is optional.</p> <p>The arguments can be hard-coded strings, so the following is valid:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">ifequal</span> <span class="nv">user.username</span> <span class="s2">"adrian"</span> <span class="cp">%}</span> ... <span class="cp">{%</span> <span class="k">endifequal</span> <span class="cp">%}</span> </pre></div> </div> <p>It is only possible to compare an argument to template variables or strings. You cannot check for equality with Python objects such as <tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt>. If you need to test if something is true or false, use the <tt class="docutils literal"><span class="pre">if</span></tt> tag instead.</p> <div class="versionadded"> <span class="title">New in Django 1.2:</span> An alternative to the <tt class="docutils literal"><span class="pre">ifequal</span></tt> tag is to use the <a class="reference internal" href="#std:templatetag-if"><tt class="xref std std-ttag docutils literal"><span class="pre">if</span></tt></a> tag and the <tt class="docutils literal"><span class="pre">==</span></tt> operator.</div> </div> <div class="section" id="s-ifnotequal"> <span id="s-std:templatetag-ifnotequal"></span><span id="ifnotequal"></span><span id="std:templatetag-ifnotequal"></span><h3>ifnotequal<a class="headerlink" href="#ifnotequal" title="Permalink to this headline">¶</a></h3> <p>Just like <tt class="docutils literal"><span class="pre">ifequal</span></tt>, except it tests that the two arguments are not equal.</p> <div class="versionadded"> <span class="title">New in Django 1.2:</span> An alternative to the <tt class="docutils literal"><span class="pre">ifnotequal</span></tt> tag is to use the <a class="reference internal" href="#std:templatetag-if"><tt class="xref std std-ttag docutils literal"><span class="pre">if</span></tt></a> tag and the <tt class="docutils literal"><span class="pre">!=</span></tt> operator.</div> </div> <div class="section" id="s-include"> <span id="s-std:templatetag-include"></span><span id="include"></span><span id="std:templatetag-include"></span><h3>include<a class="headerlink" href="#include" title="Permalink to this headline">¶</a></h3> <p>Loads a template and renders it with the current context. This is a way of "including" other templates within a template.</p> <p>The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.</p> <p>This example includes the contents of the template <tt class="docutils literal"><span class="pre">"foo/bar.html"</span></tt>:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">include</span> <span class="s2">"foo/bar.html"</span> <span class="cp">%}</span> </pre></div> </div> <p>This example includes the contents of the template whose name is contained in the variable <tt class="docutils literal"><span class="pre">template_name</span></tt>:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">include</span> <span class="nv">template_name</span> <span class="cp">%}</span> </pre></div> </div> <p>An included template is rendered with the context of the template that's including it. This example produces the output <tt class="docutils literal"><span class="pre">"Hello,</span> <span class="pre">John"</span></tt>:</p> <ul> <li><p class="first">Context: variable <tt class="docutils literal"><span class="pre">person</span></tt> is set to <tt class="docutils literal"><span class="pre">"john"</span></tt>.</p> </li> <li><p class="first">Template:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">include</span> <span class="s2">"name_snippet.html"</span> <span class="cp">%}</span> </pre></div> </div> </li> <li><p class="first">The <tt class="docutils literal"><span class="pre">name_snippet.html</span></tt> template:</p> <div class="highlight-html+django"><div class="highlight"><pre>Hello, <span class="cp">{{</span> <span class="nv">person</span> <span class="cp">}}</span> </pre></div> </div> </li> </ul> <p>See also: <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">ssi</span> <span class="pre">%}</span></tt>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#std:templatetag-include"><tt class="xref std std-ttag docutils literal"><span class="pre">include</span></tt></a> tag should be considered as an implementation of "render this subtemplate and include the HTML", not as "parse this subtemplate and include its contents as if it were part of the parent". This means that there is no shared state between included templates -- each include is a completely independent rendering process.</p> </div> </div> <div class="section" id="s-load"> <span id="s-std:templatetag-load"></span><span id="load"></span><span id="std:templatetag-load"></span><h3>load<a class="headerlink" href="#load" title="Permalink to this headline">¶</a></h3> <p>Load a custom template tag set.</p> <p>See <a class="reference internal" href="../../howto/custom-template-tags.html"><em>Custom tag and filter libraries</em></a> for more information.</p> </div> <div class="section" id="s-now"> <span id="s-std:templatetag-now"></span><span id="now"></span><span id="std:templatetag-now"></span><h3>now<a class="headerlink" href="#now" title="Permalink to this headline">¶</a></h3> <p>Display the date, formatted according to the given string.</p> <p>Uses the same format as PHP's <tt class="docutils literal"><span class="pre">date()</span></tt> function (<a class="reference external" href="http://php.net/date">http://php.net/date</a>) with some custom extensions.</p> <p>Available format strings:</p> <table class="docutils"> <colgroup> <col width="12%" /> <col width="31%" /> <col width="57%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">Format character</th> <th class="head">Description</th> <th class="head">Example output</th> </tr> </thead> <tbody valign="top"> <tr><td>a</td> <td><tt class="docutils literal"><span class="pre">'a.m.'</span></tt> or <tt class="docutils literal"><span class="pre">'p.m.'</span></tt> (Note that this is slightly different than PHP's output, because this includes periods to match Associated Press style.)</td> <td><tt class="docutils literal"><span class="pre">'a.m.'</span></tt></td> </tr> <tr><td>A</td> <td><tt class="docutils literal"><span class="pre">'AM'</span></tt> or <tt class="docutils literal"><span class="pre">'PM'</span></tt>.</td> <td><tt class="docutils literal"><span class="pre">'AM'</span></tt></td> </tr> <tr><td>b</td> <td>Month, textual, 3 letters, lowercase.</td> <td><tt class="docutils literal"><span class="pre">'jan'</span></tt></td> </tr> <tr><td>B</td> <td>Not implemented.</td> <td> </td> </tr> <tr><td>c</td> <td>ISO 8601 Format.</td> <td><tt class="docutils literal"><span class="pre">2008-01-02T10:30:00.000123</span></tt></td> </tr> <tr><td>d</td> <td>Day of the month, 2 digits with leading zeros.</td> <td><tt class="docutils literal"><span class="pre">'01'</span></tt> to <tt class="docutils literal"><span class="pre">'31'</span></tt></td> </tr> <tr><td>D</td> <td>Day of the week, textual, 3 letters.</td> <td><tt class="docutils literal"><span class="pre">'Fri'</span></tt></td> </tr> <tr><td>f</td> <td>Time, in 12-hour hours and minutes, with minutes left off if they're zero. Proprietary extension.</td> <td><tt class="docutils literal"><span class="pre">'1'</span></tt>, <tt class="docutils literal"><span class="pre">'1:30'</span></tt></td> </tr> <tr><td>F</td> <td>Month, textual, long.</td> <td><tt class="docutils literal"><span class="pre">'January'</span></tt></td> </tr> <tr><td>g</td> <td>Hour, 12-hour format without leading zeros.</td> <td><tt class="docutils literal"><span class="pre">'1'</span></tt> to <tt class="docutils literal"><span class="pre">'12'</span></tt></td> </tr> <tr><td>G</td> <td>Hour, 24-hour format without leading zeros.</td> <td><tt class="docutils literal"><span class="pre">'0'</span></tt> to <tt class="docutils literal"><span class="pre">'23'</span></tt></td> </tr> <tr><td>h</td> <td>Hour, 12-hour format.</td> <td><tt class="docutils literal"><span class="pre">'01'</span></tt> to <tt class="docutils literal"><span class="pre">'12'</span></tt></td> </tr> <tr><td>H</td> <td>Hour, 24-hour format.</td> <td><tt class="docutils literal"><span class="pre">'00'</span></tt> to <tt class="docutils literal"><span class="pre">'23'</span></tt></td> </tr> <tr><td>i</td> <td>Minutes.</td> <td><tt class="docutils literal"><span class="pre">'00'</span></tt> to <tt class="docutils literal"><span class="pre">'59'</span></tt></td> </tr> <tr><td>I</td> <td>Not implemented.</td> <td> </td> </tr> <tr><td>j</td> <td>Day of the month without leading zeros.</td> <td><tt class="docutils literal"><span class="pre">'1'</span></tt> to <tt class="docutils literal"><span class="pre">'31'</span></tt></td> </tr> <tr><td>l</td> <td>Day of the week, textual, long.</td> <td><tt class="docutils literal"><span class="pre">'Friday'</span></tt></td> </tr> <tr><td>L</td> <td>Boolean for whether it's a leap year.</td> <td><tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt></td> </tr> <tr><td>m</td> <td>Month, 2 digits with leading zeros.</td> <td><tt class="docutils literal"><span class="pre">'01'</span></tt> to <tt class="docutils literal"><span class="pre">'12'</span></tt></td> </tr> <tr><td>M</td> <td>Month, textual, 3 letters.</td> <td><tt class="docutils literal"><span class="pre">'Jan'</span></tt></td> </tr> <tr><td>n</td> <td>Month without leading zeros.</td> <td><tt class="docutils literal"><span class="pre">'1'</span></tt> to <tt class="docutils literal"><span class="pre">'12'</span></tt></td> </tr> <tr><td>N</td> <td>Month abbreviation in Associated Press style. Proprietary extension.</td> <td><tt class="docutils literal"><span class="pre">'Jan.'</span></tt>, <tt class="docutils literal"><span class="pre">'Feb.'</span></tt>, <tt class="docutils literal"><span class="pre">'March'</span></tt>, <tt class="docutils literal"><span class="pre">'May'</span></tt></td> </tr> <tr><td>O</td> <td>Difference to Greenwich time in hours.</td> <td><tt class="docutils literal"><span class="pre">'+0200'</span></tt></td> </tr> <tr><td>P</td> <td>Time, in 12-hour hours, minutes and 'a.m.'/'p.m.', with minutes left off if they're zero and the special-case strings 'midnight' and 'noon' if appropriate. Proprietary extension.</td> <td><tt class="docutils literal"><span class="pre">'1</span> <span class="pre">a.m.'</span></tt>, <tt class="docutils literal"><span class="pre">'1:30</span> <span class="pre">p.m.'</span></tt>, <tt class="docutils literal"><span class="pre">'midnight'</span></tt>, <tt class="docutils literal"><span class="pre">'noon'</span></tt>, <tt class="docutils literal"><span class="pre">'12:30</span> <span class="pre">p.m.'</span></tt></td> </tr> <tr><td>r</td> <td>RFC 2822 formatted date.</td> <td><tt class="docutils literal"><span class="pre">'Thu,</span> <span class="pre">21</span> <span class="pre">Dec</span> <span class="pre">2000</span> <span class="pre">16:01:07</span> <span class="pre">+0200'</span></tt></td> </tr> <tr><td>s</td> <td>Seconds, 2 digits with leading zeros.</td> <td><tt class="docutils literal"><span class="pre">'00'</span></tt> to <tt class="docutils literal"><span class="pre">'59'</span></tt></td> </tr> <tr><td>S</td> <td>English ordinal suffix for day of the month, 2 characters.</td> <td><tt class="docutils literal"><span class="pre">'st'</span></tt>, <tt class="docutils literal"><span class="pre">'nd'</span></tt>, <tt class="docutils literal"><span class="pre">'rd'</span></tt> or <tt class="docutils literal"><span class="pre">'th'</span></tt></td> </tr> <tr><td>t</td> <td>Number of days in the given month.</td> <td><tt class="docutils literal"><span class="pre">28</span></tt> to <tt class="docutils literal"><span class="pre">31</span></tt></td> </tr> <tr><td>T</td> <td>Time zone of this machine.</td> <td><tt class="docutils literal"><span class="pre">'EST'</span></tt>, <tt class="docutils literal"><span class="pre">'MDT'</span></tt></td> </tr> <tr><td>u</td> <td>Microseconds.</td> <td><tt class="docutils literal"><span class="pre">0</span></tt> to <tt class="docutils literal"><span class="pre">999999</span></tt></td> </tr> <tr><td>U</td> <td>Seconds since the Unix Epoch (January 1 1970 00:00:00 UTC).</td> <td> </td> </tr> <tr><td>w</td> <td>Day of the week, digits without leading zeros.</td> <td><tt class="docutils literal"><span class="pre">'0'</span></tt> (Sunday) to <tt class="docutils literal"><span class="pre">'6'</span></tt> (Saturday)</td> </tr> <tr><td>W</td> <td>ISO-8601 week number of year, with weeks starting on Monday.</td> <td><tt class="docutils literal"><span class="pre">1</span></tt>, <tt class="docutils literal"><span class="pre">53</span></tt></td> </tr> <tr><td>y</td> <td>Year, 2 digits.</td> <td><tt class="docutils literal"><span class="pre">'99'</span></tt></td> </tr> <tr><td>Y</td> <td>Year, 4 digits.</td> <td><tt class="docutils literal"><span class="pre">'1999'</span></tt></td> </tr> <tr><td>z</td> <td>Day of the year.</td> <td><tt class="docutils literal"><span class="pre">0</span></tt> to <tt class="docutils literal"><span class="pre">365</span></tt></td> </tr> <tr><td>Z</td> <td>Time zone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.</td> <td><tt class="docutils literal"><span class="pre">-43200</span></tt> to <tt class="docutils literal"><span class="pre">43200</span></tt></td> </tr> </tbody> </table> <p>Example:</p> <div class="highlight-html+django"><div class="highlight"><pre>It is <span class="cp">{%</span> <span class="k">now</span> <span class="s2">"jS F Y H:i"</span> <span class="cp">%}</span> </pre></div> </div> <p>Note that you can backslash-escape a format string if you want to use the "raw" value. In this example, "f" is backslash-escaped, because otherwise "f" is a format string that displays the time. The "o" doesn't need to be escaped, because it's not a format character:</p> <div class="highlight-html+django"><div class="highlight"><pre>It is the <span class="cp">{%</span> <span class="k">now</span> <span class="s2">"jS o\f F"</span> <span class="cp">%}</span> </pre></div> </div> <p>This would display as "It is the 4th of September".</p> <div class="versionadded"> <span class="title">New in Django 1.2:</span> <a class="reference internal" href="../../releases/1.2.html"><em>Please, see the release notes</em></a></div> <p>The <tt class="docutils literal"><span class="pre">c</span></tt> and <tt class="docutils literal"><span class="pre">u</span></tt> format specification characters were added in Django 1.2.</p> </div> <div class="section" id="s-regroup"> <span id="s-std:templatetag-regroup"></span><span id="regroup"></span><span id="std:templatetag-regroup"></span><h3>regroup<a class="headerlink" href="#regroup" title="Permalink to this headline">¶</a></h3> <p>Regroup a list of alike objects by a common attribute.</p> <p>This complex tag is best illustrated by use of an example: say that <tt class="docutils literal"><span class="pre">people</span></tt> is a list of people represented by dictionaries with <tt class="docutils literal"><span class="pre">first_name</span></tt>, <tt class="docutils literal"><span class="pre">last_name</span></tt>, and <tt class="docutils literal"><span class="pre">gender</span></tt> keys:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">people</span> <span class="o">=</span> <span class="p">[</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'George'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Bush'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Male'</span><span class="p">},</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Bill'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Clinton'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Male'</span><span class="p">},</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Margaret'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Thatcher'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Female'</span><span class="p">},</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Condoleezza'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Rice'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Female'</span><span class="p">},</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Pat'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Smith'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Unknown'</span><span class="p">},</span> <span class="p">]</span> </pre></div> </div> <p>...and you'd like to display a hierarchical list that is ordered by gender, like this:</p> <ul> <li><dl class="first docutils"> <dt>Male:</dt> <dd><ul class="first last simple"> <li>George Bush</li> <li>Bill Clinton</li> </ul> </dd> </dl> </li> <li><dl class="first docutils"> <dt>Female:</dt> <dd><ul class="first last simple"> <li>Margaret Thatcher</li> <li>Condoleezza Rice</li> </ul> </dd> </dl> </li> <li><dl class="first docutils"> <dt>Unknown:</dt> <dd><ul class="first last simple"> <li>Pat Smith</li> </ul> </dd> </dl> </li> </ul> <p>You can use the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></tt> tag to group the list of people by gender. The following snippet of template code would accomplish this:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">people</span> <span class="nv">by</span> <span class="nv">gender</span> <span class="k">as</span> <span class="nv">gender_list</span> <span class="cp">%}</span> <span class="nt"><ul></span> <span class="cp">{%</span> <span class="k">for</span> <span class="nv">gender</span> <span class="k">in</span> <span class="nv">gender_list</span> <span class="cp">%}</span> <span class="nt"><li></span><span class="cp">{{</span> <span class="nv">gender.grouper</span> <span class="cp">}}</span> <span class="nt"><ul></span> <span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">gender.list</span> <span class="cp">%}</span> <span class="nt"><li></span><span class="cp">{{</span> <span class="nv">item.first_name</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">item.last_name</span> <span class="cp">}}</span><span class="nt"></li></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> <span class="nt"></ul></span> <span class="nt"></li></span> <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span> <span class="nt"></ul></span> </pre></div> </div> <p>Let's walk through this example. <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></tt> takes three arguments: the list you want to regroup, the attribute to group by, and the name of the resulting list. Here, we're regrouping the <tt class="docutils literal"><span class="pre">people</span></tt> list by the <tt class="docutils literal"><span class="pre">gender</span></tt> attribute and calling the result <tt class="docutils literal"><span class="pre">gender_list</span></tt>.</p> <p><tt class="docutils literal"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></tt> produces a list (in this case, <tt class="docutils literal"><span class="pre">gender_list</span></tt>) of <strong>group objects</strong>. Each group object has two attributes:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">grouper</span></tt> -- the item that was grouped by (e.g., the string "Male" or "Female").</li> <li><tt class="docutils literal"><span class="pre">list</span></tt> -- a list of all items in this group (e.g., a list of all people with gender='Male').</li> </ul> <p>Note that <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></tt> does not order its input! Our example relies on the fact that the <tt class="docutils literal"><span class="pre">people</span></tt> list was ordered by <tt class="docutils literal"><span class="pre">gender</span></tt> in the first place. If the <tt class="docutils literal"><span class="pre">people</span></tt> list did <em>not</em> order its members by <tt class="docutils literal"><span class="pre">gender</span></tt>, the regrouping would naively display more than one group for a single gender. For example, say the <tt class="docutils literal"><span class="pre">people</span></tt> list was set to this (note that the males are not grouped together):</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">people</span> <span class="o">=</span> <span class="p">[</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Bill'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Clinton'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Male'</span><span class="p">},</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Pat'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Smith'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Unknown'</span><span class="p">},</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Margaret'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Thatcher'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Female'</span><span class="p">},</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'George'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Bush'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Male'</span><span class="p">},</span> <span class="p">{</span><span class="s">'first_name'</span><span class="p">:</span> <span class="s">'Condoleezza'</span><span class="p">,</span> <span class="s">'last_name'</span><span class="p">:</span> <span class="s">'Rice'</span><span class="p">,</span> <span class="s">'gender'</span><span class="p">:</span> <span class="s">'Female'</span><span class="p">},</span> <span class="p">]</span> </pre></div> </div> <p>With this input for <tt class="docutils literal"><span class="pre">people</span></tt>, the example <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></tt> template code above would result in the following output:</p> <ul> <li><dl class="first docutils"> <dt>Male:</dt> <dd><ul class="first last simple"> <li>Bill Clinton</li> </ul> </dd> </dl> </li> <li><dl class="first docutils"> <dt>Unknown:</dt> <dd><ul class="first last simple"> <li>Pat Smith</li> </ul> </dd> </dl> </li> <li><dl class="first docutils"> <dt>Female:</dt> <dd><ul class="first last simple"> <li>Margaret Thatcher</li> </ul> </dd> </dl> </li> <li><dl class="first docutils"> <dt>Male:</dt> <dd><ul class="first last simple"> <li>George Bush</li> </ul> </dd> </dl> </li> <li><dl class="first docutils"> <dt>Female:</dt> <dd><ul class="first last simple"> <li>Condoleezza Rice</li> </ul> </dd> </dl> </li> </ul> <p>The easiest solution to this gotcha is to make sure in your view code that the data is ordered according to how you want to display it.</p> <p>Another solution is to sort the data in the template using the <tt class="docutils literal"><span class="pre">dictsort</span></tt> filter, if your data is in a list of dictionaries:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">people</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:"gender"</span> <span class="nv">by</span> <span class="nv">gender</span> <span class="k">as</span> <span class="nv">gender_list</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-spaceless"> <span id="s-std:templatetag-spaceless"></span><span id="spaceless"></span><span id="std:templatetag-spaceless"></span><h3>spaceless<a class="headerlink" href="#spaceless" title="Permalink to this headline">¶</a></h3> <p>Removes whitespace between HTML tags. This includes tab characters and newlines.</p> <p>Example usage:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span> <span class="nt"><p></span> <span class="nt"><a</span> <span class="na">href=</span><span class="s">"foo/"</span><span class="nt">></span>Foo<span class="nt"></a></span> <span class="nt"></p></span> <span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span> </pre></div> </div> <p>This example would return this HTML:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><p><a</span> <span class="na">href=</span><span class="s">"foo/"</span><span class="nt">></span>Foo<span class="nt"></a></p></span> </pre></div> </div> <p>Only space between <em>tags</em> is removed -- not space between tags and text. In this example, the space around <tt class="docutils literal"><span class="pre">Hello</span></tt> won't be stripped:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span> <span class="nt"><strong></span> Hello <span class="nt"></strong></span> <span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="s-ssi"> <span id="s-std:templatetag-ssi"></span><span id="ssi"></span><span id="std:templatetag-ssi"></span><h3>ssi<a class="headerlink" href="#ssi" title="Permalink to this headline">¶</a></h3> <p>Output the contents of a given file into the page.</p> <p>Like a simple "include" tag, <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">ssi</span> <span class="pre">%}</span></tt> includes the contents of another file -- which must be specified using an absolute path -- in the current page:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">ssi</span> <span class="o">/</span><span class="nv">home</span><span class="o">/</span><span class="nv">html</span><span class="o">/</span><span class="nv">ljworld.com</span><span class="o">/</span><span class="nv">includes</span><span class="o">/</span><span class="nv">right_generic.html</span> <span class="cp">%}</span> </pre></div> </div> <p>If the optional "parsed" parameter is given, the contents of the included file are evaluated as template code, within the current context:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">ssi</span> <span class="o">/</span><span class="nv">home</span><span class="o">/</span><span class="nv">html</span><span class="o">/</span><span class="nv">ljworld.com</span><span class="o">/</span><span class="nv">includes</span><span class="o">/</span><span class="nv">right_generic.html</span> <span class="nv">parsed</span> <span class="cp">%}</span> </pre></div> </div> <p>Note that if you use <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">ssi</span> <span class="pre">%}</span></tt>, you'll need to define <a class="reference internal" href="../settings.html#std:setting-ALLOWED_INCLUDE_ROOTS"><tt class="xref std std-setting docutils literal"><span class="pre">ALLOWED_INCLUDE_ROOTS</span></tt></a> in your Django settings, as a security measure.</p> <p>See also: <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">include</span> <span class="pre">%}</span></tt>.</p> </div> <div class="section" id="s-templatetag"> <span id="s-std:templatetag-templatetag"></span><span id="templatetag"></span><span id="std:templatetag-templatetag"></span><h3>templatetag<a class="headerlink" href="#templatetag" title="Permalink to this headline">¶</a></h3> <p>Output one of the syntax characters used to compose template tags.</p> <p>Since the template system has no concept of "escaping", to display one of the bits used in template tags, you must use the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">templatetag</span> <span class="pre">%}</span></tt> tag.</p> <p>The argument tells which template bit to output:</p> <table class="docutils"> <colgroup> <col width="72%" /> <col width="28%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">Argument</th> <th class="head">Outputs</th> </tr> </thead> <tbody valign="top"> <tr><td><tt class="docutils literal"><span class="pre">openblock</span></tt></td> <td><tt class="docutils literal"><span class="pre">{%</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">closeblock</span></tt></td> <td><tt class="docutils literal"><span class="pre">%}</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">openvariable</span></tt></td> <td><tt class="docutils literal"><span class="pre">{{</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">closevariable</span></tt></td> <td><tt class="docutils literal"><span class="pre">}}</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">openbrace</span></tt></td> <td><tt class="docutils literal"><span class="pre">{</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">closebrace</span></tt></td> <td><tt class="docutils literal"><span class="pre">}</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">opencomment</span></tt></td> <td><tt class="docutils literal"><span class="pre">{#</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">closecomment</span></tt></td> <td><tt class="docutils literal"><span class="pre">#}</span></tt></td> </tr> </tbody> </table> </div> <div class="section" id="s-url"> <span id="s-std:templatetag-url"></span><span id="url"></span><span id="std:templatetag-url"></span><h3>url<a class="headerlink" href="#url" title="Permalink to this headline">¶</a></h3> <p>Returns an absolute URL (i.e., a URL without the domain name) matching a given view function and optional parameters. This is a way to output links without violating the DRY principle by having to hard-code URLs in your templates:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">path.to.some_view</span> <span class="nv">v1</span> <span class="nv">v2</span> <span class="cp">%}</span> </pre></div> </div> <p>The first argument is a path to a view function in the format <tt class="docutils literal"><span class="pre">package.package.module.function</span></tt>. Additional arguments are optional and should be space-separated values that will be used as arguments in the URL. The example above shows passing positional arguments. Alternatively you may use keyword syntax:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">path.to.some_view</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> </pre></div> </div> <p>Do not mix both positional and keyword syntax in a single call. All arguments required by the URLconf should be present.</p> <p>For example, suppose you have a view, <tt class="docutils literal"><span class="pre">app_views.client</span></tt>, whose URLconf takes a client ID (here, <tt class="docutils literal"><span class="pre">client()</span></tt> is a method inside the views file <tt class="docutils literal"><span class="pre">app_views.py</span></tt>). The URLconf line might look like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="s">'^client/(\d+)/$'</span><span class="p">,</span> <span class="s">'app_views.client'</span><span class="p">)</span> </pre></div> </div> <p>If this app's URLconf is included into the project's URLconf under a path such as this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="s">'^clients/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">'project_name.app_name.urls'</span><span class="p">))</span> </pre></div> </div> <p>...then, in a template, you can create a link to this view like this:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">app_views.client</span> <span class="nv">client.id</span> <span class="cp">%}</span> </pre></div> </div> <p>The template tag will output the string <tt class="docutils literal"><span class="pre">/clients/client/123/</span></tt>.</p> <div class="versionadded"> <span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div> <p>If you're using <a class="reference internal" href="../../topics/http/urls.html#naming-url-patterns"><em>named URL patterns</em></a>, you can refer to the name of the pattern in the <tt class="docutils literal"><span class="pre">url</span></tt> tag instead of using the path to the view.</p> <p>Note that if the URL you're reversing doesn't exist, you'll get an <tt class="xref py py-exc docutils literal"><span class="pre">NoReverseMatch</span></tt> exception raised, which will cause your site to display an error page.</p> <div class="versionadded"> <span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div> <p>If you'd like to retrieve a URL without displaying it, you can use a slightly different call:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">path.to.view</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> <span class="nt"><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span>I'm linking to <span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="nt"></a></span> </pre></div> </div> <p>This <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">...</span> <span class="pre">as</span> <span class="pre">var</span> <span class="pre">%}</span></tt> syntax will <em>not</em> cause an error if the view is missing. In practice you'll use this to link to views that are optional:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">path.to.view</span> <span class="k">as</span> <span class="nv">the_url</span> <span class="cp">%}</span> <span class="cp">{%</span> <span class="k">if</span> <span class="nv">the_url</span> <span class="cp">%}</span> <span class="nt"><a</span> <span class="na">href=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">></span>Link to optional stuff<span class="nt"></a></span> <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span> </pre></div> </div> <div class="versionadded"> <span class="title">New in Django 1.1:</span> <a class="reference internal" href="../../releases/1.1.html"><em>Please, see the release notes</em></a></div> <p>If you'd like to retrieve a namespaced URL, specify the fully qualified name:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">myapp</span><span class="o">:</span><span class="nv">view</span><span class="o">-</span><span class="nv">name</span> <span class="cp">%}</span> </pre></div> </div> <p>This will follow the normal <a class="reference internal" href="../../topics/http/urls.html#topics-http-reversing-url-namespaces"><em>namespaced URL resolution strategy</em></a>, including using any hints provided by the context as to the current application.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.2:</span> <a class="reference internal" href="../../releases/1.2.html"><em>Please, see the release notes</em></a></div> <p>For backwards compatibility, the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">%}</span></tt> tag also supports the use of commas to separate arguments. You shouldn't use this in any new projects, but for the sake of the people who are still using it, here's what it looks like:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">path.to.view</span> <span class="nv">arg</span><span class="o">,</span><span class="nv">arg2</span> <span class="cp">%}</span> <span class="cp">{%</span> <span class="k">url</span> <span class="nv">path.to.view</span> <span class="nv">arg</span><span class="o">,</span> <span class="nv">arg2</span> <span class="cp">%}</span> </pre></div> </div> <p>This syntax doesn't support the use of literal commas, or or equals signs. Did we mention you shouldn't use this syntax in any new projects?</p> </div> <div class="section" id="s-widthratio"> <span id="s-std:templatetag-widthratio"></span><span id="widthratio"></span><span id="std:templatetag-widthratio"></span><h3>widthratio<a class="headerlink" href="#widthratio" title="Permalink to this headline">¶</a></h3> <p>For creating bar charts and such, this tag calculates the ratio of a given value to a maximum value, and then applies that ratio to a constant.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><img</span> <span class="na">src=</span><span class="s">"bar.gif"</span> <span class="na">height=</span><span class="s">"10"</span> <span class="na">width=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">widthratio</span> <span class="nv">this_value</span> <span class="nv">max_value</span> <span class="m">100</span> <span class="cp">%}</span><span class="s">"</span> <span class="nt">/></span> </pre></div> </div> <p>Above, if <tt class="docutils literal"><span class="pre">this_value</span></tt> is 175 and <tt class="docutils literal"><span class="pre">max_value</span></tt> is 200, the image in the above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5 which is rounded up to 88).</p> </div> <div class="section" id="s-with"> <span id="s-std:templatetag-with"></span><span id="with"></span><span id="std:templatetag-with"></span><h3>with<a class="headerlink" href="#with" title="Permalink to this headline">¶</a></h3> <div class="versionadded"> <span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div> <p>Caches a complex variable under a simpler name. This is useful when accessing an "expensive" method (e.g., one that hits the database) multiple times.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">with</span> <span class="nv">business.employees.count</span> <span class="k">as</span> <span class="nv">total</span> <span class="cp">%}</span> <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> <span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span> </pre></div> </div> <p>The populated variable (in the example above, <tt class="docutils literal"><span class="pre">total</span></tt>) is only available between the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">with</span> <span class="pre">%}</span></tt> and <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">endwith</span> <span class="pre">%}</span></tt> tags.</p> </div> </div> <div class="section" id="s-built-in-filter-reference"> <span id="s-ref-templates-builtins-filters"></span><span id="built-in-filter-reference"></span><span id="ref-templates-builtins-filters"></span><h2>Built-in filter reference<a class="headerlink" href="#built-in-filter-reference" title="Permalink to this headline">¶</a></h2> <div class="section" id="s-add"> <span id="s-std:templatefilter-add"></span><span id="add"></span><span id="std:templatefilter-add"></span><h3>add<a class="headerlink" href="#add" title="Permalink to this headline">¶</a></h3> <p>Adds the argument to the value.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">add</span><span class="s2">:"2"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">4</span></tt>, then the output will be <tt class="docutils literal"><span class="pre">6</span></tt>.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.2:</span> The following behavior didn't exist in previous Django versions.</div> <p>This filter will first try to coerce both values to integers. If this fails, it'll attempt to add the values together anyway. This will work on some data types (strings, list, etc.) and fail on others. If it fails, the result will be an empty string.</p> <p>For example, if we have:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> </pre></div> </div> <p>and <tt class="docutils literal"><span class="pre">first</span></tt> is <tt class="docutils literal"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></tt> and <tt class="docutils literal"><span class="pre">second</span></tt> is <tt class="docutils literal"><span class="pre">[4,</span> <span class="pre">5,</span> <span class="pre">6]</span></tt>, then the output will be <tt class="docutils literal"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3,</span> <span class="pre">4,</span> <span class="pre">5,</span> <span class="pre">6]</span></tt>.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Keep in mind that strings that can both be coerced to integers will be, and thus will be will be <em>summed</em>, not concatenated, as in the first example above.</p> </div> </div> <div class="section" id="s-addslashes"> <span id="s-std:templatefilter-addslashes"></span><span id="addslashes"></span><span id="std:templatefilter-addslashes"></span><h3>addslashes<a class="headerlink" href="#addslashes" title="Permalink to this headline">¶</a></h3> <p>Adds slashes before quotes. Useful for escaping strings in CSV, for example.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">addslashes</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"I'm</span> <span class="pre">using</span> <span class="pre">Django"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"I\'m</span> <span class="pre">using</span> <span class="pre">Django"</span></tt>.</p> </div> <div class="section" id="s-capfirst"> <span id="s-std:templatefilter-capfirst"></span><span id="capfirst"></span><span id="std:templatefilter-capfirst"></span><h3>capfirst<a class="headerlink" href="#capfirst" title="Permalink to this headline">¶</a></h3> <p>Capitalizes the first character of the value.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">capfirst</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"django"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"Django"</span></tt>.</p> </div> <div class="section" id="s-center"> <span id="s-std:templatefilter-center"></span><span id="center"></span><span id="std:templatefilter-center"></span><h3>center<a class="headerlink" href="#center" title="Permalink to this headline">¶</a></h3> <p>Centers the value in a field of a given width.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre>"<span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">center</span><span class="s2">:"15"</span> <span class="cp">}}</span>" </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Django"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"</span> <span class="pre">Django</span> <span class="pre">"</span></tt>.</p> </div> <div class="section" id="s-cut"> <span id="s-std:templatefilter-cut"></span><span id="cut"></span><span id="std:templatefilter-cut"></span><h3>cut<a class="headerlink" href="#cut" title="Permalink to this headline">¶</a></h3> <p>Removes all values of arg from the given string.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">cut</span><span class="s2">:" "</span><span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"String</span> <span class="pre">with</span> <span class="pre">spaces"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"Stringwithspaces"</span></tt>.</p> </div> <div class="section" id="s-date"> <span id="s-std:templatefilter-date"></span><span id="date"></span><span id="std:templatefilter-date"></span><h3>date<a class="headerlink" href="#date" title="Permalink to this headline">¶</a></h3> <p>Formats a date according to the given format.</p> <p>Given format can be one of the predefined ones <tt class="docutils literal"><span class="pre">DATE_FORMAT</span></tt>, <tt class="docutils literal"><span class="pre">DATETIME_FORMAT</span></tt>, <tt class="docutils literal"><span class="pre">SHORT_DATE_FORMAT</span></tt> or <tt class="docutils literal"><span class="pre">SHORT_DATETIME_FORMAT</span></tt>, or a custom format, same as the <a class="reference internal" href="#std:templatetag-now"><tt class="xref std std-ttag docutils literal"><span class="pre">now</span></tt></a> tag. Note that predefined formats may vary depending on the current locale.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"D d M Y"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is a <tt class="docutils literal"><span class="pre">datetime</span></tt> object (e.g., the result of <tt class="docutils literal"><span class="pre">datetime.datetime.now()</span></tt>), the output will be the string <tt class="docutils literal"><span class="pre">'Wed</span> <span class="pre">09</span> <span class="pre">Jan</span> <span class="pre">2008'</span></tt>.</p> <p>Another example:</p> <p>Assuming that <a class="reference internal" href="../settings.html#std:setting-USE_L10N"><tt class="xref std std-setting docutils literal"><span class="pre">USE_L10N</span></tt></a> is <tt class="xref docutils literal"><span class="pre">True</span></tt> and <a class="reference internal" href="../settings.html#std:setting-LANGUAGE_CODE"><tt class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_CODE</span></tt></a> is, for example, <tt class="docutils literal"><span class="pre">"es"</span></tt>, then for:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"SHORT_DATE_FORMAT"</span> <span class="cp">}}</span> </pre></div> </div> <p>the output will be the string <tt class="docutils literal"><span class="pre">"09/01/2008"</span></tt> (The <tt class="docutils literal"><span class="pre">"SHORT_DATE_FORMAT"</span></tt> format specifier for the <tt class="docutils literal"><span class="pre">es</span></tt> locale as shipped with Django is <tt class="docutils literal"><span class="pre">"d/m/Y"</span></tt>).</p> <p>When used without a format string:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span> <span class="cp">}}</span> </pre></div> </div> <p>...the formatting string defined in the <a class="reference internal" href="../settings.html#std:setting-DATE_FORMAT"><tt class="xref std std-setting docutils literal"><span class="pre">DATE_FORMAT</span></tt></a> setting will be used, without applying any localization.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.2:</span> Predefined formats can now be influenced by the current locale.</div> </div> <div class="section" id="s-default"> <span id="s-std:templatefilter-default"></span><span id="default"></span><span id="std:templatefilter-default"></span><h3>default<a class="headerlink" href="#default" title="Permalink to this headline">¶</a></h3> <p>If value evaluates to <tt class="xref docutils literal"><span class="pre">False</span></tt>, use given default. Otherwise, use the value.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">default</span><span class="s2">:"nothing"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">""</span></tt> (the empty string), the output will be <tt class="docutils literal"><span class="pre">nothing</span></tt>.</p> </div> <div class="section" id="s-default-if-none"> <span id="s-std:templatefilter-default_if_none"></span><span id="default-if-none"></span><span id="std:templatefilter-default_if_none"></span><h3>default_if_none<a class="headerlink" href="#default-if-none" title="Permalink to this headline">¶</a></h3> <p>If (and only if) value is <tt class="xref docutils literal"><span class="pre">None</span></tt>, use given default. Otherwise, use the value.</p> <p>Note that if an empty string is given, the default value will <em>not</em> be used. Use the <tt class="docutils literal"><span class="pre">default</span></tt> filter if you want to fallback for empty strings.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">default_if_none</span><span class="s2">:"nothing"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="xref docutils literal"><span class="pre">None</span></tt>, the output will be the string <tt class="docutils literal"><span class="pre">"nothing"</span></tt>.</p> </div> <div class="section" id="s-dictsort"> <span id="s-std:templatefilter-dictsort"></span><span id="dictsort"></span><span id="std:templatefilter-dictsort"></span><h3>dictsort<a class="headerlink" href="#dictsort" title="Permalink to this headline">¶</a></h3> <p>Takes a list of dictionaries and returns that list sorted by the key given in the argument.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:"name"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">[</span> <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'zed'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mi">19</span><span class="p">},</span> <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'amy'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mi">22</span><span class="p">},</span> <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'joe'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mi">31</span><span class="p">},</span> <span class="p">]</span> </pre></div> </div> <p>then the output would be:</p> <div class="highlight-python"><div class="highlight"><pre><span class="p">[</span> <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'amy'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mi">22</span><span class="p">},</span> <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'joe'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mi">31</span><span class="p">},</span> <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'zed'</span><span class="p">,</span> <span class="s">'age'</span><span class="p">:</span> <span class="mi">19</span><span class="p">},</span> <span class="p">]</span> </pre></div> </div> </div> <div class="section" id="s-dictsortreversed"> <span id="s-std:templatefilter-dictsortreversed"></span><span id="dictsortreversed"></span><span id="std:templatefilter-dictsortreversed"></span><h3>dictsortreversed<a class="headerlink" href="#dictsortreversed" title="Permalink to this headline">¶</a></h3> <p>Takes a list of dictionaries and returns that list sorted in reverse order by the key given in the argument. This works exactly the same as the above filter, but the returned value will be in reverse order.</p> </div> <div class="section" id="s-divisibleby"> <span id="s-std:templatefilter-divisibleby"></span><span id="divisibleby"></span><span id="std:templatefilter-divisibleby"></span><h3>divisibleby<a class="headerlink" href="#divisibleby" title="Permalink to this headline">¶</a></h3> <p>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> if the value is divisible by the argument.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">divisibleby</span><span class="s2">:"3"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">21</span></tt>, the output would be <tt class="xref docutils literal"><span class="pre">True</span></tt>.</p> </div> <div class="section" id="s-escape"> <span id="s-std:templatefilter-escape"></span><span id="escape"></span><span id="std:templatefilter-escape"></span><h3>escape<a class="headerlink" href="#escape" title="Permalink to this headline">¶</a></h3> <p>Escapes a string's HTML. Specifically, it makes these replacements:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre"><</span></tt> is converted to <tt class="docutils literal"><span class="pre">&lt;</span></tt></li> <li><tt class="docutils literal"><span class="pre">></span></tt> is converted to <tt class="docutils literal"><span class="pre">&gt;</span></tt></li> <li><tt class="docutils literal"><span class="pre">'</span></tt> (single quote) is converted to <tt class="docutils literal"><span class="pre">&#39;</span></tt></li> <li><tt class="docutils literal"><span class="pre">"</span></tt> (double quote) is converted to <tt class="docutils literal"><span class="pre">&quot;</span></tt></li> <li><tt class="docutils literal"><span class="pre">&</span></tt> is converted to <tt class="docutils literal"><span class="pre">&amp;</span></tt></li> </ul> <p>The escaping is only applied when the string is output, so it does not matter where in a chained sequence of filters you put <tt class="docutils literal"><span class="pre">escape</span></tt>: it will always be applied as though it were the last filter. If you want escaping to be applied immediately, use the <tt class="docutils literal"><span class="pre">force_escape</span></tt> filter.</p> <p>Applying <tt class="docutils literal"><span class="pre">escape</span></tt> to a variable that would normally have auto-escaping applied to the result will only result in one round of escaping being done. So it is safe to use this function even in auto-escaping environments. If you want multiple escaping passes to be applied, use the <tt class="docutils literal"><span class="pre">force_escape</span></tt> filter.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.0:</span> Due to auto-escaping, the behavior of this filter has changed slightly. The replacements are only made once, after all other filters are applied -- including filters before and after it.</div> </div> <div class="section" id="s-escapejs"> <span id="s-std:templatefilter-escapejs"></span><span id="escapejs"></span><span id="std:templatefilter-escapejs"></span><h3>escapejs<a class="headerlink" href="#escapejs" title="Permalink to this headline">¶</a></h3> <div class="versionadded"> <span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div> <p>Escapes characters for use in JavaScript strings. This does <em>not</em> make the string safe for use in HTML, but does protect you from syntax errors when using templates to generate JavaScript/JSON.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">escapejs</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"testing\r\njavascript</span> <span class="pre">\'string"</span> <span class="pre"><b>escaping</b>"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"testing\\u000D\\u000Ajavascript</span> <span class="pre">\\u0027string\\u0022</span> <span class="pre">\\u003Cb\\u003Eescaping\\u003C/b\\u003E"</span></tt>.</p> </div> <div class="section" id="s-filesizeformat"> <span id="s-std:templatefilter-filesizeformat"></span><span id="filesizeformat"></span><span id="std:templatefilter-filesizeformat"></span><h3>filesizeformat<a class="headerlink" href="#filesizeformat" title="Permalink to this headline">¶</a></h3> <p>Format the value like a 'human-readable' file size (i.e. <tt class="docutils literal"><span class="pre">'13</span> <span class="pre">KB'</span></tt>, <tt class="docutils literal"><span class="pre">'4.1</span> <span class="pre">MB'</span></tt>, <tt class="docutils literal"><span class="pre">'102</span> <span class="pre">bytes'</span></tt>, etc).</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">filesizeformat</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is 123456789, the output would be <tt class="docutils literal"><span class="pre">117.7</span> <span class="pre">MB</span></tt>.</p> </div> <div class="section" id="s-first"> <span id="s-std:templatefilter-first"></span><span id="first"></span><span id="std:templatefilter-first"></span><h3>first<a class="headerlink" href="#first" title="Permalink to this headline">¶</a></h3> <p>Returns the first item in a list.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">first</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is the list <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></tt>, the output will be <tt class="docutils literal"><span class="pre">'a'</span></tt>.</p> </div> <div class="section" id="s-fix-ampersands"> <span id="s-std:templatefilter-fix_ampersands"></span><span id="fix-ampersands"></span><span id="std:templatefilter-fix_ampersands"></span><h3>fix_ampersands<a class="headerlink" href="#fix-ampersands" title="Permalink to this headline">¶</a></h3> <div class="versionchanged"> <span class="title">Changed in Django 1.0:</span> This is rarely useful as ampersands are now automatically escaped. See <a class="reference internal" href="#escape">escape</a> for more information.</div> <p>Replaces ampersands with <tt class="docutils literal"><span class="pre">&amp;</span></tt> entities.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">fix_ampersands</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Tom</span> <span class="pre">&</span> <span class="pre">Jerry</span></tt>, the output will be <tt class="docutils literal"><span class="pre">Tom</span> <span class="pre">&amp;</span> <span class="pre">Jerry</span></tt>.</p> </div> <div class="section" id="s-floatformat"> <span id="s-std:templatefilter-floatformat"></span><span id="floatformat"></span><span id="std:templatefilter-floatformat"></span><h3>floatformat<a class="headerlink" href="#floatformat" title="Permalink to this headline">¶</a></h3> <p>When used without an argument, rounds a floating-point number to one decimal place -- but only if there's a decimal part to be displayed. For example:</p> <table class="docutils"> <colgroup> <col width="26%" /> <col width="57%" /> <col width="17%" /> </colgroup> <thead valign="bottom"> <tr><th class="head"><tt class="docutils literal"><span class="pre">value</span></tt></th> <th class="head">Template</th> <th class="head">Output</th> </tr> </thead> <tbody valign="top"> <tr><td><tt class="docutils literal"><span class="pre">34.23234</span></tt></td> <td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat</span> <span class="pre">}}</span></tt></td> <td><tt class="docutils literal"><span class="pre">34.2</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">34.00000</span></tt></td> <td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat</span> <span class="pre">}}</span></tt></td> <td><tt class="docutils literal"><span class="pre">34</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">34.26000</span></tt></td> <td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat</span> <span class="pre">}}</span></tt></td> <td><tt class="docutils literal"><span class="pre">34.3</span></tt></td> </tr> </tbody> </table> <p>If used with a numeric integer argument, <tt class="docutils literal"><span class="pre">floatformat</span></tt> rounds a number to that many decimal places. For example:</p> <table class="docutils"> <colgroup> <col width="24%" /> <col width="57%" /> <col width="20%" /> </colgroup> <thead valign="bottom"> <tr><th class="head"><tt class="docutils literal"><span class="pre">value</span></tt></th> <th class="head">Template</th> <th class="head">Output</th> </tr> </thead> <tbody valign="top"> <tr><td><tt class="docutils literal"><span class="pre">34.23234</span></tt></td> <td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:3</span> <span class="pre">}}</span></tt></td> <td><tt class="docutils literal"><span class="pre">34.232</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">34.00000</span></tt></td> <td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:3</span> <span class="pre">}}</span></tt></td> <td><tt class="docutils literal"><span class="pre">34.000</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">34.26000</span></tt></td> <td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:3</span> <span class="pre">}}</span></tt></td> <td><tt class="docutils literal"><span class="pre">34.260</span></tt></td> </tr> </tbody> </table> <p>If the argument passed to <tt class="docutils literal"><span class="pre">floatformat</span></tt> is negative, it will round a number to that many decimal places -- but only if there's a decimal part to be displayed. For example:</p> <table class="docutils"> <colgroup> <col width="22%" /> <col width="59%" /> <col width="19%" /> </colgroup> <thead valign="bottom"> <tr><th class="head"><tt class="docutils literal"><span class="pre">value</span></tt></th> <th class="head">Template</th> <th class="head">Output</th> </tr> </thead> <tbody valign="top"> <tr><td><tt class="docutils literal"><span class="pre">34.23234</span></tt></td> <td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:"-3"</span> <span class="pre">}}</span></tt></td> <td><tt class="docutils literal"><span class="pre">34.232</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">34.00000</span></tt></td> <td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:"-3"</span> <span class="pre">}}</span></tt></td> <td><tt class="docutils literal"><span class="pre">34</span></tt></td> </tr> <tr><td><tt class="docutils literal"><span class="pre">34.26000</span></tt></td> <td><tt class="docutils literal"><span class="pre">{{</span> <span class="pre">value|floatformat:"-3"</span> <span class="pre">}}</span></tt></td> <td><tt class="docutils literal"><span class="pre">34.260</span></tt></td> </tr> </tbody> </table> <p>Using <tt class="docutils literal"><span class="pre">floatformat</span></tt> with no argument is equivalent to using <tt class="docutils literal"><span class="pre">floatformat</span></tt> with an argument of <tt class="docutils literal"><span class="pre">-1</span></tt>.</p> </div> <div class="section" id="s-force-escape"> <span id="s-std:templatefilter-force_escape"></span><span id="force-escape"></span><span id="std:templatefilter-force_escape"></span><h3>force_escape<a class="headerlink" href="#force-escape" title="Permalink to this headline">¶</a></h3> <div class="versionadded"> <span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div> <p>Applies HTML escaping to a string (see the <tt class="docutils literal"><span class="pre">escape</span></tt> filter for details). This filter is applied <em>immediately</em> and returns a new, escaped string. This is useful in the rare cases where you need multiple escaping or want to apply other filters to the escaped results. Normally, you want to use the <tt class="docutils literal"><span class="pre">escape</span></tt> filter.</p> </div> <div class="section" id="s-get-digit"> <span id="s-std:templatefilter-get_digit"></span><span id="get-digit"></span><span id="std:templatefilter-get_digit"></span><h3>get_digit<a class="headerlink" href="#get-digit" title="Permalink to this headline">¶</a></h3> <p>Given a whole number, returns the requested digit, where 1 is the right-most digit, 2 is the second-right-most digit, etc. Returns the original value for invalid input (if input or argument is not an integer, or if argument is less than 1). Otherwise, output is always an integer.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">get_digit</span><span class="s2">:"2"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">123456789</span></tt>, the output will be <tt class="docutils literal"><span class="pre">8</span></tt>.</p> </div> <div class="section" id="s-iriencode"> <span id="s-std:templatefilter-iriencode"></span><span id="iriencode"></span><span id="std:templatefilter-iriencode"></span><h3>iriencode<a class="headerlink" href="#iriencode" title="Permalink to this headline">¶</a></h3> <p>Converts an IRI (Internationalized Resource Identifier) to a string that is suitable for including in a URL. This is necessary if you're trying to use strings containing non-ASCII characters in a URL.</p> <p>It's safe to use this filter on a string that has already gone through the <tt class="docutils literal"><span class="pre">urlencode</span></tt> filter.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">iriencode</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"?test=1&me=2"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"?test=1&amp;me=2"</span></tt>.</p> </div> <div class="section" id="s-join"> <span id="s-std:templatefilter-join"></span><span id="join"></span><span id="std:templatefilter-join"></span><h3>join<a class="headerlink" href="#join" title="Permalink to this headline">¶</a></h3> <p>Joins a list with a string, like Python's <tt class="docutils literal"><span class="pre">str.join(list)</span></tt></p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">join</span><span class="s2">:" // "</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is the list <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></tt>, the output will be the string <tt class="docutils literal"><span class="pre">"a</span> <span class="pre">//</span> <span class="pre">b</span> <span class="pre">//</span> <span class="pre">c"</span></tt>.</p> </div> <div class="section" id="s-last"> <span id="s-std:templatefilter-last"></span><span id="last"></span><span id="std:templatefilter-last"></span><h3>last<a class="headerlink" href="#last" title="Permalink to this headline">¶</a></h3> <div class="versionadded"> <span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div> <p>Returns the last item in a list.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">last</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is the list <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></tt>, the output will be the string <tt class="docutils literal"><span class="pre">"d"</span></tt>.</p> </div> <div class="section" id="s-length"> <span id="s-std:templatefilter-length"></span><span id="length"></span><span id="std:templatefilter-length"></span><h3>length<a class="headerlink" href="#length" title="Permalink to this headline">¶</a></h3> <p>Returns the length of the value. This works for both strings and lists.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">length</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></tt>, the output will be <tt class="docutils literal"><span class="pre">4</span></tt>.</p> </div> <div class="section" id="s-length-is"> <span id="s-std:templatefilter-length_is"></span><span id="length-is"></span><span id="std:templatefilter-length_is"></span><h3>length_is<a class="headerlink" href="#length-is" title="Permalink to this headline">¶</a></h3> <p>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> if the value's length is the argument, or <tt class="xref docutils literal"><span class="pre">False</span></tt> otherwise.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">length_is</span><span class="s2">:"4"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></tt>, the output will be <tt class="xref docutils literal"><span class="pre">True</span></tt>.</p> </div> <div class="section" id="s-linebreaks"> <span id="s-std:templatefilter-linebreaks"></span><span id="linebreaks"></span><span id="std:templatefilter-linebreaks"></span><h3>linebreaks<a class="headerlink" href="#linebreaks" title="Permalink to this headline">¶</a></h3> <p>Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML line break (<tt class="docutils literal"><span class="pre"><br</span> <span class="pre">/></span></tt>) and a new line followed by a blank line becomes a paragraph break (<tt class="docutils literal"><span class="pre"></p></span></tt>).</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">linebreaks</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Joel\nis</span> <span class="pre">a</span> <span class="pre">slug</span></tt>, the output will be <tt class="docutils literal"><span class="pre"><p>Joel<br</span> <span class="pre">/>is</span> <span class="pre">a</span> <span class="pre">slug</p></span></tt>.</p> </div> <div class="section" id="s-linebreaksbr"> <span id="s-std:templatefilter-linebreaksbr"></span><span id="linebreaksbr"></span><span id="std:templatefilter-linebreaksbr"></span><h3>linebreaksbr<a class="headerlink" href="#linebreaksbr" title="Permalink to this headline">¶</a></h3> <p>Converts all newlines in a piece of plain text to HTML line breaks (<tt class="docutils literal"><span class="pre"><br</span> <span class="pre">/></span></tt>).</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">linebreaksbr</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Joel\nis</span> <span class="pre">a</span> <span class="pre">slug</span></tt>, the output will be <tt class="docutils literal"><span class="pre">Joel<br</span> <span class="pre">/>is</span> <span class="pre">a</span> <span class="pre">slug</span></tt>.</p> </div> <div class="section" id="s-linenumbers"> <span id="s-std:templatefilter-linenumbers"></span><span id="linenumbers"></span><span id="std:templatefilter-linenumbers"></span><h3>linenumbers<a class="headerlink" href="#linenumbers" title="Permalink to this headline">¶</a></h3> <p>Displays text with line numbers.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">linenumbers</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is:</p> <div class="highlight-html+django"><div class="highlight"><pre>one two three </pre></div> </div> <p>the output will be:</p> <div class="highlight-html+django"><div class="highlight"><pre>1. one 2. two 3. three </pre></div> </div> </div> <div class="section" id="s-ljust"> <span id="s-std:templatefilter-ljust"></span><span id="ljust"></span><span id="std:templatefilter-ljust"></span><h3>ljust<a class="headerlink" href="#ljust" title="Permalink to this headline">¶</a></h3> <p>Left-aligns the value in a field of a given width.</p> <p><strong>Argument:</strong> field size</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre>"<span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">ljust</span><span class="s2">:"10"</span> <span class="cp">}}</span>" </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Django</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"Django</span> <span class="pre">"</span></tt>.</p> </div> <div class="section" id="s-lower"> <span id="s-std:templatefilter-lower"></span><span id="lower"></span><span id="std:templatefilter-lower"></span><h3>lower<a class="headerlink" href="#lower" title="Permalink to this headline">¶</a></h3> <p>Converts a string into all lowercase.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Still</span> <span class="pre">MAD</span> <span class="pre">At</span> <span class="pre">Yoko</span></tt>, the output will be <tt class="docutils literal"><span class="pre">still</span> <span class="pre">mad</span> <span class="pre">at</span> <span class="pre">yoko</span></tt>.</p> </div> <div class="section" id="s-make-list"> <span id="s-std:templatefilter-make_list"></span><span id="make-list"></span><span id="std:templatefilter-make_list"></span><h3>make_list<a class="headerlink" href="#make-list" title="Permalink to this headline">¶</a></h3> <p>Returns the value turned into a list. For an integer, it's a list of digits. For a string, it's a list of characters.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">make_list</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is the string <tt class="docutils literal"><span class="pre">"Joel"</span></tt>, the output would be the list <tt class="docutils literal"><span class="pre">[u'J',</span> <span class="pre">u'o',</span> <span class="pre">u'e',</span> <span class="pre">u'l']</span></tt>. If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">123</span></tt>, the output will be the list <tt class="docutils literal"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></tt>.</p> </div> <div class="section" id="s-phone2numeric"> <span id="s-std:templatefilter-phone2numeric"></span><span id="phone2numeric"></span><span id="std:templatefilter-phone2numeric"></span><h3>phone2numeric<a class="headerlink" href="#phone2numeric" title="Permalink to this headline">¶</a></h3> <p>Converts a phone number (possibly containing letters) to its numerical equivalent.</p> <p>The input doesn't have to be a valid phone number. This will happily convert any string.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">phone2numeric</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">800-COLLECT</span></tt>, the output will be <tt class="docutils literal"><span class="pre">800-2655328</span></tt>.</p> </div> <div class="section" id="s-pluralize"> <span id="s-std:templatefilter-pluralize"></span><span id="pluralize"></span><span id="std:templatefilter-pluralize"></span><h3>pluralize<a class="headerlink" href="#pluralize" title="Permalink to this headline">¶</a></h3> <p>Returns a plural suffix if the value is not 1. By default, this suffix is <tt class="docutils literal"><span class="pre">'s'</span></tt>.</p> <p>Example:</p> <div class="highlight-html+django"><div class="highlight"><pre>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>. </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">num_messages</span></tt> is <tt class="docutils literal"><span class="pre">1</span></tt>, the output will be <tt class="docutils literal"><span class="pre">You</span> <span class="pre">have</span> <span class="pre">1</span> <span class="pre">message.</span></tt> If <tt class="docutils literal"><span class="pre">num_messages</span></tt> is <tt class="docutils literal"><span class="pre">2</span></tt> the output will be <tt class="docutils literal"><span class="pre">You</span> <span class="pre">have</span> <span class="pre">2</span> <span class="pre">messages.</span></tt></p> <p>For words that require a suffix other than <tt class="docutils literal"><span class="pre">'s'</span></tt>, you can provide an alternate suffix as a parameter to the filter.</p> <p>Example:</p> <div class="highlight-html+django"><div class="highlight"><pre>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">:"es"</span> <span class="cp">}}</span>. </pre></div> </div> <p>For words that don't pluralize by simple suffix, you can specify both a singular and plural suffix, separated by a comma.</p> <p>Example:</p> <div class="highlight-html+django"><div class="highlight"><pre>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">:"y,ies"</span> <span class="cp">}}</span>. </pre></div> </div> </div> <div class="section" id="s-pprint"> <span id="s-std:templatefilter-pprint"></span><span id="pprint"></span><span id="std:templatefilter-pprint"></span><h3>pprint<a class="headerlink" href="#pprint" title="Permalink to this headline">¶</a></h3> <p>A wrapper around <a class="reference external" href="http://docs.python.org/library/pprint.html">pprint.pprint</a> -- for debugging, really.</p> </div> <div class="section" id="s-random"> <span id="s-std:templatefilter-random"></span><span id="random"></span><span id="std:templatefilter-random"></span><h3>random<a class="headerlink" href="#random" title="Permalink to this headline">¶</a></h3> <p>Returns a random item from the given list.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">random</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is the list <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></tt>, the output could be <tt class="docutils literal"><span class="pre">"b"</span></tt>.</p> </div> <div class="section" id="s-removetags"> <span id="s-std:templatefilter-removetags"></span><span id="removetags"></span><span id="std:templatefilter-removetags"></span><h3>removetags<a class="headerlink" href="#removetags" title="Permalink to this headline">¶</a></h3> <p>Removes a space-separated list of [X]HTML tags from the output.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">removetags</span><span class="s2">:"b span"</span><span class="o">|</span><span class="nf">safe</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"<b>Joel</b></span> <span class="pre"><button>is</button></span> <span class="pre">a</span> <span class="pre"><span>slug</span>"</span></tt> the output will be <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre"><button>is</button></span> <span class="pre">a</span> <span class="pre">slug"</span></tt>.</p> </div> <div class="section" id="s-rjust"> <span id="s-std:templatefilter-rjust"></span><span id="rjust"></span><span id="std:templatefilter-rjust"></span><h3>rjust<a class="headerlink" href="#rjust" title="Permalink to this headline">¶</a></h3> <p>Right-aligns the value in a field of a given width.</p> <p><strong>Argument:</strong> field size</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre>"<span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">rjust</span><span class="s2">:"10"</span> <span class="cp">}}</span>" </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Django</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"</span> <span class="pre">Django"</span></tt>.</p> </div> <div class="section" id="s-safe"> <span id="s-std:templatefilter-safe"></span><span id="safe"></span><span id="std:templatefilter-safe"></span><h3>safe<a class="headerlink" href="#safe" title="Permalink to this headline">¶</a></h3> <p>Marks a string as not requiring further HTML escaping prior to output. When autoescaping is off, this filter has no effect.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>If you are chaining filters, a filter applied after <tt class="docutils literal"><span class="pre">safe</span></tt> can make the contents unsafe again. For example, the following code prints the variable as is, unescaped:</p> <div class="last highlight-html+django"><div class="highlight"><pre><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> </pre></div> </div> </div> </div> <div class="section" id="s-safeseq"> <span id="s-std:templatefilter-safeseq"></span><span id="safeseq"></span><span id="std:templatefilter-safeseq"></span><h3>safeseq<a class="headerlink" href="#safeseq" title="Permalink to this headline">¶</a></h3> <p>Applies the <a class="reference internal" href="#std:templatefilter-safe"><tt class="xref std std-tfilter docutils literal"><span class="pre">safe</span></tt></a> filter to each element of a sequence. Useful in conjunction with other filters that operate on sequences, such as <a class="reference internal" href="#std:templatefilter-join"><tt class="xref std std-tfilter docutils literal"><span class="pre">join</span></tt></a>. For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><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">:", "</span> <span class="cp">}}</span> </pre></div> </div> <p>You couldn't use the <a class="reference internal" href="#std:templatefilter-safe"><tt class="xref std std-tfilter docutils literal"><span class="pre">safe</span></tt></a> filter directly in this case, as it would first convert the variable into a string, rather than working with the individual elements of the sequence.</p> </div> <div class="section" id="s-slice"> <span id="s-std:templatefilter-slice"></span><span id="slice"></span><span id="std:templatefilter-slice"></span><h3>slice<a class="headerlink" href="#slice" title="Permalink to this headline">¶</a></h3> <p>Returns a slice of the list.</p> <p>Uses the same syntax as Python's list slicing. See <a class="reference external" href="http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice">http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice</a> for an introduction.</p> <p>Example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">some_list</span><span class="o">|</span><span class="nf">slice</span><span class="s2">:":2"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">some_list</span></tt> is <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></tt>, the output will be <tt class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b']</span></tt>.</p> </div> <div class="section" id="s-slugify"> <span id="s-std:templatefilter-slugify"></span><span id="slugify"></span><span id="std:templatefilter-slugify"></span><h3>slugify<a class="headerlink" href="#slugify" title="Permalink to this headline">¶</a></h3> <p>Converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens. Also strips leading and trailing whitespace.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">slugify</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"joel-is-a-slug"</span></tt>.</p> </div> <div class="section" id="s-stringformat"> <span id="s-std:templatefilter-stringformat"></span><span id="stringformat"></span><span id="std:templatefilter-stringformat"></span><h3>stringformat<a class="headerlink" href="#stringformat" title="Permalink to this headline">¶</a></h3> <p>Formats the variable according to the argument, a string formatting specifier. This specifier uses Python string formatting syntax, with the exception that the leading "%" is dropped.</p> <p>See <a class="reference external" href="http://docs.python.org/library/stdtypes.html#string-formatting-operations">http://docs.python.org/library/stdtypes.html#string-formatting-operations</a> for documentation of Python string formatting</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">stringformat</span><span class="s2">:"s"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>.</p> </div> <div class="section" id="s-striptags"> <span id="s-std:templatefilter-striptags"></span><span id="striptags"></span><span id="std:templatefilter-striptags"></span><h3>striptags<a class="headerlink" href="#striptags" title="Permalink to this headline">¶</a></h3> <p>Strips all [X]HTML tags.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">striptags</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"<b>Joel</b></span> <span class="pre"><button>is</button></span> <span class="pre">a</span> <span class="pre"><span>slug</span>"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>.</p> </div> <div class="section" id="s-time"> <span id="s-std:templatefilter-time"></span><span id="time"></span><span id="std:templatefilter-time"></span><h3>time<a class="headerlink" href="#time" title="Permalink to this headline">¶</a></h3> <p>Formats a time according to the given format.</p> <p>Given format can be the predefined one <tt class="docutils literal"><span class="pre">TIME_FORMAT</span></tt>, or a custom format, same as the <a class="reference internal" href="#std:templatetag-now"><tt class="xref std std-ttag docutils literal"><span class="pre">now</span></tt></a> tag. Note that the predefined format is locale- dependant.</p> <p>The time filter will only accept parameters in the format string that relate to the time of day, not the date (for obvious reasons). If you need to format a date, use the <a class="reference internal" href="#std:templatefilter-date"><tt class="xref std std-tfilter docutils literal"><span class="pre">date</span></tt></a> filter.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:"H:i"</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">datetime.datetime.now()</span></tt>, the output will be the string <tt class="docutils literal"><span class="pre">"01:23"</span></tt>.</p> <p>Another example:</p> <p>Assuming that <a class="reference internal" href="../settings.html#std:setting-USE_L10N"><tt class="xref std std-setting docutils literal"><span class="pre">USE_L10N</span></tt></a> is <tt class="xref docutils literal"><span class="pre">True</span></tt> and <a class="reference internal" href="../settings.html#std:setting-LANGUAGE_CODE"><tt class="xref std std-setting docutils literal"><span class="pre">LANGUAGE_CODE</span></tt></a> is, for example, <tt class="docutils literal"><span class="pre">"de"</span></tt>, then for:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:"TIME_FORMAT"</span> <span class="cp">}}</span> </pre></div> </div> <p>the output will be the string <tt class="docutils literal"><span class="pre">"01:23:00"</span></tt> (The <tt class="docutils literal"><span class="pre">"TIME_FORMAT"</span></tt> format specifier for the <tt class="docutils literal"><span class="pre">de</span></tt> locale as shipped with Django is <tt class="docutils literal"><span class="pre">"H:i:s"</span></tt>).</p> <p>When used without a format string:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span> <span class="cp">}}</span> </pre></div> </div> <p>...the formatting string defined in the <a class="reference internal" href="../settings.html#std:setting-TIME_FORMAT"><tt class="xref std std-setting docutils literal"><span class="pre">TIME_FORMAT</span></tt></a> setting will be used, without applying any localization.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.2:</span> Predefined formats can now be influenced by the current locale.</div> </div> <div class="section" id="s-timesince"> <span id="s-std:templatefilter-timesince"></span><span id="timesince"></span><span id="std:templatefilter-timesince"></span><h3>timesince<a class="headerlink" href="#timesince" title="Permalink to this headline">¶</a></h3> <p>Formats a date as the time since that date (e.g., "4 days, 6 hours").</p> <p>Takes an optional argument that is a variable containing the date to use as the comparison point (without the argument, the comparison point is <em>now</em>). For example, if <tt class="docutils literal"><span class="pre">blog_date</span></tt> is a date instance representing midnight on 1 June 2006, and <tt class="docutils literal"><span class="pre">comment_date</span></tt> is a date instance for 08:00 on 1 June 2006, then <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">blog_date|timesince:comment_date</span> <span class="pre">}}</span></tt> would return "8 hours".</p> <p>Comparing offset-naive and offset-aware datetimes will return an empty string.</p> <p>Minutes is the smallest unit used, and "0 minutes" will be returned for any date that is in the future relative to the comparison point.</p> </div> <div class="section" id="s-timeuntil"> <span id="s-std:templatefilter-timeuntil"></span><span id="timeuntil"></span><span id="std:templatefilter-timeuntil"></span><h3>timeuntil<a class="headerlink" href="#timeuntil" title="Permalink to this headline">¶</a></h3> <p>Similar to <tt class="docutils literal"><span class="pre">timesince</span></tt>, except that it measures the time from now until the given date or datetime. For example, if today is 1 June 2006 and <tt class="docutils literal"><span class="pre">conference_date</span></tt> is a date instance holding 29 June 2006, then <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">conference_date|timeuntil</span> <span class="pre">}}</span></tt> will return "4 weeks".</p> <p>Takes an optional argument that is a variable containing the date to use as the comparison point (instead of <em>now</em>). If <tt class="docutils literal"><span class="pre">from_date</span></tt> contains 22 June 2006, then <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">conference_date|timeuntil:from_date</span> <span class="pre">}}</span></tt> will return "1 week".</p> <p>Comparing offset-naive and offset-aware datetimes will return an empty string.</p> <p>Minutes is the smallest unit used, and "0 minutes" will be returned for any date that is in the past relative to the comparison point.</p> </div> <div class="section" id="s-title"> <span id="s-std:templatefilter-title"></span><span id="title"></span><span id="std:templatefilter-title"></span><h3>title<a class="headerlink" href="#title" title="Permalink to this headline">¶</a></h3> <p>Converts a string into titlecase.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">title</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"my</span> <span class="pre">first</span> <span class="pre">post"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"My</span> <span class="pre">First</span> <span class="pre">Post"</span></tt>.</p> </div> <div class="section" id="s-truncatewords"> <span id="s-std:templatefilter-truncatewords"></span><span id="truncatewords"></span><span id="std:templatefilter-truncatewords"></span><h3>truncatewords<a class="headerlink" href="#truncatewords" title="Permalink to this headline">¶</a></h3> <p>Truncates a string after a certain number of words.</p> <p><strong>Argument:</strong> Number of words to truncate after</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">..."</span></tt>.</p> <p>Newlines within the string will be removed.</p> </div> <div class="section" id="s-truncatewords-html"> <span id="s-std:templatefilter-truncatewords_html"></span><span id="truncatewords-html"></span><span id="std:templatefilter-truncatewords_html"></span><h3>truncatewords_html<a class="headerlink" href="#truncatewords-html" title="Permalink to this headline">¶</a></h3> <p>Similar to <tt class="docutils literal"><span class="pre">truncatewords</span></tt>, except that it is aware of HTML tags. Any tags that are opened in the string and not closed before the truncation point, are closed immediately after the truncation.</p> <p>This is less efficient than <tt class="docutils literal"><span class="pre">truncatewords</span></tt>, so should only be used when it is being passed HTML text.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"<p>Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug</p>"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"<p>Joel</span> <span class="pre">is</span> <span class="pre">...</p>"</span></tt>.</p> <p>Newlines in the HTML content will be preserved.</p> </div> <div class="section" id="s-unordered-list"> <span id="s-std:templatefilter-unordered_list"></span><span id="unordered-list"></span><span id="std:templatefilter-unordered_list"></span><h3>unordered_list<a class="headerlink" href="#unordered-list" title="Permalink to this headline">¶</a></h3> <p>Recursively takes a self-nested list and returns an HTML unordered list -- WITHOUT opening and closing <ul> tags.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.0:</span> The format accepted by <tt class="docutils literal"><span class="pre">unordered_list</span></tt> has changed to be easier to understand.</div> <p>The list is assumed to be in the proper format. For example, if <tt class="docutils literal"><span class="pre">var</span></tt> contains <tt class="docutils literal"><span class="pre">['States',</span> <span class="pre">['Kansas',</span> <span class="pre">['Lawrence',</span> <span class="pre">'Topeka'],</span> <span class="pre">'Illinois']]</span></tt>, then <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">var|unordered_list</span> <span class="pre">}}</span></tt> would return:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="nt"><li></span>States <span class="nt"><ul></span> <span class="nt"><li></span>Kansas <span class="nt"><ul></span> <span class="nt"><li></span>Lawrence<span class="nt"></li></span> <span class="nt"><li></span>Topeka<span class="nt"></li></span> <span class="nt"></ul></span> <span class="nt"></li></span> <span class="nt"><li></span>Illinois<span class="nt"></li></span> <span class="nt"></ul></span> <span class="nt"></li></span> </pre></div> </div> <p>Note: the previous more restrictive and verbose format is still supported: <tt class="docutils literal"><span class="pre">['States',</span> <span class="pre">[['Kansas',</span> <span class="pre">[['Lawrence',</span> <span class="pre">[]],</span> <span class="pre">['Topeka',</span> <span class="pre">[]]]],</span> <span class="pre">['Illinois',</span> <span class="pre">[]]]]</span></tt>,</p> </div> <div class="section" id="s-upper"> <span id="s-std:templatefilter-upper"></span><span id="upper"></span><span id="std:templatefilter-upper"></span><h3>upper<a class="headerlink" href="#upper" title="Permalink to this headline">¶</a></h3> <p>Converts a string into all uppercase.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">upper</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"JOEL</span> <span class="pre">IS</span> <span class="pre">A</span> <span class="pre">SLUG"</span></tt>.</p> </div> <div class="section" id="s-urlencode"> <span id="s-std:templatefilter-urlencode"></span><span id="urlencode"></span><span id="std:templatefilter-urlencode"></span><h3>urlencode<a class="headerlink" href="#urlencode" title="Permalink to this headline">¶</a></h3> <p>Escapes a value for use in a URL.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlencode</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"http://www.example.org/foo?a=b&c=d"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"http%3A//www.example.org/foo%3Fa%3Db%26c%3Dd"</span></tt>.</p> </div> <div class="section" id="s-urlize"> <span id="s-std:templatefilter-urlize"></span><span id="urlize"></span><span id="std:templatefilter-urlize"></span><h3>urlize<a class="headerlink" href="#urlize" title="Permalink to this headline">¶</a></h3> <p>Converts URLs in plain text into clickable links.</p> <p>Note that if <tt class="docutils literal"><span class="pre">urlize</span></tt> is applied to text that already contains HTML markup, things won't work as expected. Apply this filter only to <em>plain</em> text.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlize</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Check</span> <span class="pre">out</span> <span class="pre">www.djangoproject.com"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">"Check</span> <span class="pre">out</span> <span class="pre"><a</span> <span class="pre">href="http://www.djangoproject.com">www.djangoproject.com</a>"</span></tt>.</p> </div> <div class="section" id="s-urlizetrunc"> <span id="s-std:templatefilter-urlizetrunc"></span><span id="urlizetrunc"></span><span id="std:templatefilter-urlizetrunc"></span><h3>urlizetrunc<a class="headerlink" href="#urlizetrunc" title="Permalink to this headline">¶</a></h3> <p>Converts URLs into clickable links, truncating URLs longer than the given character limit.</p> <p>As with <a class="reference internal" href="#urlize">urlize</a>, this filter should only be applied to <em>plain</em> text.</p> <p><strong>Argument:</strong> Length to truncate URLs to</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Check</span> <span class="pre">out</span> <span class="pre">www.djangoproject.com"</span></tt>, the output would be <tt class="docutils literal"><span class="pre">'Check</span> <span class="pre">out</span> <span class="pre"><a</span> <span class="pre">href="http://www.djangoproject.com">www.djangopr...</a>'</span></tt>.</p> </div> <div class="section" id="s-wordcount"> <span id="s-std:templatefilter-wordcount"></span><span id="wordcount"></span><span id="std:templatefilter-wordcount"></span><h3>wordcount<a class="headerlink" href="#wordcount" title="Permalink to this headline">¶</a></h3> <p>Returns the number of words.</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">wordcount</span> <span class="cp">}}</span> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></tt>, the output will be <tt class="docutils literal"><span class="pre">4</span></tt>.</p> </div> <div class="section" id="s-wordwrap"> <span id="s-std:templatefilter-wordwrap"></span><span id="wordwrap"></span><span id="std:templatefilter-wordwrap"></span><h3>wordwrap<a class="headerlink" href="#wordwrap" title="Permalink to this headline">¶</a></h3> <p>Wraps words at specified line length.</p> <p><strong>Argument:</strong> number of characters at which to wrap the text</p> <p>For example:</p> <div class="highlight-html+django"><div class="highlight"><pre><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> </pre></div> </div> <p>If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug</span></tt>, the output would be:</p> <div class="highlight-html+django"><div class="highlight"><pre>Joel is a slug </pre></div> </div> </div> <div class="section" id="s-yesno"> <span id="s-std:templatefilter-yesno"></span><span id="yesno"></span><span id="std:templatefilter-yesno"></span><h3>yesno<a class="headerlink" href="#yesno" title="Permalink to this headline">¶</a></h3> <p>Given a string mapping values for true, false and (optionally) None, returns one of those strings according to the value:</p> <table class="docutils"> <colgroup> <col width="15%" /> <col width="33%" /> <col width="52%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">Value</th> <th class="head">Argument</th> <th class="head">Outputs</th> </tr> </thead> <tbody valign="top"> <tr><td><tt class="xref docutils literal"><span class="pre">True</span></tt></td> <td><tt class="docutils literal"><span class="pre">"yeah,no,maybe"</span></tt></td> <td><tt class="docutils literal"><span class="pre">yeah</span></tt></td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">False</span></tt></td> <td><tt class="docutils literal"><span class="pre">"yeah,no,maybe"</span></tt></td> <td><tt class="docutils literal"><span class="pre">no</span></tt></td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">None</span></tt></td> <td><tt class="docutils literal"><span class="pre">"yeah,no,maybe"</span></tt></td> <td><tt class="docutils literal"><span class="pre">maybe</span></tt></td> </tr> <tr><td><tt class="xref docutils literal"><span class="pre">None</span></tt></td> <td><tt class="docutils literal"><span class="pre">"yeah,no"</span></tt></td> <td><tt class="docutils literal"><span class="pre">"no"</span></tt> (converts None to False if no mapping for None is given)</td> </tr> </tbody> </table> </div> </div> <div class="section" id="s-other-tags-and-filter-libraries"> <span id="other-tags-and-filter-libraries"></span><h2>Other tags and filter libraries<a class="headerlink" href="#other-tags-and-filter-libraries" title="Permalink to this headline">¶</a></h2> <p>Django comes with a couple of other template-tag libraries that you have to enable explicitly in your <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt> setting and enable in your template with the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">%}</span></tt> tag.</p> <div class="section" id="s-django-contrib-humanize"> <span id="django-contrib-humanize"></span><h3>django.contrib.humanize<a class="headerlink" href="#django-contrib-humanize" title="Permalink to this headline">¶</a></h3> <p>A set of Django template filters useful for adding a "human touch" to data. See <a class="reference internal" href="../contrib/humanize.html"><em>django.contrib.humanize</em></a>.</p> </div> <div class="section" id="s-django-contrib-markup"> <span id="django-contrib-markup"></span><h3>django.contrib.markup<a class="headerlink" href="#django-contrib-markup" title="Permalink to this headline">¶</a></h3> <p>A collection of template filters that implement these common markup languages:</p> <ul class="simple"> <li>Textile</li> <li>Markdown</li> <li>ReST (ReStructured Text)</li> </ul> <p>See the <a class="reference internal" href="../contrib/markup.html"><em>markup documentation</em></a>.</p> </div> <div class="section" id="s-django-contrib-webdesign"> <span id="django-contrib-webdesign"></span><h3>django.contrib.webdesign<a class="headerlink" href="#django-contrib-webdesign" title="Permalink to this headline">¶</a></h3> <p>A collection of template tags that can be useful while designing a website, such as a generator of Lorem Ipsum text. See <a class="reference internal" href="../contrib/webdesign.html"><em>django.contrib.webdesign</em></a>.</p> </div> <div class="section" id="s-i18n"> <span id="i18n"></span><h3>i18n<a class="headerlink" href="#i18n" title="Permalink to this headline">¶</a></h3> <p>Provides a couple of templatetags that allow specifying translatable text in Django templates. It is slightly different from the libraries described above because you don't need to add any application to the <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt> setting but rather set <a class="reference internal" href="../settings.html#std:setting-USE_I18N"><tt class="xref std std-setting docutils literal"><span class="pre">USE_I18N</span></tt></a> to True, then loading it with <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">i18n</span> <span class="pre">%}</span></tt>. See <a class="reference internal" href="../../topics/i18n/internationalization.html#specifying-translation-strings-in-template-code"><em>Specifying translation strings: In template code</em></a>.</p> </div> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Built-in template tags and filters</a><ul> <li><a class="reference internal" href="#built-in-tag-reference">Built-in tag reference</a><ul> <li><a class="reference internal" href="#autoescape">autoescape</a></li> <li><a class="reference internal" href="#block">block</a></li> <li><a class="reference internal" href="#comment">comment</a></li> <li><a class="reference internal" href="#csrf-token">csrf_token</a></li> <li><a class="reference internal" href="#cycle">cycle</a></li> <li><a class="reference internal" href="#debug">debug</a></li> <li><a class="reference internal" href="#extends">extends</a></li> <li><a class="reference internal" href="#filter">filter</a></li> <li><a class="reference internal" href="#firstof">firstof</a></li> <li><a class="reference internal" href="#for">for</a><ul> <li><a class="reference internal" href="#for-empty">for ... empty</a></li> </ul> </li> <li><a class="reference internal" href="#if">if</a><ul> <li><a class="reference internal" href="#boolean-operators">Boolean operators</a></li> <li><a class="reference internal" href="#operator"><tt class="docutils literal"><span class="pre">==</span></tt> operator</a></li> <li><a class="reference internal" href="#id1"><tt class="docutils literal"><span class="pre">!=</span></tt> operator</a></li> <li><a class="reference internal" href="#id2"><tt class="docutils literal"><span class="pre"><</span></tt> operator</a></li> <li><a class="reference internal" href="#id3"><tt class="docutils literal"><span class="pre">></span></tt> operator</a></li> <li><a class="reference internal" href="#id4"><tt class="docutils literal"><span class="pre"><=</span></tt> operator</a></li> <li><a class="reference internal" href="#id5"><tt class="docutils literal"><span class="pre">>=</span></tt> operator</a></li> <li><a class="reference internal" href="#in-operator"><tt class="docutils literal"><span class="pre">in</span></tt> operator</a></li> </ul> </li> <li><a class="reference internal" href="#not-in-operator"><tt class="docutils literal"><span class="pre">not</span> <span class="pre">in</span></tt> operator</a><ul> <li><a class="reference internal" href="#filters">Filters</a></li> <li><a class="reference internal" href="#complex-expressions">Complex expressions</a></li> </ul> </li> <li><a class="reference internal" href="#ifchanged">ifchanged</a></li> <li><a class="reference internal" href="#ifequal">ifequal</a></li> <li><a class="reference internal" href="#ifnotequal">ifnotequal</a></li> <li><a class="reference internal" href="#include">include</a></li> <li><a class="reference internal" href="#load">load</a></li> <li><a class="reference internal" href="#now">now</a></li> <li><a class="reference internal" href="#regroup">regroup</a></li> <li><a class="reference internal" href="#spaceless">spaceless</a></li> <li><a class="reference internal" href="#ssi">ssi</a></li> <li><a class="reference internal" href="#templatetag">templatetag</a></li> <li><a class="reference internal" href="#url">url</a></li> <li><a class="reference internal" href="#widthratio">widthratio</a></li> <li><a class="reference internal" href="#with">with</a></li> </ul> </li> <li><a class="reference internal" href="#built-in-filter-reference">Built-in filter reference</a><ul> <li><a class="reference internal" href="#add">add</a></li> <li><a class="reference internal" href="#addslashes">addslashes</a></li> <li><a class="reference internal" href="#capfirst">capfirst</a></li> <li><a class="reference internal" href="#center">center</a></li> <li><a class="reference internal" href="#cut">cut</a></li> <li><a class="reference internal" href="#date">date</a></li> <li><a class="reference internal" href="#default">default</a></li> <li><a class="reference internal" href="#default-if-none">default_if_none</a></li> <li><a class="reference internal" href="#dictsort">dictsort</a></li> <li><a class="reference internal" href="#dictsortreversed">dictsortreversed</a></li> <li><a class="reference internal" href="#divisibleby">divisibleby</a></li> <li><a class="reference internal" href="#escape">escape</a></li> <li><a class="reference internal" href="#escapejs">escapejs</a></li> <li><a class="reference internal" href="#filesizeformat">filesizeformat</a></li> <li><a class="reference internal" href="#first">first</a></li> <li><a class="reference internal" href="#fix-ampersands">fix_ampersands</a></li> <li><a class="reference internal" href="#floatformat">floatformat</a></li> <li><a class="reference internal" href="#force-escape">force_escape</a></li> <li><a class="reference internal" href="#get-digit">get_digit</a></li> <li><a class="reference internal" href="#iriencode">iriencode</a></li> <li><a class="reference internal" href="#join">join</a></li> <li><a class="reference internal" href="#last">last</a></li> <li><a class="reference internal" href="#length">length</a></li> <li><a class="reference internal" href="#length-is">length_is</a></li> <li><a class="reference internal" href="#linebreaks">linebreaks</a></li> <li><a class="reference internal" href="#linebreaksbr">linebreaksbr</a></li> <li><a class="reference internal" href="#linenumbers">linenumbers</a></li> <li><a class="reference internal" href="#ljust">ljust</a></li> <li><a class="reference internal" href="#lower">lower</a></li> <li><a class="reference internal" href="#make-list">make_list</a></li> <li><a class="reference internal" href="#phone2numeric">phone2numeric</a></li> <li><a class="reference internal" href="#pluralize">pluralize</a></li> <li><a class="reference internal" href="#pprint">pprint</a></li> <li><a class="reference internal" href="#random">random</a></li> <li><a class="reference internal" href="#removetags">removetags</a></li> <li><a class="reference internal" href="#rjust">rjust</a></li> <li><a class="reference internal" href="#safe">safe</a></li> <li><a class="reference internal" href="#safeseq">safeseq</a></li> <li><a class="reference internal" href="#slice">slice</a></li> <li><a class="reference internal" href="#slugify">slugify</a></li> <li><a class="reference internal" href="#stringformat">stringformat</a></li> <li><a class="reference internal" href="#striptags">striptags</a></li> <li><a class="reference internal" href="#time">time</a></li> <li><a class="reference internal" href="#timesince">timesince</a></li> <li><a class="reference internal" href="#timeuntil">timeuntil</a></li> <li><a class="reference internal" href="#title">title</a></li> <li><a class="reference internal" href="#truncatewords">truncatewords</a></li> <li><a class="reference internal" href="#truncatewords-html">truncatewords_html</a></li> <li><a class="reference internal" href="#unordered-list">unordered_list</a></li> <li><a class="reference internal" href="#upper">upper</a></li> <li><a class="reference internal" href="#urlencode">urlencode</a></li> <li><a class="reference internal" href="#urlize">urlize</a></li> <li><a class="reference internal" href="#urlizetrunc">urlizetrunc</a></li> <li><a class="reference internal" href="#wordcount">wordcount</a></li> <li><a class="reference internal" href="#wordwrap">wordwrap</a></li> <li><a class="reference internal" href="#yesno">yesno</a></li> </ul> </li> <li><a class="reference internal" href="#other-tags-and-filter-libraries">Other tags and filter libraries</a><ul> <li><a class="reference internal" href="#django-contrib-humanize">django.contrib.humanize</a></li> <li><a class="reference internal" href="#django-contrib-markup">django.contrib.markup</a></li> <li><a class="reference internal" href="#django-contrib-webdesign">django.contrib.webdesign</a></li> <li><a class="reference internal" href="#i18n">i18n</a></li> </ul> </li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="index.html">Templates</a></li> <li>Next: <a href="api.html">The Django template language: For Python programmers</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django v1.2 documentation</a> <ul><li><a href="../index.html">API Reference</a> <ul><li><a href="index.html">Templates</a> <ul><li>Built-in template tags and filters</li></ul> </li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/ref/templates/builtins.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Oct 20, 2010</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="index.html" title="Templates">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="api.html" title="The Django template language: For Python programmers">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>