Sophie

Sophie

distrib > Arklinux > devel > i586 > media > main > by-pkgid > 5fcb1fedf34660bc240dc59b7bfcebc4 > files > 472

django-doc-1.2.3-1ark.noarch.rpm


<!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>The Django template language &mdash; 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="Using Django" href="index.html" />
    <link rel="next" title="Generic views" href="generic-views.html" />
    <link rel="prev" title="Creating forms from models" href="forms/modelforms.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 = "../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>

  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 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">
    &laquo; <a href="forms/modelforms.html" title="Creating forms from models">previous</a> 
     |
    <a href="index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="generic-views.html" title="Generic views">next</a> &raquo;</div>
    </div>
    
    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-templates">
            
  <div class="section" id="s-the-django-template-language">
<span id="the-django-template-language"></span><h1>The Django template language<a class="headerlink" href="#the-django-template-language" title="Permalink to this headline">¶</a></h1>
<div class="admonition-about-this-document admonition ">
<p class="first admonition-title">About this document</p>
<p class="last">This document explains the language syntax of the Django template system. If
you&#8217;re looking for a more technical perspective on how it works and how to
extend it, see <a class="reference internal" href="../ref/templates/api.html"><em>The Django template language: For Python programmers</em></a>.</p>
</div>
<p>Django&#8217;s template language is designed to strike a balance between power and
ease. It&#8217;s designed to feel comfortable to those used to working with HTML. If
you have any exposure to other text-based template languages, such as <a class="reference external" href="http://smarty.php.net/">Smarty</a>
or <a class="reference external" href="http://www.cheetahtemplate.org/">CheetahTemplate</a>, you should feel right at home with Django&#8217;s templates.</p>
<div class="admonition-philosophy admonition ">
<p class="first admonition-title">Philosophy</p>
<p>If you have a background in programming, or if you&#8217;re used to languages
like PHP which mix programming code directly into HTML, you&#8217;ll want to
bear in mind that the Django template system is not simply Python embedded
into HTML. This is by design: the template system is meant to express
presentation, not program logic.</p>
<p class="last">The Django template system provides tags which function similarly to some
programming constructs &#8211; an <a class="reference internal" href="../ref/templates/builtins.html#std:templatetag-if"><tt class="xref std std-ttag docutils literal"><span class="pre">if</span></tt></a> tag for boolean tests, a <a class="reference internal" href="../ref/templates/builtins.html#std:templatetag-for"><tt class="xref std std-ttag docutils literal"><span class="pre">for</span></tt></a>
tag for looping, etc. &#8211; but these are not simply executed as the
corresponding Python code, and the template system will not execute
arbitrary Python expressions. Only the tags, filters and syntax listed below
are supported by default (although you can add <a class="reference internal" href="../howto/custom-template-tags.html"><em>your own extensions</em></a> to the template language as needed).</p>
</div>
<div class="section" id="s-templates">
<span id="templates"></span><h2>Templates<a class="headerlink" href="#templates" title="Permalink to this headline">¶</a></h2>
<p>A template is simply a text file. It can generate any text-based format (HTML,
XML, CSV, etc.).</p>
<p>A template contains <strong>variables</strong>, which get replaced with values when the
template is evaluated, and <strong>tags</strong>, which control the logic of the template.</p>
<p>Below is a minimal template that illustrates a few basics. Each element will be
explained later in this document.:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">&quot;base_generic.html&quot;</span> <span class="cp">%}</span>

<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>

<span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}</span>
<span class="nt">&lt;h1&gt;</span><span class="cp">{{</span> <span class="nv">section.title</span> <span class="cp">}}</span><span class="nt">&lt;/h1&gt;</span>

<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>
<span class="nt">&lt;h2&gt;</span>
  <span class="nt">&lt;a</span> <span class="na">href=</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="nt">&gt;</span>
    <span class="cp">{{</span> <span class="nv">story.headline</span><span class="o">|</span><span class="nf">upper</span> <span class="cp">}}</span>
  <span class="nt">&lt;/a&gt;</span>
