{"title":"The Django template language","version":"1.9","locale":"ja","docname":"ref/templates/language","url":"/ja/1.9/ref/templates/language/","canonical":"https://djangodocs.dev/ja/1.9/ref/templates/language/","summary":"This document explains the language syntax of the Django template system. If you're looking for a more technical perspective on how it works and how to extend it,…","html":"<h1>The Django template language<a class=\"heading-anchor\" href=\"#the-django-template-language\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document explains the language syntax of the Django template system. If\nyou're looking for a more technical perspective on how it works and how to\nextend it, see <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/api/\"><span class=\"doc\">The Django template language: for Python programmers</span></a>.</p>\n<p>Django's template language is designed to strike a balance between power and\nease. It's designed to feel comfortable to those used to working with HTML. If\nyou have any exposure to other text-based template languages, such as <a class=\"reference external\" href=\"http://www.smarty.net/\">Smarty</a>\nor <a class=\"reference external\" href=\"http://jinja.pocoo.org/\">Jinja2</a>, you should feel right at home with Django's templates.</p>\n<aside class=\"admonition-philosophy admonition\">\n<p class=\"admonition-title\">Philosophy</p>\n<p>If you have a background in programming, or if you're used to languages\nwhich mix programming code directly into HTML, you'll want to bear in\nmind that the Django template system is not simply Python embedded into\nHTML. This is by design: the template system is meant to express\npresentation, not program logic.</p>\n<p>The Django template system provides tags which function similarly to some\nprogramming constructs -- an <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-if\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">if</span></code></a> tag for boolean tests, a <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-for\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">for</span></code></a>\ntag for looping, etc. -- but these are not simply executed as the\ncorresponding Python code, and the template system will not execute\narbitrary Python expressions. Only the tags, filters and syntax listed below\nare supported by default (although you can add <a class=\"reference internal\" href=\"/ja/1.9/howto/custom-template-tags/\"><span class=\"doc\">your own extensions</span></a> to the template language as needed).</p>\n</aside>\n<section id=\"templates\">\n<h2>Templates<a class=\"heading-anchor\" href=\"#templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A template is simply a text file. It can generate any text-based format (HTML,\nXML, CSV, etc.).</p>\n<p>A template contains <strong>variables</strong>, which get replaced with values when the\ntemplate is evaluated, and <strong>tags</strong>, which control the logic of the template.</p>\n<p>Below is a minimal template that illustrates a few basics. Each element will be\nexplained later in this document.</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">extends</span> <span class=\"s2\">&quot;base_generic.html&quot;</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">title</span> <span class=\"cp\">%}{{</span> <span class=\"nv\">section.title</span> <span class=\"cp\">}}{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">content</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">section.title</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">story</span> <span class=\"k\">in</span> <span class=\"nv\">story_list</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>\n  <span class=\"p\">&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">story.get_absolute_url</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">story.headline</span><span class=\"o\">|</span><span class=\"nf\">upper</span> <span class=\"cp\">}}</span>\n  <span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">story.tease</span><span class=\"o\">|</span><span class=\"nf\">truncatewords</span><span class=\"s2\">:&quot;100&quot;</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<aside class=\"admonition-philosophy admonition\">\n<p class=\"admonition-title\">Philosophy</p>\n<p>Why use a text-based template instead of an XML-based one (like Zope's\nTAL)? We wanted Django's template language to be usable for more than\njust XML/HTML templates. At World Online, we use it for emails,\nJavaScript and CSV. You can use the template language for any text-based\nformat.</p>\n<p>Oh, and one more thing: making humans edit XML is sadistic!</p>\n</aside>\n</section>\n<section id=\"variables\">\n<span id=\"template-variables\"></span><h2>Variables<a class=\"heading-anchor\" href=\"#variables\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Variables look like this: <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">variable</span> <span class=\"pre\">}}</span></code>. When the template engine\nencounters a variable, it evaluates that variable and replaces it with the\nresult. Variable names consist of any combination of alphanumeric characters\nand the underscore (<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;_&quot;</span></code>). The dot (<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;.&quot;</span></code>) also appears in variable\nsections, although that has a special meaning, as indicated below.\nImportantly, <em>you cannot have spaces or punctuation characters in variable\nnames.</em></p>\n<p>Use a dot (<code class=\"docutils literal notranslate\"><span class=\"pre\">.</span></code>) to access attributes of a variable.</p>\n<aside class=\"admonition-behind-the-scenes admonition\">\n<p class=\"admonition-title\">Behind the scenes</p>\n<p>Technically, when the template system encounters a dot, it tries the\nfollowing lookups, in this order:</p>\n<ul class=\"simple\">\n<li><p>Dictionary lookup</p></li>\n<li><p>Attribute or method lookup</p></li>\n<li><p>Numeric index lookup</p></li>\n</ul>\n<p>If the resulting value is callable, it is called with no arguments. The\nresult of the call becomes the template value.</p>\n<p>This lookup order can cause some unexpected behavior with objects that\noverride dictionary lookup. For example, consider the following code snippet\nthat attempts to loop over a <code class=\"docutils literal notranslate\"><span class=\"pre\">collections.defaultdict</span></code>:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">k</span><span class=\"o\">,</span> <span class=\"nv\">v</span> <span class=\"k\">in</span> <span class=\"nv\">defaultdict.iteritems</span> <span class=\"cp\">%}</span>\n    Do something with k and v here...\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Because dictionary lookup happens first, that behavior kicks in and provides\na default value instead of using the intended <code class=\"docutils literal notranslate\"><span class=\"pre\">.iteritems()</span></code>\nmethod. In this case, consider converting to a dictionary first.</p>\n</aside>\n<p>In the above example, <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">section.title</span> <span class=\"pre\">}}</span></code> will be replaced with the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">title</span></code> attribute of the <code class=\"docutils literal notranslate\"><span class=\"pre\">section</span></code> object.</p>\n<p>If you use a variable that doesn't exist, the template system will insert the\nvalue of the <code class=\"docutils literal notranslate\"><span class=\"pre\">string_if_invalid</span></code> option, which is set to <code class=\"docutils literal notranslate\"><span class=\"pre\">''</span></code> (the empty\nstring) by default.</p>\n<p>Note that &quot;bar&quot; in a template expression like <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">foo.bar</span> <span class=\"pre\">}}</span></code> will be\ninterpreted as a literal string and not using the value of the variable &quot;bar&quot;,\nif one exists in the template context.</p>\n</section>\n<section id=\"filters\">\n<h2>Filters<a class=\"heading-anchor\" href=\"#filters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>You can modify variables for display by using <strong>filters</strong>.</p>\n<p>Filters look like this: <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">name|lower</span> <span class=\"pre\">}}</span></code>. This displays the value of the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">name</span> <span class=\"pre\">}}</span></code> variable after being filtered through the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-lower\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">lower</span></code></a>\nfilter, which converts text to lowercase. Use a pipe (<code class=\"docutils literal notranslate\"><span class=\"pre\">|</span></code>) to apply a filter.</p>\n<p>Filters can be &quot;chained.&quot; The output of one filter is applied to the next.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">text|escape|linebreaks</span> <span class=\"pre\">}}</span></code> is a common idiom for escaping text contents,\nthen converting line breaks to <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;p&gt;</span></code> tags.</p>\n<p>Some filters take arguments. A filter argument looks like this: <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span>\n<span class=\"pre\">bio|truncatewords:30</span> <span class=\"pre\">}}</span></code>. This will display the first 30 words of the <code class=\"docutils literal notranslate\"><span class=\"pre\">bio</span></code>\nvariable.</p>\n<p>Filter arguments that contain spaces must be quoted; for example, to join a\nlist with commas and spaces you'd use <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">list|join:&quot;,</span> <span class=\"pre\">&quot;</span> <span class=\"pre\">}}</span></code>.</p>\n<p>Django provides about sixty built-in template filters. You can read all about\nthem in the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#ref-templates-builtins-filters\"><span class=\"std std-ref\">built-in filter reference</span></a>.\nTo give you a taste of what's available, here are some of the more commonly\nused template filters:</p>\n<dl>\n<dt><a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-default\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">default</span></code></a></dt><dd><p>If a variable is false or empty, use given default. Otherwise, use the\nvalue of the variable. For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">default</span><span class=\"s2\">:&quot;nothing&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">value</span></code> isn't provided or is empty, the above will display\n&quot;<code class=\"docutils literal notranslate\"><span class=\"pre\">nothing</span></code>&quot;.</p>\n</dd>\n<dt><a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-length\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">length</span></code></a></dt><dd><p>Returns the length of the value. This works for both strings and lists.\nFor example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">length</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">value</span></code> is <code class=\"docutils literal notranslate\"><span class=\"pre\">['a',</span> <span class=\"pre\">'b',</span> <span class=\"pre\">'c',</span> <span class=\"pre\">'d']</span></code>, the output will be <code class=\"docutils literal notranslate\"><span class=\"pre\">4</span></code>.</p>\n</dd>\n<dt><a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-filesizeformat\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">filesizeformat</span></code></a></dt><dd><p>Formats the value like a &quot;human-readable&quot; file size (i.e. <code class=\"docutils literal notranslate\"><span class=\"pre\">'13</span> <span class=\"pre\">KB'</span></code>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'4.1</span> <span class=\"pre\">MB'</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">'102</span> <span class=\"pre\">bytes'</span></code>, etc.). For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">filesizeformat</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">value</span></code> is 123456789, the output would be <code class=\"docutils literal notranslate\"><span class=\"pre\">117.7</span> <span class=\"pre\">MB</span></code>.</p>\n</dd>\n</dl>\n<p>Again, these are just a few examples; see the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#ref-templates-builtins-filters\"><span class=\"std std-ref\">built-in filter reference</span></a> for the complete list.</p>\n<p>You can also create your own custom template filters; see\n<a class=\"reference internal\" href=\"/ja/1.9/howto/custom-template-tags/\"><span class=\"doc\">Custom template tags and filters</span></a>.</p>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">参考</p>\n<p>Django's admin interface can include a complete reference of all template\ntags and filters available for a given site. See\n<a class=\"reference internal\" href=\"/ja/1.9/ref/contrib/admin/admindocs/\"><span class=\"doc\">The Django admin documentation generator</span></a>.</p>\n</aside>\n</section>\n<section id=\"tags\">\n<h2>Tags<a class=\"heading-anchor\" href=\"#tags\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Tags look like this: <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">tag</span> <span class=\"pre\">%}</span></code>. Tags are more complex than variables: Some\ncreate text in the output, some control flow by performing loops or logic, and\nsome load external information into the template to be used by later variables.</p>\n<p>Some tags require beginning and ending tags (i.e. <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">tag</span> <span class=\"pre\">%}</span> <span class=\"pre\">...</span> <span class=\"pre\">tag</span> <span class=\"pre\">contents</span>\n<span class=\"pre\">...</span> <span class=\"pre\">{%</span> <span class=\"pre\">endtag</span> <span class=\"pre\">%}</span></code>).</p>\n<p>Django ships with about two dozen built-in template tags. You can read all about\nthem in the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#ref-templates-builtins-tags\"><span class=\"std std-ref\">built-in tag reference</span></a>. To give\nyou a taste of what's available, here are some of the more commonly used\ntags:</p>\n<dl>\n<dt><a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-for\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">for</span></code></a></dt><dd><p>Loop over each item in an array.  For example, to display a list of athletes\nprovided in <code class=\"docutils literal notranslate\"><span class=\"pre\">athlete_list</span></code>:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">athlete</span> <span class=\"k\">in</span> <span class=\"nv\">athlete_list</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">athlete.name</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</dd>\n<dt><a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-if\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">if</span></code></a>, <code class=\"docutils literal notranslate\"><span class=\"pre\">elif</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">else</span></code></dt><dd><p>Evaluates a variable, and if that variable is &quot;true&quot; the contents of the\nblock are displayed:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">athlete_list</span> <span class=\"cp\">%}</span>\n    Number of athletes: <span class=\"cp\">{{</span> <span class=\"nv\">athlete_list</span><span class=\"o\">|</span><span class=\"nf\">length</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">elif</span> <span class=\"nv\">athlete_in_locker_room_list</span> <span class=\"cp\">%}</span>\n    Athletes should be out of the locker room soon!\n<span class=\"cp\">{%</span> <span class=\"k\">else</span> <span class=\"cp\">%}</span>\n    No athletes.\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>In the above, if <code class=\"docutils literal notranslate\"><span class=\"pre\">athlete_list</span></code> is not empty, the number of athletes\nwill be displayed by the <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">athlete_list|length</span> <span class=\"pre\">}}</span></code> variable. Otherwise,\nif <code class=\"docutils literal notranslate\"><span class=\"pre\">athlete_in_locker_room_list</span></code> is not empty, the message &quot;Athletes\nshould be out...&quot; will be displayed. If both lists are empty,\n&quot;No athletes.&quot; will be displayed.</p>\n<p>You can also use filters and various operators in the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-if\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">if</span></code></a> tag:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">athlete_list</span><span class=\"o\">|</span><span class=\"nf\">length</span> <span class=\"o\">&gt;</span> <span class=\"m\">1</span> <span class=\"cp\">%}</span>\n   Team: <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=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">else</span> <span class=\"cp\">%}</span>\n   Athlete: <span class=\"cp\">{{</span> <span class=\"nv\">athlete_list.0.name</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>While the above example works, be aware that most template filters return\nstrings, so mathematical comparisons using filters will generally not work\nas you expect. <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-length\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">length</span></code></a> is an exception.</p>\n</dd>\n<dt><a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-block\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">block</span></code></a> and <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-extends\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">extends</span></code></a></dt><dd><p>Set up <a class=\"reference internal\" href=\"#id1\">template inheritance</a> (see below), a powerful way\nof cutting down on &quot;boilerplate&quot; in templates.</p>\n</dd>\n</dl>\n<p>Again, the above is only a selection of the whole list; see the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#ref-templates-builtins-tags\"><span class=\"std std-ref\">built-in\ntag reference</span></a> for the complete list.</p>\n<p>You can also create your own custom template tags; see\n<a class=\"reference internal\" href=\"/ja/1.9/howto/custom-template-tags/\"><span class=\"doc\">Custom template tags and filters</span></a>.</p>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">参考</p>\n<p>Django's admin interface can include a complete reference of all template\ntags and filters available for a given site. See\n<a class=\"reference internal\" href=\"/ja/1.9/ref/contrib/admin/admindocs/\"><span class=\"doc\">The Django admin documentation generator</span></a>.</p>\n</aside>\n</section>\n<section id=\"comments\">\n<span id=\"template-comments\"></span><h2>Comments<a class=\"heading-anchor\" href=\"#comments\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To comment-out part of a line in a template, use the comment syntax: <code class=\"docutils literal notranslate\"><span class=\"pre\">{#</span> <span class=\"pre\">#}</span></code>.</p>\n<p>For example, this template would render as <code class=\"docutils literal notranslate\"><span class=\"pre\">'hello'</span></code>:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"c\">{# greeting #}</span>hello\n</code></pre></div>\n<p>A comment can contain any template code, invalid or not. For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"c\">{# {% if foo %}bar{% else %} #}</span>\n</code></pre></div>\n<p>This syntax can only be used for single-line comments (no newlines are permitted\nbetween the <code class=\"docutils literal notranslate\"><span class=\"pre\">{#</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">#}</span></code> delimiters). If you need to comment out a\nmultiline portion of the template, see the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-comment\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">comment</span></code></a> tag.</p>\n</section>\n<section id=\"template-inheritance\">\n<span id=\"id1\"></span><h2>Template inheritance<a class=\"heading-anchor\" href=\"#template-inheritance\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The most powerful -- and thus the most complex -- part of Django's template\nengine is template inheritance. Template inheritance allows you to build a base\n&quot;skeleton&quot; template that contains all the common elements of your site and\ndefines <strong>blocks</strong> that child templates can override.</p>\n<p>It's easiest to understand template inheritance by starting with an example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">&lt;!DOCTYPE html&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">html</span> <span class=\"na\">lang</span><span class=\"o\">=</span><span class=\"s\">&quot;en&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">head</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">link</span> <span class=\"na\">rel</span><span class=\"o\">=</span><span class=\"s\">&quot;stylesheet&quot;</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;style.css&quot;</span> <span class=\"p\">/&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">title</span><span class=\"p\">&gt;</span><span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">title</span> <span class=\"cp\">%}</span>My amazing site<span class=\"cp\">{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span><span class=\"p\">&lt;/</span><span class=\"nt\">title</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">head</span><span class=\"p\">&gt;</span>\n\n<span class=\"p\">&lt;</span><span class=\"nt\">body</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;sidebar&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">sidebar</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n            <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;/&quot;</span><span class=\"p\">&gt;</span>Home<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n            <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;/blog/&quot;</span><span class=\"p\">&gt;</span>Blog<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n\n    <span class=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;content&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">content</span> <span class=\"cp\">%}{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">body</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">html</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>This template, which we'll call <code class=\"docutils literal notranslate\"><span class=\"pre\">base.html</span></code>, defines a simple HTML skeleton\ndocument that you might use for a simple two-column page. It's the job of\n&quot;child&quot; templates to fill the empty blocks with content.</p>\n<p>In this example, the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-block\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">block</span></code></a> tag defines three blocks that child\ntemplates can fill in. All the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-block\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">block</span></code></a> tag does is to tell the template\nengine that a child template may override those portions of the template.</p>\n<p>A child template might look like this:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">extends</span> <span class=\"s2\">&quot;base.html&quot;</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">title</span> <span class=\"cp\">%}</span>My amazing blog<span class=\"cp\">{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">content</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">entry</span> <span class=\"k\">in</span> <span class=\"nv\">blog_entries</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">entry.title</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">entry.body</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-extends\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">extends</span></code></a> tag is the key here. It tells the template engine that\nthis template &quot;extends&quot; another template. When the template system evaluates\nthis template, first it locates the parent -- in this case, &quot;base.html&quot;.</p>\n<p>At that point, the template engine will notice the three <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-block\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">block</span></code></a> tags\nin <code class=\"docutils literal notranslate\"><span class=\"pre\">base.html</span></code> and replace those blocks with the contents of the child\ntemplate. Depending on the value of <code class=\"docutils literal notranslate\"><span class=\"pre\">blog_entries</span></code>, the output might look\nlike:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">&lt;!DOCTYPE html&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">html</span> <span class=\"na\">lang</span><span class=\"o\">=</span><span class=\"s\">&quot;en&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">head</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">link</span> <span class=\"na\">rel</span><span class=\"o\">=</span><span class=\"s\">&quot;stylesheet&quot;</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;style.css&quot;</span> <span class=\"p\">/&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">title</span><span class=\"p\">&gt;</span>My amazing blog<span class=\"p\">&lt;/</span><span class=\"nt\">title</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">head</span><span class=\"p\">&gt;</span>\n\n<span class=\"p\">&lt;</span><span class=\"nt\">body</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;sidebar&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n            <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;/&quot;</span><span class=\"p\">&gt;</span>Home<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n            <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;/blog/&quot;</span><span class=\"p\">&gt;</span>Blog<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n\n    <span class=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">id</span><span class=\"o\">=</span><span class=\"s\">&quot;content&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>Entry one<span class=\"p\">&lt;/</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>This is my first entry.<span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n\n        <span class=\"p\">&lt;</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>Entry two<span class=\"p\">&lt;/</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>This is my second entry.<span class=\"p\">&lt;/</span><span class=\"nt\">p</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">body</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">html</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>Note that since the child template didn't define the <code class=\"docutils literal notranslate\"><span class=\"pre\">sidebar</span></code> block, the\nvalue from the parent template is used instead. Content within a <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">block</span> <span class=\"pre\">%}</span></code>\ntag in a parent template is always used as a fallback.</p>\n<p>You can use as many levels of inheritance as needed. One common way of using\ninheritance is the following three-level approach:</p>\n<ul class=\"simple\">\n<li><p>Create a <code class=\"docutils literal notranslate\"><span class=\"pre\">base.html</span></code> template that holds the main look-and-feel of your\nsite.</p></li>\n<li><p>Create a <code class=\"docutils literal notranslate\"><span class=\"pre\">base_SECTIONNAME.html</span></code> template for each &quot;section&quot; of your\nsite. For example, <code class=\"docutils literal notranslate\"><span class=\"pre\">base_news.html</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">base_sports.html</span></code>. These\ntemplates all extend <code class=\"docutils literal notranslate\"><span class=\"pre\">base.html</span></code> and include section-specific\nstyles/design.</p></li>\n<li><p>Create individual templates for each type of page, such as a news\narticle or blog entry. These templates extend the appropriate section\ntemplate.</p></li>\n</ul>\n<p>This approach maximizes code reuse and makes it easy to add items to shared\ncontent areas, such as section-wide navigation.</p>\n<p>Here are some tips for working with inheritance:</p>\n<ul>\n<li><p>If you use <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-extends\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">extends</span> <span class=\"pre\">%}</span></code></a> in a template, it must be the first template\ntag in that template. Template inheritance won't work, otherwise.</p></li>\n<li><p>More <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-block\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">block</span> <span class=\"pre\">%}</span></code></a> tags in your base templates are better. Remember,\nchild templates don't have to define all parent blocks, so you can fill\nin reasonable defaults in a number of blocks, then only define the ones\nyou need later. It's better to have more hooks than fewer hooks.</p></li>\n<li><p>If you find yourself duplicating content in a number of templates, it\nprobably means you should move that content to a <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">block</span> <span class=\"pre\">%}</span></code> in a\nparent template.</p></li>\n<li><p>If you need to get the content of the block from the parent template,\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">block.super</span> <span class=\"pre\">}}</span></code> variable will do the trick. This is useful\nif you want to add to the contents of a parent block instead of\ncompletely overriding it. Data inserted using <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">block.super</span> <span class=\"pre\">}}</span></code> will\nnot be automatically escaped (see the <a class=\"reference external\" href=\"#automatic-html-escaping\">next section</a>), since it was\nalready escaped, if necessary, in the parent template.</p></li>\n<li><p>For extra readability, you can optionally give a <em>name</em> to your\n<code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">endblock</span> <span class=\"pre\">%}</span></code> tag. For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">content</span> <span class=\"cp\">%}</span>\n...\n<span class=\"cp\">{%</span> <span class=\"k\">endblock</span> <span class=\"nv\">content</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>In larger templates, this technique helps you see which <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">block</span> <span class=\"pre\">%}</span></code>\ntags are being closed.</p>\n</li>\n</ul>\n<p>Finally, note that you can't define multiple <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-block\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">block</span></code></a> tags with the same\nname in the same template. This limitation exists because a block tag works in\n&quot;both&quot; directions. That is, a block tag doesn't just provide a hole to fill --\nit also defines the content that fills the hole in the <em>parent</em>. If there were\ntwo similarly-named <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-block\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">block</span></code></a> tags in a template, that template's parent\nwouldn't know which one of the blocks' content to use.</p>\n</section>\n<section id=\"automatic-html-escaping\">\n<span id=\"id2\"></span><h2>Automatic HTML escaping<a class=\"heading-anchor\" href=\"#automatic-html-escaping\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When generating HTML from templates, there's always a risk that a variable will\ninclude characters that affect the resulting HTML. For example, consider this\ntemplate fragment:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code>Hello, <span class=\"cp\">{{</span> <span class=\"nv\">name</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>At first, this seems like a harmless way to display a user's name, but consider\nwhat would happen if the user entered their name as this:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">script</span><span class=\"p\">&gt;</span><span class=\"nx\">alert</span><span class=\"p\">(</span><span class=\"s1\">&#39;hello&#39;</span><span class=\"p\">)&lt;/</span><span class=\"nt\">script</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>With this name value, the template would be rendered as:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code>Hello, <span class=\"p\">&lt;</span><span class=\"nt\">script</span><span class=\"p\">&gt;</span><span class=\"nx\">alert</span><span class=\"p\">(</span><span class=\"s1\">&#39;hello&#39;</span><span class=\"p\">)&lt;/</span><span class=\"nt\">script</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>...which means the browser would pop-up a JavaScript alert box!</p>\n<p>Similarly, what if the name contained a <code class=\"docutils literal notranslate\"><span class=\"pre\">'&lt;'</span></code> symbol, like this?</p>\n<div class=\"code-block\" data-language=\"html\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Html</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Html code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">b</span><span class=\"p\">&gt;</span>username\n</code></pre></div>\n<p>That would result in a rendered template like this:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code>Hello, <span class=\"p\">&lt;</span><span class=\"nt\">b</span><span class=\"p\">&gt;</span>username\n</code></pre></div>\n<p>...which, in turn, would result in the remainder of the Web page being bolded!</p>\n<p>Clearly, user-submitted data shouldn't be trusted blindly and inserted directly\ninto your Web pages, because a malicious user could use this kind of hole to\ndo potentially bad things. This type of security exploit is called a\n<a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Cross-site_scripting\">Cross Site Scripting</a> (XSS) attack.</p>\n<p>To avoid this problem, you have two options:</p>\n<ul class=\"simple\">\n<li><p>One, you can make sure to run each untrusted variable through the\n<a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-escape\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">escape</span></code></a> filter (documented below), which converts potentially\nharmful HTML characters to unharmful ones. This was the default solution\nin Django for its first few years, but the problem is that it puts the\nonus on <em>you</em>, the developer / template author, to ensure you're escaping\neverything. It's easy to forget to escape data.</p></li>\n<li><p>Two, you can take advantage of Django's automatic HTML escaping. The\nremainder of this section describes how auto-escaping works.</p></li>\n</ul>\n<p>By default in Django, every template automatically escapes the output\nof every variable tag. Specifically, these five characters are\nescaped:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;</span></code> is converted to <code class=\"docutils literal notranslate\"><span class=\"pre\">&amp;lt;</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&gt;</span></code> is converted to <code class=\"docutils literal notranslate\"><span class=\"pre\">&amp;gt;</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'</span></code> (single quote) is converted to <code class=\"docutils literal notranslate\"><span class=\"pre\">&amp;#39;</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;</span></code> (double quote) is converted to <code class=\"docutils literal notranslate\"><span class=\"pre\">&amp;quot;</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&amp;</span></code> is converted to <code class=\"docutils literal notranslate\"><span class=\"pre\">&amp;amp;</span></code></p></li>\n</ul>\n<p>Again, we stress that this behavior is on by default. If you're using Django's\ntemplate system, you're protected.</p>\n<section id=\"how-to-turn-it-off\">\n<h3>How to turn it off<a class=\"heading-anchor\" href=\"#how-to-turn-it-off\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you don't want data to be auto-escaped, on a per-site, per-template level or\nper-variable level, you can turn it off in several ways.</p>\n<p>Why would you want to turn it off? Because sometimes, template variables\ncontain data that you <em>intend</em> to be rendered as raw HTML, in which case you\ndon't want their contents to be escaped. For example, you might store a blob of\nHTML in your database and want to embed that directly into your template. Or,\nyou might be using Django's template system to produce text that is <em>not</em> HTML\n-- like an email message, for instance.</p>\n<section id=\"for-individual-variables\">\n<h4>For individual variables<a class=\"heading-anchor\" href=\"#for-individual-variables\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>To disable auto-escaping for an individual variable, use the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-safe\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">safe</span></code></a>\nfilter:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code>This will be escaped: <span class=\"cp\">{{</span> <span class=\"nv\">data</span> <span class=\"cp\">}}</span>\nThis will not be escaped: <span class=\"cp\">{{</span> <span class=\"nv\">data</span><span class=\"o\">|</span><span class=\"nf\">safe</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>Think of <em>safe</em> as shorthand for <em>safe from further escaping</em> or <em>can be\nsafely interpreted as HTML</em>. In this example, if <code class=\"docutils literal notranslate\"><span class=\"pre\">data</span></code> contains <code class=\"docutils literal notranslate\"><span class=\"pre\">'&lt;b&gt;'</span></code>,\nthe output will be:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code>This will be escaped: <span class=\"ni\">&amp;lt;</span>b<span class=\"ni\">&amp;gt;</span>\nThis will not be escaped: <span class=\"p\">&lt;</span><span class=\"nt\">b</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"for-template-blocks\">\n<h4>For template blocks<a class=\"heading-anchor\" href=\"#for-template-blocks\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>To control auto-escaping for a template, wrap the template (or just a\nparticular section of the template) in the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-autoescape\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">autoescape</span></code></a> tag, like so:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">autoescape</span> <span class=\"nv\">off</span> <span class=\"cp\">%}</span>\n    Hello <span class=\"cp\">{{</span> <span class=\"nv\">name</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endautoescape</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-autoescape\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">autoescape</span></code></a> tag takes either <code class=\"docutils literal notranslate\"><span class=\"pre\">on</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">off</span></code> as its argument. At\ntimes, you might want to force auto-escaping when it would otherwise be\ndisabled. Here is an example template:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code>Auto-escaping is on by default. Hello <span class=\"cp\">{{</span> <span class=\"nv\">name</span> <span class=\"cp\">}}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">autoescape</span> <span class=\"nv\">off</span> <span class=\"cp\">%}</span>\n    This will not be auto-escaped: <span class=\"cp\">{{</span> <span class=\"nv\">data</span> <span class=\"cp\">}}</span>.\n\n    Nor this: <span class=\"cp\">{{</span> <span class=\"nv\">other_data</span> <span class=\"cp\">}}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">autoescape</span> <span class=\"nv\">on</span> <span class=\"cp\">%}</span>\n        Auto-escaping applies again: <span class=\"cp\">{{</span> <span class=\"nv\">name</span> <span class=\"cp\">}}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">endautoescape</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endautoescape</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The auto-escaping tag passes its effect onto templates that extend the\ncurrent one as well as templates included via the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-include\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">include</span></code></a> tag,\njust like all block tags. For example:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>base.html</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"base.html\"><code><span class=\"p\">{</span><span class=\"o\">%</span> <span class=\"n\">autoescape</span> <span class=\"n\">off</span> <span class=\"o\">%</span><span class=\"p\">}</span>\n<span class=\"o\">&lt;</span><span class=\"n\">h1</span><span class=\"o\">&gt;</span><span class=\"p\">{</span><span class=\"o\">%</span> <span class=\"n\">block</span> <span class=\"n\">title</span> <span class=\"o\">%</span><span class=\"p\">}{</span><span class=\"o\">%</span> <span class=\"n\">endblock</span> <span class=\"o\">%</span><span class=\"p\">}</span><span class=\"o\">&lt;/</span><span class=\"n\">h1</span><span class=\"o\">&gt;</span>\n<span class=\"p\">{</span><span class=\"o\">%</span> <span class=\"n\">block</span> <span class=\"n\">content</span> <span class=\"o\">%</span><span class=\"p\">}</span>\n<span class=\"p\">{</span><span class=\"o\">%</span> <span class=\"n\">endblock</span> <span class=\"o\">%</span><span class=\"p\">}</span>\n<span class=\"p\">{</span><span class=\"o\">%</span> <span class=\"n\">endautoescape</span> <span class=\"o\">%</span><span class=\"p\">}</span>\n</code></pre></figure>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>child.html</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"child.html\"><code><span class=\"p\">{</span><span class=\"o\">%</span> <span class=\"n\">extends</span> <span class=\"s2\">&quot;base.html&quot;</span> <span class=\"o\">%</span><span class=\"p\">}</span>\n<span class=\"p\">{</span><span class=\"o\">%</span> <span class=\"n\">block</span> <span class=\"n\">title</span> <span class=\"o\">%</span><span class=\"p\">}</span><span class=\"n\">This</span> <span class=\"o\">&amp;</span><span class=\"n\">amp</span><span class=\"p\">;</span> <span class=\"n\">that</span><span class=\"p\">{</span><span class=\"o\">%</span> <span class=\"n\">endblock</span> <span class=\"o\">%</span><span class=\"p\">}</span>\n<span class=\"p\">{</span><span class=\"o\">%</span> <span class=\"n\">block</span> <span class=\"n\">content</span> <span class=\"o\">%</span><span class=\"p\">}{{</span> <span class=\"n\">greeting</span> <span class=\"p\">}}{</span><span class=\"o\">%</span> <span class=\"n\">endblock</span> <span class=\"o\">%</span><span class=\"p\">}</span>\n</code></pre></figure>\n<p>Because auto-escaping is turned off in the base template, it will also be\nturned off in the child template, resulting in the following rendered\nHTML when the <code class=\"docutils literal notranslate\"><span class=\"pre\">greeting</span></code> variable contains the string <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;b&gt;Hello!&lt;/b&gt;</span></code>:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>This <span class=\"ni\">&amp;amp;</span> that<span class=\"p\">&lt;/</span><span class=\"nt\">h1</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">b</span><span class=\"p\">&gt;</span>Hello!<span class=\"p\">&lt;/</span><span class=\"nt\">b</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"notes\">\n<h3>Notes<a class=\"heading-anchor\" href=\"#notes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Generally, template authors don't need to worry about auto-escaping very much.\nDevelopers on the Python side (people writing views and custom filters) need to\nthink about the cases in which data shouldn't be escaped, and mark data\nappropriately, so things Just Work in the template.</p>\n<p>If you're creating a template that might be used in situations where you're\nnot sure whether auto-escaping is enabled, then add an <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-escape\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">escape</span></code></a> filter\nto any variable that needs escaping. When auto-escaping is on, there's no\ndanger of the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-escape\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">escape</span></code></a> filter <em>double-escaping</em> data -- the\n<a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-escape\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">escape</span></code></a> filter does not affect auto-escaped variables.</p>\n</section>\n<section id=\"string-literals-and-automatic-escaping\">\n<span id=\"id3\"></span><h3>String literals and automatic escaping<a class=\"heading-anchor\" href=\"#string-literals-and-automatic-escaping\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>As we mentioned earlier, filter arguments can be strings:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">data</span><span class=\"o\">|</span><span class=\"nf\">default</span><span class=\"s2\">:&quot;This is a string literal.&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>All string literals are inserted <strong>without</strong> any automatic escaping into the\ntemplate -- they act as if they were all passed through the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatefilter-safe\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">safe</span></code></a>\nfilter. The reasoning behind this is that the template author is in control of\nwhat goes into the string literal, so they can make sure the text is correctly\nescaped when the template is written.</p>\n<p>This means you would write</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">data</span><span class=\"o\">|</span><span class=\"nf\">default</span><span class=\"s2\">:&quot;3 &amp;lt; 2&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>...rather than:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">data</span><span class=\"o\">|</span><span class=\"nf\">default</span><span class=\"s2\">:&quot;3 &lt; 2&quot;</span> <span class=\"cp\">}}</span>  <span class=\"c\">{# Bad! Don&#39;t do this. #}</span>\n</code></pre></div>\n<p>This doesn't affect what happens to data coming from the variable itself.\nThe variable's contents are still automatically escaped, if necessary, because\nthey're beyond the control of the template author.</p>\n</section>\n</section>\n<section id=\"accessing-method-calls\">\n<span id=\"template-accessing-methods\"></span><h2>Accessing method calls<a class=\"heading-anchor\" href=\"#accessing-method-calls\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Most method calls attached to objects are also available from within templates.\nThis means that templates have access to much more than just class attributes\n(like field names) and variables passed in from views. For example, the Django\nORM provides the <a class=\"reference internal\" href=\"/ja/1.9/topics/db/queries/#topics-db-queries-related\"><span class=\"std std-ref\">&quot;entry_set&quot;</span></a> syntax for\nfinding a collection of objects related on a foreign key. Therefore, given\na model called &quot;comment&quot; with a foreign key relationship to a model called\n&quot;task&quot; you can loop through all comments attached to a given task like this:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">comment</span> <span class=\"k\">in</span> <span class=\"nv\">task.comment_set.all</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">comment</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Similarly, <a class=\"reference internal\" href=\"/ja/1.9/ref/models/querysets/\"><span class=\"doc\">QuerySets</span></a> provide a <code class=\"docutils literal notranslate\"><span class=\"pre\">count()</span></code> method\nto count the number of objects they contain. Therefore, you can obtain a count\nof all comments related to the current task with:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">task.comment_set.all.count</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>And of course you can easily access methods you've explicitly defined on your\nown models:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>models.py</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"models.py\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Task</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Model</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">foo</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"s2\">&quot;bar&quot;</span>\n</code></pre></figure>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>template.html</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"template.html\"><code><span class=\"p\">{{</span> <span class=\"n\">task</span><span class=\"o\">.</span><span class=\"n\">foo</span> <span class=\"p\">}}</span>\n</code></pre></figure>\n<p>Because Django intentionally limits the amount of logic processing available\nin the template language, it is not possible to pass arguments to method calls\naccessed from within templates. Data should be calculated in views, then passed\nto templates for display.</p>\n</section>\n<section id=\"custom-tag-and-filter-libraries\">\n<span id=\"loading-custom-template-libraries\"></span><h2>Custom tag and filter libraries<a class=\"heading-anchor\" href=\"#custom-tag-and-filter-libraries\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Certain applications provide custom tag and filter libraries. To access them in\na template, ensure the application is in <a class=\"reference internal\" href=\"/ja/1.9/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> (we'd add\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'django.contrib.humanize'</span></code> for this example), and then use the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-load\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">load</span></code></a>\ntag in a template:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">humanize</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{{</span> <span class=\"m\">45000</span><span class=\"o\">|</span><span class=\"nf\">intcomma</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>In the above, the <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-load\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">load</span></code></a> tag loads the <code class=\"docutils literal notranslate\"><span class=\"pre\">humanize</span></code> tag library, which then\nmakes the <code class=\"docutils literal notranslate\"><span class=\"pre\">intcomma</span></code> filter available for use. If you've enabled\n<a class=\"reference internal\" href=\"/ja/1.9/ref/contrib/admin/admindocs/#module-django.contrib.admindocs\" title=\"django.contrib.admindocs: Django's admin documentation generator.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.admindocs</span></code></a>, you can consult the documentation area in your\nadmin to find the list of custom libraries in your installation.</p>\n<p>The <a class=\"reference internal\" href=\"/ja/1.9/ref/templates/builtins/#std-templatetag-load\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">load</span></code></a> tag can take multiple library names, separated by spaces.\nExample:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">humanize</span> <span class=\"nv\">i18n</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>See <a class=\"reference internal\" href=\"/ja/1.9/howto/custom-template-tags/\"><span class=\"doc\">Custom template tags and filters</span></a> for information on writing your own custom\ntemplate libraries.</p>\n<section id=\"custom-libraries-and-template-inheritance\">\n<h3>Custom libraries and template inheritance<a class=\"heading-anchor\" href=\"#custom-libraries-and-template-inheritance\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When you load a custom tag or filter library, the tags/filters are only made\navailable to the current template -- not any parent or child templates along\nthe template-inheritance path.</p>\n<p>For example, if a template <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.html</span></code> has <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">load</span> <span class=\"pre\">humanize</span> <span class=\"pre\">%}</span></code>, a child\ntemplate (e.g., one that has <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">extends</span> <span class=\"pre\">&quot;foo.html&quot;</span> <span class=\"pre\">%}</span></code>) will <em>not</em> have\naccess to the humanize template tags and filters. The child template is\nresponsible for its own <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">load</span> <span class=\"pre\">humanize</span> <span class=\"pre\">%}</span></code>.</p>\n<p>This is a feature for the sake of maintainability and sanity.</p>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">参考</p>\n<dl class=\"simple\">\n<dt><a class=\"reference internal\" href=\"/ja/1.9/ref/templates/\"><span class=\"doc\">The Templates Reference</span></a></dt><dd><p>Covers built-in tags, built-in filters, using an alternative template,\nlanguage, and more.</p>\n</dd>\n</dl>\n</aside>\n</section>\n</section>","rootId":"the-django-template-language","toc":[{"title":"Templates","anchor":"templates","children":[]},{"title":"Variables","anchor":"variables","children":[]},{"title":"Filters","anchor":"filters","children":[]},{"title":"Tags","anchor":"tags","children":[]},{"title":"Comments","anchor":"comments","children":[]},{"title":"Template inheritance","anchor":"template-inheritance","children":[]},{"title":"Automatic HTML escaping","anchor":"automatic-html-escaping","children":[{"title":"How to turn it off","anchor":"how-to-turn-it-off","children":[{"title":"For individual variables","anchor":"for-individual-variables","children":[]},{"title":"For template blocks","anchor":"for-template-blocks","children":[]}]},{"title":"Notes","anchor":"notes","children":[]},{"title":"String literals and automatic escaping","anchor":"string-literals-and-automatic-escaping","children":[]}]},{"title":"Accessing method calls","anchor":"accessing-method-calls","children":[]},{"title":"Custom tag and filter libraries","anchor":"custom-tag-and-filter-libraries","children":[{"title":"Custom libraries and template inheritance","anchor":"custom-libraries-and-template-inheritance","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/ja/1.9/ref/"},{"docname":"ref/templates/index","title":"Templates","url":"/ja/1.9/ref/templates/"}],"prev":{"docname":"ref/templates/index","title":"Templates","url":"/ja/1.9/ref/templates/"},"next":{"docname":"ref/templates/builtins","title":"Built-in template tags and filters","url":"/ja/1.9/ref/templates/builtins/"},"formats":{"html":"/ja/1.9/ref/templates/language/","markdown":"/ja/1.9/ref/templates/language.md","json":"/ja/1.9/ref/templates/language.json"},"source":"https://github.com/django/django/blob/stable/1.9.x/docs/ref/templates/language.txt","official":"https://docs.djangoproject.com/ja/1.9/ref/templates/language/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","fr","ja","id","pt-br","es"]}