<span class="nt">&lt;/h2&gt;</span>
<span class="nt">&lt;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="nt">&lt;/p&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition-philosophy admonition ">
<p class="first admonition-title">Philosophy</p>
<p>Why use a text-based template instead of an XML-based one (like Zope's
TAL)? We wanted Django's template language to be usable for more than
just XML/HTML templates. At World Online, we use it for e-mails,
JavaScript and CSV. You can use the template language for any text-based
format.</p>
<p class="last">Oh, and one more thing: Making humans edit XML is sadistic!</p>
</div>
</div>
<div class="section" id="s-variables">
<span id="variables"></span><h2>Variables<a class="headerlink" href="#variables" title="Permalink to this headline">¶</a></h2>
<p>Variables look like this: <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">variable</span> <span class="pre">}}</span></tt>. When the template engine
encounters a variable, it evaluates that variable and replaces it with the
result.</p>
<p>Use a dot (<tt class="docutils literal"><span class="pre">.</span></tt>) to access attributes of a variable.</p>
<div class="admonition-behind-the-scenes admonition ">
<p class="first admonition-title">Behind the scenes</p>
<p>Technically, when the template system encounters a dot, it tries the
following lookups, in this order:</p>
<ul class="last simple">
<li>Dictionary lookup</li>
<li>Attribute lookup</li>
<li>Method call</li>
<li>List-index lookup</li>
</ul>
</div>
<p>In the above example, <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">section.title</span> <span class="pre">}}</span></tt> will be replaced with the
<tt class="docutils literal"><span class="pre">title</span></tt> attribute of the <tt class="docutils literal"><span class="pre">section</span></tt> object.</p>
<p>If you use a variable that doesn't exist, the template system will insert
the value of the <tt class="docutils literal"><span class="pre">TEMPLATE_STRING_IF_INVALID</span></tt> setting, which is set to <tt class="docutils literal"><span class="pre">''</span></tt>
(the empty string) by default.</p>
<p>See <a class="reference internal" href="#using-the-built-in-reference">Using the built-in reference</a>, below, for help on finding what variables
are available in a given template.</p>
</div>
<div class="section" id="s-filters">
<span id="filters"></span><h2>Filters<a class="headerlink" href="#filters" title="Permalink to this headline">¶</a></h2>
<p>You can modify variables for display by using <strong>filters</strong>.</p>
<p>Filters look like this: <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">name|lower</span> <span class="pre">}}</span></tt>. This displays the value of the
<tt class="docutils literal"><span class="pre">{{</span> <span class="pre">name</span> <span class="pre">}}</span></tt> variable after being filtered through the <tt class="docutils literal"><span class="pre">lower</span></tt> filter,
which converts text to lowercase. Use a pipe (<tt class="docutils literal"><span class="pre">|</span></tt>) to apply a filter.</p>
<p>Filters can be &quot;chained.&quot; The output of one filter is applied to the next.
<tt class="docutils literal"><span class="pre">{{</span> <span class="pre">text|escape|linebreaks</span> <span class="pre">}}</span></tt> is a common idiom for escaping text contents,
then converting line breaks to <tt class="docutils literal"><span class="pre">&lt;p&gt;</span></tt> tags.</p>
<p>Some filters take arguments. A filter argument looks like this: <tt class="docutils literal"><span class="pre">{{</span>
<span class="pre">bio|truncatewords:30</span> <span class="pre">}}</span></tt>. This will display the first 30 words of the <tt class="docutils literal"><span class="pre">bio</span></tt>
variable.</p>
<p>Filter arguments that contain spaces must be quoted; for example, to join a list
with commas and spaced you'd use <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">list|join:&quot;,</span> <span class="pre">&quot;</span> <span class="pre">}}</span></tt>.</p>
<p>Django provides about thirty built-in template filters. You can read all about
them in the <a class="reference internal" href="../ref/templates/builtins.html#ref-templates-builtins-filters"><em>built-in filter reference</em></a>.
To give you a taste of what's available, here are some of the more commonly used
template filters:</p>
<dl class="docutils">
<dt><a class="reference internal" href="../ref/templates/builtins.html#std:templatefilter-default"><tt class="xref std std-tfilter docutils literal"><span class="pre">default</span></tt></a></dt>
<dd><p class="first">If a variable is false or empty, use given default. Otherwise, use the
value of the variable</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">:&quot;nothing&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p class="last">If <tt class="docutils literal"><span class="pre">value</span></tt> isn't provided or is empty, the above will display
&quot;<tt class="docutils literal"><span class="pre">nothing</span></tt>&quot;.</p>
</dd>
<dt><a class="reference internal" href="../ref/templates/builtins.html#std:templatefilter-length"><tt class="xref std std-tfilter docutils literal"><span class="pre">length</span></tt></a></dt>
<dd><p class="first">Returns the length of the value. This works for both strings and lists;
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 class="last">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>
</dd>
<dt><a class="reference internal" href="../ref/templates/builtins.html#std:templatefilter-striptags"><tt class="xref std std-tfilter docutils literal"><span class="pre">striptags</span></tt></a></dt>
<dd><p class="first">Strips all [X]HTML tags. 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 class="last">If <tt class="docutils literal"><span class="pre">value</span></tt> is <tt class="docutils literal"><span class="pre">&quot;&lt;b&gt;Joel&lt;/b&gt;</span> <span class="pre">&lt;button&gt;is&lt;/button&gt;</span> <span class="pre">a</span>
<span class="pre">&lt;span&gt;slug&lt;/span&gt;&quot;</span></tt>, the output will be <tt class="docutils literal"><span class="pre">&quot;Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug&quot;</span></tt>.</p>
</dd>
</dl>
<p>Again, these are just a few examples; see the <a class="reference internal" href="../ref/templates/builtins.html#ref-templates-builtins-filters"><em>built-in filter reference</em></a> for the complete list.</p>
<p>You can also create your own custom template filters; see
<a class="reference internal" href="../howto/custom-template-tags.html"><em>Custom template tags and filters</em></a>.</p>
</div>
<div class="section" id="s-tags">
<span id="tags"></span><h2>Tags<a class="headerlink" href="#tags" title="Permalink to this headline">¶</a></h2>
<p>Tags look like this: <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">tag</span> <span class="pre">%}</span></tt>. Tags are more complex than variables: Some
create text in the output, some control flow by performing loops or logic, and
some load external information into the template to be used by later variables.</p>
<p>Some tags require beginning and ending tags (i.e. <tt class="docutils literal"><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>
<span class="pre">...</span> <span class="pre">{%</span> <span class="pre">endtag</span> <span class="pre">%}</span></tt>).</p>
<p>Django ships with about two dozen built-in template tags. You can read all about
them in the <a class="reference internal" href="../ref/templates/builtins.html#ref-templates-builtins-tags"><em>built-in tag reference</em></a>. To give
you a taste of what's available, here are some of the more commonly used
tags:</p>
<dl class="docutils">
<dt><a class="reference internal" href="../ref/templates/builtins.html#std:templatetag-for"><tt class="xref std std-ttag docutils literal"><span class="pre">for</span></tt></a></dt>
<dd><p class="first">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="last highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;ul&gt;</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">&lt;li&gt;</span><span class="cp">{{</span> <span class="nv">athlete.name</span> <span class="cp">}}</span><span class="nt">&lt;/li&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="nt">&lt;/ul&gt;</span>
</pre></div>
</div>
</dd>
<dt><a class="reference internal" href="../ref/templates/builtins.html#std:templatetag-if"><tt class="xref std std-ttag docutils literal"><span class="pre">if</span></tt></a> and <tt class="docutils literal"><span class="pre">else</span></tt></dt>
<dd><p class="first">Evaluates a variable, and if that variable is &quot;true&quot; the contents of the
block are displayed:</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>You can also use filters and various operators in the <tt class="docutils literal"><span class="pre">if</span></tt> tag:</p>
<div class="last highlight-html+django"><div class="highlight"><pre><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>
   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>
<span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span>
   Athlete: <span class="cp">{{</span> <span class="nv">athlete_list.0.name</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</dd>
<dt><a class="reference internal" href="../ref/templates/builtins.html#std:templatetag-block"><tt class="xref std std-ttag docutils literal"><span class="pre">block</span></tt></a> and <a class="reference internal" href="../ref/templates/builtins.html#std:templatetag-extends"><tt class="xref std std-ttag docutils literal"><span class="pre">extends</span></tt></a></dt>
<dd>Set up <a class="reference internal" href="#id1">template inheritance</a> (see below), a powerful way
of cutting down on &quot;boilerplate&quot; in templates.</dd>
</dl>
<p>Again, the above is only a selection of the whole list; see the <a class="reference internal" href="../ref/templates/builtins.html#ref-templates-builtins-tags"><em>built-in
tag reference</em></a> for the complete list.</p>
<p>You can also create your own custom template tags; see
<a class="reference internal" href="../howto/custom-template-tags.html"><em>Custom template tags and filters</em></a>.</p>
</div>
<div class="section" id="s-comments">
<span id="comments"></span><h2>Comments<a class="headerlink" href="#comments" title="Permalink to this headline">¶</a></h2>
<p>To comment-out part of a line in a template, use the comment syntax: <tt class="docutils literal"><span class="pre">{#</span> <span class="pre">#}</span></tt>.</p>
<p>For example, this template would render as <tt class="docutils literal"><span class="pre">'hello'</span></tt>:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="c">{# greeting #}</span>hello
</pre></div>
</div>
<p>A comment can contain any template code, invalid or not. For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="c">{# {% if foo %}bar{% else %} #}</span>
</pre></div>
</div>
<p>This syntax can only be used for single-line comments (no newlines are permitted
between the <tt class="docutils literal"><span class="pre">{#</span></tt> and <tt class="docutils literal"><span class="pre">#}</span></tt> delimiters). If you need to comment out a
multiline portion of the template, see the <a class="reference internal" href="../ref/templates/builtins.html#std:templatetag-comment"><tt class="xref std std-ttag docutils literal"><span class="pre">comment</span></tt></a> tag.</p>
</div>
<div class="section" id="s-template-inheritance">
<span id="s-id1"></span><span id="template-inheritance"></span><span id="id1"></span><h2>Template inheritance<a class="headerlink" href="#template-inheritance" title="Permalink to this headline">¶</a></h2>
<p>The most powerful -- and thus the most complex -- part of Django's template
engine is template inheritance. Template inheritance allows you to build a base
&quot;skeleton&quot; template that contains all the common elements of your site and
defines <strong>blocks</strong> that child templates can override.</p>
<p>It's easiest to understand template inheritance by starting with an example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
<span class="cp">    &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span class="nt">&lt;html</span> <span class="na">xmlns=</span><span class="s">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span class="na">xml:lang=</span><span class="s">&quot;en&quot;</span> <span class="na">lang=</span><span class="s">&quot;en&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;head&gt;</span>
    <span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">&quot;stylesheet&quot;</span> <span class="na">href=</span><span class="s">&quot;style.css&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;title&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="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>

<span class="nt">&lt;body&gt;</span>
    <span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;sidebar&quot;</span><span class="nt">&gt;</span>
        <span class="cp">{%</span> <span class="k">block</span> <span class="nv">sidebar</span> <span class="cp">%}</span>
        <span class="nt">&lt;ul&gt;</span>
            <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/&quot;</span><span class="nt">&gt;</span>Home<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/blog/&quot;</span><span class="nt">&gt;</span>Blog<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
        <span class="nt">&lt;/ul&gt;</span>
        <span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
    <span class="nt">&lt;/div&gt;</span>

    <span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;content&quot;</span><span class="nt">&gt;</span>
        <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>
    <span class="nt">&lt;/div&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div>
<p>This template, which we'll call <tt class="docutils literal"><span class="pre">base.html</span></tt>, defines a simple HTML skeleton
document that you might use for a simple two-column page. It's the job of
&quot;child&quot; templates to fill the empty blocks with content.</p>
<p>In this example, the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt> tag defines three blocks that child
templates can fill in. All the <tt class="docutils literal"><span class="pre">block</span></tt> tag does is to tell the template
engine that a child template may override those portions of the template.</p>
<p>A child template might look like this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">&quot;base.html&quot;</span> <span class="cp">%}</span>

<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>

<span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}</span>
<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>
    <span class="nt">&lt;h2&gt;</span><span class="cp">{{</span> <span class="nv">entry.title</span> <span class="cp">}}</span><span class="nt">&lt;/h2&gt;</span>
    <span class="nt">&lt;p&gt;</span><span class="cp">{{</span> <span class="nv">entry.body</span> <span class="cp">}}</span><span class="nt">&lt;/p&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">%}</span></tt> tag is the key here. It tells the template engine that
this template &quot;extends&quot; another template. When the template system evaluates
this template, first it locates the parent -- in this case, &quot;base.html&quot;.</p>
<p>At that point, the template engine will notice the three <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt> tags
in <tt class="docutils literal"><span class="pre">base.html</span></tt> and replace those blocks with the contents of the child
template. Depending on the value of <tt class="docutils literal"><span class="pre">blog_entries</span></tt>, the output might look
like:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
<span class="cp">    &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span class="nt">&lt;html</span> <span class="na">xmlns=</span><span class="s">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span class="na">xml:lang=</span><span class="s">&quot;en&quot;</span> <span class="na">lang=</span><span class="s">&quot;en&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;head&gt;</span>
    <span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">&quot;stylesheet&quot;</span> <span class="na">href=</span><span class="s">&quot;style.css&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;title&gt;</span>My amazing blog<span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>

<span class="nt">&lt;body&gt;</span>
    <span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;sidebar&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;ul&gt;</span>
            <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/&quot;</span><span class="nt">&gt;</span>Home<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/blog/&quot;</span><span class="nt">&gt;</span>Blog<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
        <span class="nt">&lt;/ul&gt;</span>
    <span class="nt">&lt;/div&gt;</span>

    <span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;content&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;h2&gt;</span>Entry one<span class="nt">&lt;/h2&gt;</span>
        <span class="nt">&lt;p&gt;</span>This is my first entry.<span class="nt">&lt;/p&gt;</span>

        <span class="nt">&lt;h2&gt;</span>Entry two<span class="nt">&lt;/h2&gt;</span>
        <span class="nt">&lt;p&gt;</span>This is my second entry.<span class="nt">&lt;/p&gt;</span>
    <span class="nt">&lt;/div&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div>
<p>Note that since the child template didn't define the <tt class="docutils literal"><span class="pre">sidebar</span></tt> block, the
value from the parent template is used instead. Content within a <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt>
tag in a parent template is always used as a fallback.</p>
<p>You can use as many levels of inheritance as needed. One common way of using
inheritance is the following three-level approach:</p>
<ul class="simple">
<li>Create a <tt class="docutils literal"><span class="pre">base.html</span></tt> template that holds the main look-and-feel of your
site.</li>
<li>Create a <tt class="docutils literal"><span class="pre">base_SECTIONNAME.html</span></tt> template for each &quot;section&quot; of your
site. For example, <tt class="docutils literal"><span class="pre">base_news.html</span></tt>, <tt class="docutils literal"><span class="pre">base_sports.html</span></tt>. These
templates all extend <tt class="docutils literal"><span class="pre">base.html</span></tt> and include section-specific
styles/design.</li>
<li>Create individual templates for each type of page, such as a news
article or blog entry. These templates extend the appropriate section
template.</li>
</ul>
<p>This approach maximizes code reuse and makes it easy to add items to shared
content areas, such as section-wide navigation.</p>
<p>Here are some tips for working with inheritance:</p>
<ul>
<li><p class="first">If you use <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">%}</span></tt> in a template, it must be the first template
tag in that template. Template inheritance won't work, otherwise.</p>
</li>
<li><p class="first">More <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt> tags in your base templates are better. Remember,
child templates don't have to define all parent blocks, so you can fill
in reasonable defaults in a number of blocks, then only define the ones
you need later. It's better to have more hooks than fewer hooks.</p>
</li>
<li><p class="first">If you find yourself duplicating content in a number of templates, it
probably means you should move that content to a <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt> in a
parent template.</p>
</li>
<li><p class="first">If you need to get the content of the block from the parent template,
the <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">block.super</span> <span class="pre">}}</span></tt> variable will do the trick. This is useful
if you want to add to the contents of a parent block instead of
completely overriding it. Data inserted using <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">block.super</span> <span class="pre">}}</span></tt> will
not be automatically escaped (see the <a class="reference external" href="#automatic-html-escaping">next section</a>), since it was
already escaped, if necessary, in the parent template.</p>
</li>
<li><p class="first">For extra readability, you can optionally give a <em>name</em> to your
<tt class="docutils literal"><span class="pre">{%</span> <span class="pre">endblock</span> <span class="pre">%}</span></tt> tag. For example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}</span>
...
<span class="cp">{%</span> <span class="k">endblock</span> <span class="nv">content</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>In larger templates, this technique helps you see which <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt>
tags are being closed.</p>
</li>
</ul>
<p>Finally, note that you can't define multiple <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt> tags with the same
name in the same template. This limitation exists because a block tag works in
&quot;both&quot; directions. That is, a block tag doesn't just provide a hole to fill --
it also defines the content that fills the hole in the <em>parent</em>. If there were
two similarly-named <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></tt> tags in a template, that template's parent
wouldn't know which one of the blocks' content to use.</p>
</div>
<div class="section" id="s-automatic-html-escaping">
<span id="s-id2"></span><span id="automatic-html-escaping"></span><span id="id2"></span><h2>Automatic HTML escaping<a class="headerlink" href="#automatic-html-escaping" title="Permalink to this headline">¶</a></h2>
<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>When generating HTML from templates, there's always a risk that a variable will
include characters that affect the resulting HTML. For example, consider this
template fragment:</p>
<div class="highlight-html+django"><div class="highlight"><pre>Hello, <span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span>.
</pre></div>
</div>
<p>At first, this seems like a harmless way to display a user's name, but consider
what would happen if the user entered his name as this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;script&gt;</span><span class="nx">alert</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span><span class="nt">&lt;/script&gt;</span>
</pre></div>
</div>
<p>With this name value, the template would be rendered as:</p>
<div class="highlight-html+django"><div class="highlight"><pre>Hello, <span class="nt">&lt;script&gt;</span><span class="nx">alert</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">)</span><span class="nt">&lt;/script&gt;</span>
</pre></div>
</div>
<p>...which means the browser would pop-up a JavaScript alert box!</p>
<p>Similarly, what if the name contained a <tt class="docutils literal"><span class="pre">'&lt;'</span></tt> symbol, like this?</p>
<blockquote>
&lt;b&gt;username</blockquote>
<p>That would result in a rendered template like this:</p>
<div class="highlight-html+django"><div class="highlight"><pre>Hello, <span class="nt">&lt;b&gt;</span>username
</pre></div>
</div>
<p>...which, in turn, would result in the remainder of the Web page being bolded!</p>
<p>Clearly, user-submitted data shouldn't be trusted blindly and inserted directly
into your Web pages, because a malicious user could use this kind of hole to
do potentially bad things. This type of security exploit is called a
<a class="reference external" href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross Site Scripting</a> (XSS) attack.</p>
<p>To avoid this problem, you have two options:</p>
<ul class="simple">
<li>One, you can make sure to run each untrusted variable through the
<tt class="docutils literal"><span class="pre">escape</span></tt> filter (documented below), which converts potentially harmful
HTML characters to unharmful ones. This was the default solution
in Django for its first few years, but the problem is that it puts the
onus on <em>you</em>, the developer / template author, to ensure you're escaping
everything. It's easy to forget to escape data.</li>
<li>Two, you can take advantage of Django's automatic HTML escaping. The
remainder of this section describes how auto-escaping works.</li>
</ul>
<p>By default in Django, every template automatically escapes the output
of every variable tag. Specifically, these five characters are
escaped:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">&lt;</span></tt> is converted to <tt class="docutils literal"><span class="pre">&amp;lt;</span></tt></li>
<li><tt class="docutils literal"><span class="pre">&gt;</span></tt> is converted to <tt class="docutils literal"><span class="pre">&amp;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">&amp;#39;</span></tt></li>
<li><tt class="docutils literal"><span class="pre">&quot;</span></tt> (double quote) is converted to <tt class="docutils literal"><span class="pre">&amp;quot;</span></tt></li>
<li><tt class="docutils literal"><span class="pre">&amp;</span></tt> is converted to <tt class="docutils literal"><span class="pre">&amp;amp;</span></tt></li>
</ul>
<p>Again, we stress that this behavior is on by default. If you're using Django's
template system, you're protected.</p>
<div class="section" id="s-how-to-turn-it-off">
<span id="how-to-turn-it-off"></span><h3>How to turn it off<a class="headerlink" href="#how-to-turn-it-off" title="Permalink to this headline">¶</a></h3>
<p>If you don't want data to be auto-escaped, on a per-site, per-template level or
per-variable level, you can turn it off in several ways.</p>
<p>Why would you want to turn it off? Because sometimes, template variables
contain data that you <em>intend</em> to be rendered as raw HTML, in which case you
don't want their contents to be escaped. For example, you might store a blob of
HTML in your database and want to embed that directly into your template. Or,
you might be using Django's template system to produce text that is <em>not</em> HTML
-- like an e-mail message, for instance.</p>
<div class="section" id="s-for-individual-variables">
<span id="for-individual-variables"></span><h4>For individual variables<a class="headerlink" href="#for-individual-variables" title="Permalink to this headline">¶</a></h4>
<p>To disable auto-escaping for an individual variable, use the <tt class="docutils literal"><span class="pre">safe</span></tt> filter:</p>
<div class="highlight-html+django"><div class="highlight"><pre>This will be escaped: <span class="cp">{{</span> <span class="nv">data</span> <span class="cp">}}</span>
This 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>
</pre></div>
</div>
<p>Think of <em>safe</em> as shorthand for <em>safe from further escaping</em> or <em>can be
safely interpreted as HTML</em>. In this example, if <tt class="docutils literal"><span class="pre">data</span></tt> contains <tt class="docutils literal"><span class="pre">'&lt;b&gt;'</span></tt>,
the output will be:</p>
<div class="highlight-html+django"><div class="highlight"><pre>This will be escaped: <span class="ni">&amp;lt;</span>b<span class="ni">&amp;gt;</span>
This will not be escaped: <span class="nt">&lt;b&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-for-template-blocks">
<span id="for-template-blocks"></span><h4>For template blocks<a class="headerlink" href="#for-template-blocks" title="Permalink to this headline">¶</a></h4>
<p>To control auto-escaping for a template, wrap the template (or just a
particular section of the template) in the <tt class="docutils literal"><span class="pre">autoescape</span></tt> tag, like so:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">off</span> <span class="cp">%}</span>
    Hello <span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">autoescape</span></tt> 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 its argument. At
times, you might want to force auto-escaping when it would otherwise be
disabled. Here is an example template:</p>
<div class="highlight-html+django"><div class="highlight"><pre>Auto-escaping is on by default. Hello <span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span>

<span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">off</span> <span class="cp">%}</span>
    This will not be auto-escaped: <span class="cp">{{</span> <span class="nv">data</span> <span class="cp">}}</span>.

    Nor this: <span class="cp">{{</span> <span class="nv">other_data</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">on</span> <span class="cp">%}</span>
        Auto-escaping applies again: <span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The auto-escaping tag passes its effect onto templates that extend the
current one as well as templates included via the <tt class="docutils literal"><span class="pre">include</span></tt> tag, just like
all block tags. For example:</p>
<div class="highlight-html+django"><pre># base.html

{% autoescape off %}
&lt;h1&gt;{% block title %}{% endblock %}&lt;/h1&gt;
{% block content %}
{% endblock %}
{% endautoescape %}


# child.html

{% extends "base.html" %}
{% block title %}This &amp; that{% endblock %}
{% block content %}{{ greeting }}{% endblock %}</pre>
</div>
<p>Because auto-escaping is turned off in the base template, it will also be
turned off in the child template, resulting in the following rendered
HTML when the <tt class="docutils literal"><span class="pre">greeting</span></tt> variable contains the string <tt class="docutils literal"><span class="pre">&lt;b&gt;Hello!&lt;/b&gt;</span></tt>:</p>
<div class="highlight-html+django"><pre>&lt;h1&gt;This &amp; that&lt;/h1&gt;
&lt;b&gt;Hello!&lt;/b&gt;</pre>
</div>
</div>
</div>
<div class="section" id="s-notes">
<span id="notes"></span><h3>Notes<a class="headerlink" href="#notes" title="Permalink to this headline">¶</a></h3>
<p>Generally, template authors don't need to worry about auto-escaping very much.
Developers on the Python side (people writing views and custom filters) need to
think about the cases in which data shouldn't be escaped, and mark data
appropriately, so things Just Work in the template.</p>
<p>If you're creating a template that might be used in situations where you're
not sure whether auto-escaping is enabled, then add an <tt class="docutils literal"><span class="pre">escape</span></tt> filter to any
variable that needs escaping. When auto-escaping is on, there's no danger of
the <tt class="docutils literal"><span class="pre">escape</span></tt> filter <em>double-escaping</em> data -- the <tt class="docutils literal"><span class="pre">escape</span></tt> filter does not
affect auto-escaped variables.</p>
</div>
<div class="section" id="s-string-literals-and-automatic-escaping">
<span id="string-literals-and-automatic-escaping"></span><h3>String literals and automatic escaping<a class="headerlink" href="#string-literals-and-automatic-escaping" title="Permalink to this headline">¶</a></h3>
<p>As we mentioned earlier, filter arguments can be strings:</p>
<div class="highlight-html+django"><div class="highlight"><pre><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>
</pre></div>
</div>
<p>All string literals are inserted <strong>without</strong> any automatic escaping into the
template -- they act as if they were all passed through the <tt class="docutils literal"><span class="pre">safe</span></tt> filter.
The reasoning behind this is that the template author is in control of what
goes into the string literal, so they can make sure the text is correctly
escaped when the template is written.</p>
<p>This means you would write</p>
<div class="highlight-html+django"><div class="highlight"><pre><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>
</pre></div>
</div>
<p>...rather than</p>
<div class="highlight-html+django"><pre>{{ data|default:"3 &lt; 2" }}  &lt;-- Bad! Don't do this.</pre>
</div>
<p>This doesn't affect what happens to data coming from the variable itself.
The variable's contents are still automatically escaped, if necessary, because
they're beyond the control of the template author.</p>
</div>
</div>
<div class="section" id="s-using-the-built-in-reference">
<span id="s-template-built-in-reference"></span><span id="using-the-built-in-reference"></span><span id="template-built-in-reference"></span><h2>Using the built-in reference<a class="headerlink" href="#using-the-built-in-reference" title="Permalink to this headline">¶</a></h2>
<p>Django's admin interface includes a complete reference of all template tags and
filters available for a given site. To activate it, follow these steps:</p>
<ul class="simple">
<li>Add <tt class="xref py py-mod docutils literal"><span class="pre">django.contrib.admindocs</span></tt> to your <a class="reference internal" href="../ref/settings.html#std:setting-INSTALLED_APPS"><tt class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a>.</li>
<li>Add <tt class="docutils literal"><span class="pre">(r'^admin/doc/',</span> <span class="pre">include('django.contrib.admindocs.urls'))</span></tt> to your
<tt class="xref py py-data docutils literal"><span class="pre">urlpatterns</span></tt>. Make sure it's included <em>before</em> the <tt class="docutils literal"><span class="pre">r'^admin/'</span></tt>
entry, so that requests to <tt class="docutils literal"><span class="pre">/admin/doc/</span></tt> don't get handled by the
latter entry.</li>
<li>Install the docutils module (<a class="reference external" href="http://docutils.sf.net/">http://docutils.sf.net/</a>).</li>
</ul>
<p>After you've followed those steps, you can start browsing the documentation by
going to your admin interface and clicking the &quot;Documentation&quot; link in the
upper right of the page.</p>
<p>The reference is divided into 4 sections: tags, filters, models, and views.</p>
<p>The <strong>tags</strong> and <strong>filters</strong> sections describe all the built-in tags (in fact,
the tag and filter references below come directly from those pages) as well as
any custom tag or filter libraries available.</p>
<p>The <strong>views</strong> page is the most valuable. Each URL in your site has a separate
entry here, and clicking on a URL will show you:</p>
<ul class="simple">
<li>The name of the view function that generates that view.</li>
<li>A short description of what the view does.</li>
<li>The <strong>context</strong>, or a list of variables available in the view's template.</li>
<li>The name of the template or templates that are used for that view.</li>
</ul>
<p>Each view documentation page also has a bookmarklet that you can use to jump
from any page to the documentation page for that view.</p>
<p>Because Django-powered sites usually use database objects, the <strong>models</strong>
section of the documentation page describes each type of object in the system
along with all the fields available on that object.</p>
<p>Taken together, the documentation pages should tell you every tag, filter,
variable and object available to you in a given template.</p>
</div>
<div class="section" id="s-custom-tag-and-filter-libraries">
<span id="s-loading-custom-template-libraries"></span><span id="custom-tag-and-filter-libraries"></span><span id="loading-custom-template-libraries"></span><h2>Custom tag and filter libraries<a class="headerlink" href="#custom-tag-and-filter-libraries" title="Permalink to this headline">¶</a></h2>
<p>Certain applications provide custom tag and filter libraries. To access them in
a template, use the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">%}</span></tt> tag:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">load</span> <span class="nv">comments</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">comment_form</span> <span class="nv">for</span> <span class="nv">blogs.entries</span> <span class="nv">entry.id</span> <span class="k">with</span> <span class="nv">is_public</span> <span class="nv">yes</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>In the above, the <tt class="docutils literal"><span class="pre">load</span></tt> tag loads the <tt class="docutils literal"><span class="pre">comments</span></tt> tag library, which then
makes the <tt class="docutils literal"><span class="pre">comment_form</span></tt> tag available for use. Consult the documentation
area in your admin to find the list of custom libraries in your installation.</p>
<p>The <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">%}</span></tt> tag can take multiple library names, separated by spaces.
Example:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">load</span> <span class="nv">comments</span> <span class="nv">i18n</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="../howto/custom-template-tags.html"><em>Custom template tags and filters</em></a> for information on writing your own custom
template libraries.</p>
<div class="section" id="s-custom-libraries-and-template-inheritance">
<span id="custom-libraries-and-template-inheritance"></span><h3>Custom libraries and template inheritance<a class="headerlink" href="#custom-libraries-and-template-inheritance" title="Permalink to this headline">¶</a></h3>
<p>When you load a custom tag or filter library, the tags/filters are only made
available to the current template -- not any parent or child templates along
the template-inheritance path.</p>
<p>For example, if a template <tt class="docutils literal"><span class="pre">foo.html</span></tt> has <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">comments</span> <span class="pre">%}</span></tt>, a child
template (e.g., one that has <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">&quot;foo.html&quot;</span> <span class="pre">%}</span></tt>) will <em>not</em> have
access to the comments template tags and filters. The child template is
responsible for its own <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">comments</span> <span class="pre">%}</span></tt>.</p>
<p>This is a feature for the sake of maintainability and sanity.</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="#">The Django template language</a><ul>
<li><a class="reference internal" href="#templates">Templates</a></li>
<li><a class="reference internal" href="#variables">Variables</a></li>
<li><a class="reference internal" href="#filters">Filters</a></li>
<li><a class="reference internal" href="#tags">Tags</a></li>
<li><a class="reference internal" href="#comments">Comments</a></li>
<li><a class="reference internal" href="#template-inheritance">Template inheritance</a></li>
<li><a class="reference internal" href="#automatic-html-escaping">Automatic HTML escaping</a><ul>
<li><a class="reference internal" href="#how-to-turn-it-off">How to turn it off</a><ul>
<li><a class="reference internal" href="#for-individual-variables">For individual variables</a></li>
<li><a class="reference internal" href="#for-template-blocks">For template blocks</a></li>
</ul>
</li>
<li><a class="reference internal" href="#notes">Notes</a></li>
<li><a class="reference internal" href="#string-literals-and-automatic-escaping">String literals and automatic escaping</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-the-built-in-reference">Using the built-in reference</a></li>
<li><a class="reference internal" href="#custom-tag-and-filter-libraries">Custom tag and filter libraries</a><ul>
<li><a class="reference internal" href="#custom-libraries-and-template-inheritance">Custom libraries and template inheritance</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="forms/modelforms.html">Creating forms from models</a></li>
    
    
      <li>Next: <a href="generic-views.html">Generic views</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">Using Django</a>
        
        <ul><li>The Django template language</li></ul>
        </li></ul>
      </li>
  </ul>  

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/topics/templates.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">
    &laquo; <a href="forms/modelforms.html" title="Creating forms from models">previous</a> 
     |
    <a href="index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="generic-views.html" title="Generic views">next</a> &raquo;</div>
    </div>
  </div>

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