
<!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" lang="fr">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Balises et filtres de gabarit intégrés &#8212; Documentation Django 3.1.9.dev</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" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></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>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/translations.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Recherche" href="../../search.html" />
    <link rel="next" title="Le langage de gabarit de Django : pour les programmeurs Python" href="api.html" />
    <link rel="prev" title="Le langage de gabarit de Django" href="language.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "#";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Documentation Django 3.1.9.dev</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="language.html" title="Le langage de gabarit de Django">previous</a>
     |
    <a href="../index.html" title="Référence de l’API" accesskey="U">up</a>
   |
    <a href="api.html" title="Le langage de gabarit de Django&amp;nbsp;: pour les programmeurs Python">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-templates-builtins">
            
  <div class="section" id="s-built-in-template-tags-and-filters">
<span id="built-in-template-tags-and-filters"></span><h1>Balises et filtres de gabarit intégrés<a class="headerlink" href="#built-in-template-tags-and-filters" title="Lien permanent vers ce titre">¶</a></h1>
<p>Ce document décrit les balises et les filtres de gabarit intégrés dans Django. Il est recommandé d’utiliser la <a class="reference internal" href="../contrib/admin/admindocs.html"><span class="doc">documentation automatique</span></a>, si disponible, car elle inclura aussi la documentation de tout filtre ou balise personnalisé potentiellement installé.</p>
<div class="section" id="s-built-in-tag-reference">
<span id="s-ref-templates-builtins-tags"></span><span id="built-in-tag-reference"></span><span id="ref-templates-builtins-tags"></span><h2>Référence des balises intégrées<a class="headerlink" href="#built-in-tag-reference" title="Lien permanent vers ce titre">¶</a></h2>
<div class="section" id="s-autoescape">
<span id="s-std:templatetag-autoescape"></span><span id="autoescape"></span><span id="std:templatetag-autoescape"></span><h3><code class="docutils literal notranslate"><span class="pre">autoescape</span></code><a class="headerlink" href="#autoescape" title="Lien permanent vers ce titre">¶</a></h3>
<p>Contrôle le comportement actuel de l’échappement automatique. Cette balise accepte <code class="docutils literal notranslate"><span class="pre">on</span></code> ou <code class="docutils literal notranslate"><span class="pre">off</span></code> comme paramètre et cela détermine si l’échappement automatique est actif dans le bloc concerné. Le bloc doit être fermé par la balise fermante <code class="docutils literal notranslate"><span class="pre">endautoescape</span></code>.</p>
<p>Lorsque l’échappement automatique est actif, tous les contenus de variables sont soumis à l’échappement HTML avant d’être affichés (mais après l’application de filtres éventuels). C’est équivalent à l’application manuelle du filtre <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> à chaque variable.</p>
<p>Les seules exceptions sont les variables qui sont déjà marquées comme «&nbsp;sûres&nbsp;» (plus besoin d’être échappées), soit par le code qui a défini la variable en question ou parce qu’elle a déjà passé par un filtre <a class="reference internal" href="#std:templatefilter-safe"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">safe</span></code></a> ou <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a>.</p>
<p>Exemple d’utilisation&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">on</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">body</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-block">
<span id="s-std:templatetag-block"></span><span id="block"></span><span id="std:templatetag-block"></span><h3><code class="docutils literal notranslate"><span class="pre">block</span></code><a class="headerlink" href="#block" title="Lien permanent vers ce titre">¶</a></h3>
<p>Définit un bloc pouvant être surchargé par les gabarits enfants. Voir <a class="reference internal" href="language.html#template-inheritance"><span class="std std-ref">Héritage des gabarits</span></a> pour plus d’informations.</p>
</div>
<div class="section" id="s-comment">
<span id="s-std:templatetag-comment"></span><span id="comment"></span><span id="std:templatetag-comment"></span><h3><code class="docutils literal notranslate"><span class="pre">comment</span></code><a class="headerlink" href="#comment" title="Lien permanent vers ce titre">¶</a></h3>
<p>Ignore tout ce qui se trouve entre <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">comment</span> <span class="pre">%}</span></code> et <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">endcomment</span> <span class="pre">%}</span></code>. Une note facultative peut être insérée dans la première balise. Par exemple, cela peut être utile pour donner les raisons d’un bout de code que l’on place en commentaire.</p>
<p>Exemple d’utilisation&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Rendered text with <span class="cp">{{</span> <span class="nv">pub_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;c&quot;</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">comment</span> <span class="s2">&quot;Optional note&quot;</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Commented out text with <span class="cp">{{</span> <span class="nv">create_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;c&quot;</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endcomment</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Les balises <code class="docutils literal notranslate"><span class="pre">comment</span></code> ne peuvent pas être imbriquées.</p>
</div>
<div class="section" id="s-csrf-token">
<span id="s-std:templatetag-csrf_token"></span><span id="csrf-token"></span><span id="std:templatetag-csrf_token"></span><h3><code class="docutils literal notranslate"><span class="pre">csrf_token</span></code><a class="headerlink" href="#csrf-token" title="Lien permanent vers ce titre">¶</a></h3>
<p>Cette balise est utilisée pour la protection CSRF, comme décrit dans la documentation <a class="reference internal" href="../csrf.html"><span class="doc">Cross Site Request Forgeries</span></a>.</p>
</div>
<div class="section" id="s-cycle">
<span id="s-std:templatetag-cycle"></span><span id="cycle"></span><span id="std:templatetag-cycle"></span><h3><code class="docutils literal notranslate"><span class="pre">cycle</span></code><a class="headerlink" href="#cycle" title="Lien permanent vers ce titre">¶</a></h3>
<p>Affiche l’un de ses paramètres à chaque apparition de la balise. Le premier paramètre est affiché lors de la première apparition, le second paramètre lors de la seconde apparition, et ainsi de suite. Une fois que tous les paramètres ont été utilisés, la balise recommence avec le premier paramètre et l’affiche de nouveau.</p>
<p>Cette balise est particulièrement utile dans une boucle&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">&#39;row1&#39;</span> <span class="s1">&#39;row2&#39;</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>
        ...
    <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>La première itération produit du code HTML se référant à la classe <code class="docutils literal notranslate"><span class="pre">row1</span></code>, la deuxième à <code class="docutils literal notranslate"><span class="pre">row2</span></code>, la troisième de nouveau à <code class="docutils literal notranslate"><span class="pre">row1</span></code> et ainsi de suite pour chaque itération de la boucle.</p>
<p>Vous pouvez aussi utiliser des variables. Par exemple, si vous avez deux variables de gabarit, <code class="docutils literal notranslate"><span class="pre">rowvalue1</span></code> et <code class="docutils literal notranslate"><span class="pre">rowvalue2</span></code>, vous pouvez alterner entre leurs valeurs comme ceci&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowvalue1</span> <span class="nv">rowvalue2</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>
        ...
    <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Les variables incluses dans le cycle seront échappées. Vous pouvez désactiver l’échappement automatique avec&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">off</span> <span class="cp">%}{%</span> <span class="k">cycle</span> <span class="nv">rowvalue1</span> <span class="nv">rowvalue2</span> <span class="cp">%}{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>
        ...
    <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Vous pouvez mélanger des variables et des chaînes&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">&#39;row1&#39;</span> <span class="nv">rowvalue2</span> <span class="s1">&#39;row3&#39;</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>
        ...
    <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Dans certains cas, il peut être souhaitable de se référer à la valeur actuelle du cycle sans passer à la valeur suivante. Pour cela, donnez un nom à la balise <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></code> via «&nbsp;as&nbsp;», comme ceci&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">&#39;row1&#39;</span> <span class="s1">&#39;row2&#39;</span> <span class="k">as</span> <span class="nv">rowcolors</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>À partir de cet instance, vous pouvez insérer la valeur actuelle du cycle chaque fois que vous en avez besoin dans votre gabarit en vous référant au nom du cycle comme variable de contexte. Si vous souhaitez faire passer le cycle à la prochaine valeur indépendamment de la balise <code class="docutils literal notranslate"><span class="pre">cycle</span></code> d’origine, vous pouvez utiliser une autre balise <code class="docutils literal notranslate"><span class="pre">cycle</span></code> et indiquer le nom de la variable. Ainsi, le gabarit suivant&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">tr</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">&#39;row1&#39;</span> <span class="s1">&#39;row2&#39;</span> <span class="k">as</span> <span class="nv">rowcolors</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">rowcolors</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">tr</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowcolors</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">rowcolors</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>affichera&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">tr</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;row1&quot;</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;row1&quot;</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">tr</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;row2&quot;</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;row2&quot;</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Vous pouvez utiliser autant de valeurs que nécessaire dans une balise <code class="docutils literal notranslate"><span class="pre">cycle</span></code>, séparées par des espaces. Les valeurs entourées de guillemets simples (<code class="docutils literal notranslate"><span class="pre">'</span></code>)  ou doubles (<code class="docutils literal notranslate"><span class="pre">&quot;</span></code>) sont traitées comme des chaînes littérales, alors que les valeurs sans guillemets sont traitées comme des variables de gabarit.</p>
<p>Par défaut, lorsque vous utilisez le mot-clé <code class="docutils literal notranslate"><span class="pre">as</span></code> avec la balise <code class="docutils literal notranslate"><span class="pre">cycle</span></code>, l’utilisation de <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></code> qui démarre le cycle produira elle-même la première valeur du cycle. Cela peut poser problème si vous souhaitez exploiter la valeur dans une boucle imbriquée ou un gabarit inclus. Si vous ne voulez que déclarer le cycle mais sans produire la première valeur, vous pouvez ajouter un mot-clé <code class="docutils literal notranslate"><span class="pre">silent</span></code> comme dernier mot-clé de la balise. Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">obj</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">&#39;row1&#39;</span> <span class="s1">&#39;row2&#39;</span> <span class="k">as</span> <span class="nv">rowcolors</span> <span class="nv">silent</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">rowcolors</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">&quot;subtemplate.html&quot;</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Ceci va afficher une liste d’éléments <code class="docutils literal notranslate"><span class="pre">&lt;tr&gt;</span></code> avec l’attribut <code class="docutils literal notranslate"><span class="pre">class</span></code> alternant entre <code class="docutils literal notranslate"><span class="pre">row1</span></code> et <code class="docutils literal notranslate"><span class="pre">row2</span></code>. Le sous-gabarit aura accès à <code class="docutils literal notranslate"><span class="pre">rowcolors</span></code> dans son contexte et sa valeur correspondra à la valeur de <code class="docutils literal notranslate"><span class="pre">class</span></code> de la balise <code class="docutils literal notranslate"><span class="pre">&lt;tr&gt;</span></code> qui l’entoure. Si on avait omis d’ajouter le mot-clé <code class="docutils literal notranslate"><span class="pre">silent</span></code>, <code class="docutils literal notranslate"><span class="pre">row1</span></code> et <code class="docutils literal notranslate"><span class="pre">row2</span></code> auraient été affichés comme du texte normal, en dehors de l’élément <code class="docutils literal notranslate"><span class="pre">&lt;tr&gt;</span></code>.</p>
<p>Lorsque le mot-clé <code class="docutils literal notranslate"><span class="pre">silent</span></code> est utilisé dans une définition de cycle, cela s’applique automatiquement à toutes les utilisations ultérieures de cette balise <code class="docutils literal notranslate"><span class="pre">cycle</span></code> spécifique. Le gabarit suivant n’affiche <em>rien</em>, même si le second appel à <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></code> ne précise pas <code class="docutils literal notranslate"><span class="pre">silent</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">&#39;row1&#39;</span> <span class="s1">&#39;row2&#39;</span> <span class="k">as</span> <span class="nv">rowcolors</span> <span class="nv">silent</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowcolors</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Il est possible d’utiliser la balise <a class="reference internal" href="#std:templatetag-resetcycle"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">resetcycle</span></code></a> pour réinitialiser une balise <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></code> à sa première valeur lors de sa prochaine apparition.</p>
</div>
<div class="section" id="s-debug">
<span id="s-std:templatetag-debug"></span><span id="debug"></span><span id="std:templatetag-debug"></span><h3><code class="docutils literal notranslate"><span class="pre">debug</span></code><a class="headerlink" href="#debug" title="Lien permanent vers ce titre">¶</a></h3>
<p>Affiche tout un lot d’informations de débogage, y compris le contexte actuel et les modules importés.</p>
</div>
<div class="section" id="s-extends">
<span id="s-std:templatetag-extends"></span><span id="extends"></span><span id="std:templatetag-extends"></span><h3><code class="docutils literal notranslate"><span class="pre">extends</span></code><a class="headerlink" href="#extends" title="Lien permanent vers ce titre">¶</a></h3>
<p>Indique que ce gabarit étend un gabarit parent.</p>
<p>Cette balise peut être utilisée de deux façons&nbsp;:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">&quot;base.html&quot;</span> <span class="pre">%}</span></code> (avec guillemets) utilise la valeur littérale <code class="docutils literal notranslate"><span class="pre">&quot;base.html&quot;</span></code> comme nom du gabarit parent à étendre.</li>
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">variable</span> <span class="pre">%}</span></code> utilise la valeur de <code class="docutils literal notranslate"><span class="pre">variable</span></code>. Si la variable contient une chaîne de caractères, Django utilise celle-ci comme nom du gabarit parent. Si la variable contient un objet <code class="docutils literal notranslate"><span class="pre">Template</span></code>, Django utilise cet objet comme gabarit parent.</li>
</ul>
<p>Voir <a class="reference internal" href="language.html#template-inheritance"><span class="std std-ref">Héritage de gabarits</span></a> pour plus d’informations.</p>
<p>En principe, le nom du gabarit est relatif au répertoire racine du chargeur de gabarits. Un paramètre textuel peut aussi être un chemin relatif commençant par <code class="docutils literal notranslate"><span class="pre">./</span></code> ou <code class="docutils literal notranslate"><span class="pre">../</span></code>. Par exemple, en admettant la structure de répertoires suivante&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>dir1/
    template.html
    base2.html
    my/
        base3.html
base1.html
</pre></div>
</div>
<p>Dans <code class="docutils literal notranslate"><span class="pre">template.html</span></code>, les chemins suivants seraient valides&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">&quot;./base2.html&quot;</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">extends</span> <span class="s2">&quot;../base1.html&quot;</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">extends</span> <span class="s2">&quot;./my/base3.html&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-filter">
<span id="s-std:templatetag-filter"></span><span id="filter"></span><span id="std:templatetag-filter"></span><h3><code class="docutils literal notranslate"><span class="pre">filter</span></code><a class="headerlink" href="#filter" title="Lien permanent vers ce titre">¶</a></h3>
<p>Filtre le contenu du bloc au travers d’un ou plusieurs filtres. Plusieurs filtres doivent être séparés par la barre verticale et les filtres peuvent posséder des paramètres, tout comme dans la syntaxe des variables.</p>
<p>Notez que le bloc inclus <em>tout</em> le texte entre les balises <code class="docutils literal notranslate"><span class="pre">filter</span></code> et <code class="docutils literal notranslate"><span class="pre">endfilter</span></code>.</p>
<p>Exemple d’utilisation&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">filter</span> <span class="nf">force_escape</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">%}</span>
    This text will be HTML-escaped, and will appear in all lowercase.
<span class="cp">{%</span> <span class="k">endfilter</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Les filtres <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> et <a class="reference internal" href="#std:templatefilter-safe"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">safe</span></code></a> ne sont pas des paramètres acceptables. Vous pouvez toujours utiliser la balise <a class="reference internal" href="#std:templatetag-autoescape"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">autoescape</span></code></a> pour gérer l’échappement automatique dans des blocs de code de gabarit.</p>
</div>
</div>
<div class="section" id="s-firstof">
<span id="s-std:templatetag-firstof"></span><span id="firstof"></span><span id="std:templatetag-firstof"></span><h3><code class="docutils literal notranslate"><span class="pre">firstof</span></code><a class="headerlink" href="#firstof" title="Lien permanent vers ce titre">¶</a></h3>
<p>Affiche le premier paramètre qui ne vaut pas « faux » (c’est-à-dire qu’il existe, qu’il n’est pas vide, qu’il ne contient pas la valeur booléenne faux et qu’il ne contient pas la valeur numérique 0). N’affiche rien si toutes les valeurs transmises valent « faux ».</p>
<p>Exemple d’utilisation&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">firstof</span> <span class="nv">var1</span> <span class="nv">var2</span> <span class="nv">var3</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>C’est l’équivalent de&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">var1</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">var1</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">elif</span> <span class="nv">var2</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">var2</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">elif</span> <span class="nv">var3</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">var3</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Vous pouvez aussi utiliser une chaîne littérale comme valeur de repli dans le cas où toutes les variables spécifiées valent <code class="docutils literal notranslate"><span class="pre">False</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">firstof</span> <span class="nv">var1</span> <span class="nv">var2</span> <span class="nv">var3</span> <span class="s2">&quot;fallback value&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Cette balise échappe automatiquement les valeurs des variables. Vous pouvez désactiver l’échappement automatique avec&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">off</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">firstof</span> <span class="nv">var1</span> <span class="nv">var2</span> <span class="nv">var3</span> <span class="s2">&quot;&lt;strong&gt;fallback value&lt;/strong&gt;&quot;</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Ou si seulement certaines variables doivent être échappées, vous pouvez employer&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">firstof</span> <span class="nv">var1</span> <span class="nv">var2</span><span class="o">|</span><span class="nf">safe</span> <span class="nv">var3</span> <span class="s2">&quot;&lt;strong&gt;fallback value&lt;/strong&gt;&quot;</span><span class="o">|</span><span class="nf">safe</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Vous pouvez utiliser la syntaxe <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">firstof</span> <span class="pre">var1</span> <span class="pre">var2</span> <span class="pre">var3</span> <span class="pre">as</span> <span class="pre">value</span> <span class="pre">%}</span></code> pour stocker le résultat dans une variable.</p>
</div>
<div class="section" id="s-for">
<span id="s-std:templatetag-for"></span><span id="for"></span><span id="std:templatetag-for"></span><h3><code class="docutils literal notranslate"><span class="pre">for</span></code><a class="headerlink" href="#for" title="Lien permanent vers ce titre">¶</a></h3>
<p>Effectue une boucle sur chaque élément d’une liste, rendant disponible la valeur dans une variable de contexte. Par exemple, pour afficher une liste des athlètes contenus dans <code class="docutils literal notranslate"><span class="pre">athlete_list</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">ul</span><span class="p">&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="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>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Vous pouvez effectuer une boucle inversée sur une liste en utilisant <cite>{% for obj in list reversed %}`</cite>.</p>
<p>Si vous devez effectuer une boucle sur une liste de listes, vous pouvez isoler les valeurs de chaque sous-liste dans des variables individuelles. Par exemple, si votre contexte contient une liste de coordonnées (x, y) appelée <code class="docutils literal notranslate"><span class="pre">points</span></code>, voici comment vous pouvez afficher la liste des points&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">x</span><span class="o">,</span> <span class="nv">y</span> <span class="k">in</span> <span class="nv">points</span> <span class="cp">%}</span>
    There is a point at <span class="cp">{{</span> <span class="nv">x</span> <span class="cp">}}</span>,<span class="cp">{{</span> <span class="nv">y</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Cela peut aussi être utile si vous avez besoin d’accéder aux éléments d’un dictionnaire. Par exemple, si votre contexte contient un dictionnaire <code class="docutils literal notranslate"><span class="pre">data</span></code>, le code suivant affiche les clés et les valeurs de ce dictionnaire&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">key</span><span class="o">,</span> <span class="nv">value</span> <span class="k">in</span> <span class="nv">data.items</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">key</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">value</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>N’oubliez pas que pour l’opérateur point, la recherche de clé de dictionnaire est prioritaire par rapport à la recherche de méthode. Ainsi, si le dictionnaire <code class="docutils literal notranslate"><span class="pre">data</span></code> contient une clé nommée <code class="docutils literal notranslate"><span class="pre">'items'</span></code>, <code class="docutils literal notranslate"><span class="pre">data.items</span></code> renvoie <code class="docutils literal notranslate"><span class="pre">data['items']</span></code> au lieu de <code class="docutils literal notranslate"><span class="pre">data.items()</span></code>. Évitez d’ajouter des clés qui ont le même nom que des méthodes de dictionnaire si vous voulez utilisez ces méthodes dans les gabarits (<code class="docutils literal notranslate"><span class="pre">items</span></code>, <code class="docutils literal notranslate"><span class="pre">values</span></code>, <code class="docutils literal notranslate"><span class="pre">keys</span></code>, etc.). Vous trouverez plus de détails sur l’ordre de recherche de l’opérateur point dans la <a class="reference internal" href="language.html#template-variables"><span class="std std-ref">documentation des variables de gabarit</span></a>.</p>
<p>La boucle <code class="docutils literal notranslate"><span class="pre">for</span></code> définit un certain nombre de variables disponibles à l’intérieur de la boucle&nbsp;:</p>
<table class="docutils">
<colgroup>
<col width="36%" />
<col width="64%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Variable</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">forloop.counter</span></code></td>
<td>L’itération actuelle de la boucle (index à partir de 1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">forloop.counter0</span></code></td>
<td>L’itération actuelle de la boucle (index à partir de 0)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">forloop.revcounter</span></code></td>
<td>Le nombre d’itérations à partir de la fin de la boucle (index à partir de 1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">forloop.revcounter0</span></code></td>
<td>Le nombre d’itérations à partir de la fin de la boucle (index à partir de 0)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">forloop.first</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">True</span></code> si la boucle est dans sa première itération</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">forloop.last</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">True</span></code> si la boucle est dans sa dernière itération</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">forloop.parentloop</span></code></td>
<td>Pour les boucles imbriquées, il s’agit de la boucle de niveau supérieur</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="s-for-empty">
<span id="for-empty"></span><h3><code class="docutils literal notranslate"><span class="pre">for</span></code> … <code class="docutils literal notranslate"><span class="pre">empty</span></code><a class="headerlink" href="#for-empty" title="Lien permanent vers ce titre">¶</a></h3>
<p>La balise <code class="docutils literal notranslate"><span class="pre">for</span></code> accepte une clause facultative <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">empty</span> <span class="pre">%}</span></code> dont le contenu est affiché si la liste en paramètre est vide ou est introuvable&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">ul</span><span class="p">&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="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>
<span class="cp">{%</span> <span class="k">empty</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Sorry, no athletes in this list.<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Le code ci-dessus est équivalent, mais plus court, plus propre et peut-être plus rapide que ceci&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
  <span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">athlete</span> <span class="k">in</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
      <span class="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>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
  <span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Sorry, no athletes in this list.<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
  <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-if">
<span id="s-std:templatetag-if"></span><span id="if"></span><span id="std:templatetag-if"></span><h3><code class="docutils literal notranslate"><span class="pre">if</span></code><a class="headerlink" href="#if" title="Lien permanent vers ce titre">¶</a></h3>
<p>La balise <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">%}</span></code> évalue une variable et si celle-ci vaut <code class="docutils literal notranslate"><span class="pre">True</span></code> (c’est-à-dire qu’elle existe, n’est pas vide et ne contient pas la valeur booléenne faux), le contenu du bloc est affiché&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><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">elif</span> <span class="nv">athlete_in_locker_room_list</span> <span class="cp">%}</span>
    Athletes should be out of the locker room soon!
<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>Dans l’exemple ci-dessus, si <code class="docutils literal notranslate"><span class="pre">athlete_list</span></code> n’est pas vide, le nombre d’athlètes est affiché par la variable <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">athlete_list|length</span> <span class="pre">}}</span></code>.</p>
<p>Comme vous pouvez le voir, la balise <code class="docutils literal notranslate"><span class="pre">if</span></code> accepte une ou plusieurs clauses <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">elif</span> <span class="pre">%}</span></code> ou une clause <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></code> dont le contenu est affiché si toutes les conditions précédentes ont échoué. Ces clauses sont facultatives.</p>
<div class="section" id="s-boolean-operators">
<span id="boolean-operators"></span><h4>Opérateurs booléens<a class="headerlink" href="#boolean-operators" title="Lien permanent vers ce titre">¶</a></h4>
<p>Les balises <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> peuvent utiliser <code class="docutils literal notranslate"><span class="pre">and</span></code> (et), <code class="docutils literal notranslate"><span class="pre">or</span></code> (ou) ou <code class="docutils literal notranslate"><span class="pre">not</span></code> (non) pour tester un certain nombre de variables ou pour inverser la valeur d’une variable donnée&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">and</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    Both athletes and coaches are available.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    There are no athletes.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">or</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    There are some athletes or some coaches.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">athlete_list</span> <span class="k">or</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    There are no athletes or there are some coaches.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">and</span> <span class="k">not</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    There are some athletes and absolutely no coaches.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>L’utilisation à la fois de <code class="docutils literal notranslate"><span class="pre">and</span></code> et de <code class="docutils literal notranslate"><span class="pre">or</span></code> dans la même balise est permise, tenant compte que <code class="docutils literal notranslate"><span class="pre">and</span></code> a une plus grande priorité que <code class="docutils literal notranslate"><span class="pre">or</span></code>. Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">and</span> <span class="nv">coach_list</span> <span class="k">or</span> <span class="nv">cheerleader_list</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>sera interprété comme&nbsp;:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">athlete_list</span> <span class="ow">and</span> <span class="n">coach_list</span><span class="p">)</span> <span class="ow">or</span> <span class="n">cheerleader_list</span>
</pre></div>
</div>
<p>L’emploi de parenthèses dans la balise <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> est une erreur de syntaxe. Si vous en avez besoin pour modifier la priorité des opérateurs, vous devez passer par des balises <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> imbriquées.</p>
<p>Les balises <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> peuvent également utiliser les opérateurs <code class="docutils literal notranslate"><span class="pre">==</span></code>, <code class="docutils literal notranslate"><span class="pre">!=</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">in</span></code> (dans), <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code>, <code class="docutils literal notranslate"><span class="pre">is</span></code> et <code class="docutils literal notranslate"><span class="pre">is</span> <span class="pre">not</span></code> qui fonctionnent comme suit&nbsp;:</p>
<div class="section" id="s-operator">
<span id="operator"></span><h5>Opérateur <code class="docutils literal notranslate"><span class="pre">==</span></code><a class="headerlink" href="#operator" title="Lien permanent vers ce titre">¶</a></h5>
<p>Égalité. Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">==</span> <span class="s2">&quot;x&quot;</span> <span class="cp">%}</span>
  This appears if variable somevar equals the string &quot;x&quot;
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-id1">
<span id="id1"></span><h5>Opérateur <code class="docutils literal notranslate"><span class="pre">!=</span></code><a class="headerlink" href="#id1" title="Lien permanent vers ce titre">¶</a></h5>
<p>Inégalité. Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">!=</span> <span class="s2">&quot;x&quot;</span> <span class="cp">%}</span>
  This appears if variable somevar does not equal the string &quot;x&quot;,
  or if somevar is not found in the context
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-id2">
<span id="id2"></span><h5>Opérateur <code class="docutils literal notranslate"><span class="pre">&lt;</span></code><a class="headerlink" href="#id2" title="Lien permanent vers ce titre">¶</a></h5>
<p>Inférieur à. Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">&lt;</span> <span class="m">100</span> <span class="cp">%}</span>
  This appears if variable somevar is less than 100.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-id3">
<span id="id3"></span><h5>Opérateur <code class="docutils literal notranslate"><span class="pre">&gt;</span></code><a class="headerlink" href="#id3" title="Lien permanent vers ce titre">¶</a></h5>
<p>Supérieur à. Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">&gt;</span> <span class="m">0</span> <span class="cp">%}</span>
  This appears if variable somevar is greater than 0.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-id4">
<span id="id4"></span><h5>Opérateur <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code><a class="headerlink" href="#id4" title="Lien permanent vers ce titre">¶</a></h5>
<p>Inférieur ou égal à. Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">&lt;=</span> <span class="m">100</span> <span class="cp">%}</span>
  This appears if variable somevar is less than 100 or equal to 100.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-id5">
<span id="id5"></span><h5>Opérateur <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code><a class="headerlink" href="#id5" title="Lien permanent vers ce titre">¶</a></h5>
<p>Supérieur ou égal à. Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">&gt;=</span> <span class="m">1</span> <span class="cp">%}</span>
  This appears if variable somevar is greater than 1 or equal to 1.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-in-operator">
<span id="in-operator"></span><h5>Opérateur <code class="docutils literal notranslate"><span class="pre">in</span></code><a class="headerlink" href="#in-operator" title="Lien permanent vers ce titre">¶</a></h5>
<p>Contenu dans. Cet opérateur est pris en charge par de nombreux conteneurs Python pour tester si la valeur donnée se trouve dans le conteneur. Le code suivant montre quelques exemples d’interprétation de <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="s2">&quot;bc&quot;</span> <span class="k">in</span> <span class="s2">&quot;abcdef&quot;</span> <span class="cp">%}</span>
  This appears since &quot;bc&quot; is a substring of &quot;abcdef&quot;
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="s2">&quot;hello&quot;</span> <span class="k">in</span> <span class="nv">greetings</span> <span class="cp">%}</span>
  If greetings is a list or set, one element of which is the string
  &quot;hello&quot;, this will appear.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span>
  If users is a QuerySet, this will appear if user is an
  instance that belongs to the QuerySet.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-not-in-operator">
<span id="not-in-operator"></span><h5>Opérateur <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code><a class="headerlink" href="#not-in-operator" title="Lien permanent vers ce titre">¶</a></h5>
<p>Non contenu dans. C’est la négation de l’opérateur <code class="docutils literal notranslate"><span class="pre">in</span></code>.</p>
</div>
<div class="section" id="s-is-operator">
<span id="is-operator"></span><h5>Opérateur <code class="docutils literal notranslate"><span class="pre">is</span></code><a class="headerlink" href="#is-operator" title="Lien permanent vers ce titre">¶</a></h5>
<p>Identité d’objet. Teste si deux valeurs sont le même objet. Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="k">is</span> <span class="nf">True</span> <span class="cp">%}</span>
  This appears if and only if somevar is True.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="k">is</span> <span class="nf">None</span> <span class="cp">%}</span>
  This appears if somevar is None, or if somevar is not found in the context.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-is-not-operator">
<span id="is-not-operator"></span><h5>Opérateur <code class="docutils literal notranslate"><span class="pre">is</span> <span class="pre">not</span></code><a class="headerlink" href="#is-not-operator" title="Lien permanent vers ce titre">¶</a></h5>
<p>Négation d’identité d’objet. Teste si deux valeurs ne sont pas le même objet. C’est la négation de l’opérateur <code class="docutils literal notranslate"><span class="pre">is</span></code>. Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="k">is</span> <span class="k">not</span> <span class="nf">True</span> <span class="cp">%}</span>
  This appears if somevar is not True, or if somevar is not found in the
  context.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="k">is</span> <span class="k">not</span> <span class="nf">None</span> <span class="cp">%}</span>
  This appears if and only if somevar is not None.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-filters">
<span id="filters"></span><h4>Filtres<a class="headerlink" href="#filters" title="Lien permanent vers ce titre">¶</a></h4>
<p>Vous pouvez aussi utiliser des filtres dans l’expression <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a>. Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">messages</span><span class="o">|</span><span class="nf">length</span> <span class="o">&gt;=</span> <span class="m">100</span> <span class="cp">%}</span>
   You have lots of messages today!
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-complex-expressions">
<span id="complex-expressions"></span><h4>Expressions complexes<a class="headerlink" href="#complex-expressions" title="Lien permanent vers ce titre">¶</a></h4>
<p>Tous les éléments ci-dessus peuvent être combinés pour former des expressions complexes. Pour de telles expressions, il peut être important de savoir comment les opérateurs sont groupés au moment de l’évaluation de l’expression, c’est-à-dire les règles de priorité appliquées. La priorité des opérateurs, de la plus faible à la plus élevée, est dans cet ordre&nbsp;:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">or</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">and</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">not</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">in</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">==</span></code>, <code class="docutils literal notranslate"><span class="pre">!=</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code></li>
</ul>
<p>(Ceci suit exactement le comportement Python). Ainsi, par exemple, la balise <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> complexe suivante&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">a</span> <span class="o">==</span> <span class="nv">b</span> <span class="k">or</span> <span class="nv">c</span> <span class="o">==</span> <span class="nv">d</span> <span class="k">and</span> <span class="nv">e</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>…sera interprétée comme&nbsp;:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">)</span> <span class="ow">or</span> <span class="p">((</span><span class="n">c</span> <span class="o">==</span> <span class="n">d</span><span class="p">)</span> <span class="ow">and</span> <span class="n">e</span><span class="p">)</span>
</pre></div>
</div>
<p>Si vous devez modifier la priorité, il faudra imbriquer les balises <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a>. C’est parfois plus lisible aussi, notamment pour ceux qui ne sont pas très au clair au sujet des règles de priorité.</p>
<p>Les opérateurs de comparaison ne peuvent pas être enchaînés comme en Python ou en notation mathématique. Par exemple, au lieu d’écrire&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">a</span> <span class="o">&gt;</span> <span class="nv">b</span> <span class="o">&gt;</span> <span class="nv">c</span> <span class="cp">%}</span>  (WRONG)
</pre></div>
</div>
<p>vous devez écrire&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">a</span> <span class="o">&gt;</span> <span class="nv">b</span> <span class="k">and</span> <span class="nv">b</span> <span class="o">&gt;</span> <span class="nv">c</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-ifequal-and-ifnotequal">
<span id="ifequal-and-ifnotequal"></span><h3><code class="docutils literal notranslate"><span class="pre">ifequal</span></code> et <code class="docutils literal notranslate"><span class="pre">ifnotequal</span></code><a class="headerlink" href="#ifequal-and-ifnotequal" title="Lien permanent vers ce titre">¶</a></h3>
<div class="deprecated">
<p><span class="versionmodified">Obsolète depuis la version 3.1.</span></p>
</div>
<p><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">ifequal</span> <span class="pre">a</span> <span class="pre">b</span> <span class="pre">%}</span> <span class="pre">...</span> <span class="pre">{%</span> <span class="pre">endifequal</span> <span class="pre">%}</span></code> est une ancienne façon d’écrire <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">a</span> <span class="pre">==</span> <span class="pre">b</span> <span class="pre">%}</span> <span class="pre">...</span> <span class="pre">{%</span> <span class="pre">endif</span> <span class="pre">%}</span></code>. De même, <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">ifnotequal</span> <span class="pre">a</span> <span class="pre">b</span> <span class="pre">%}</span> <span class="pre">...</span> <span class="pre">{%</span> <span class="pre">endifnotequal</span> <span class="pre">%}</span></code> est maintenant remplacé par <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">a</span> <span class="pre">!=</span> <span class="pre">b</span> <span class="pre">%}</span> <span class="pre">...</span> <span class="pre">{%</span> <span class="pre">endif</span> <span class="pre">%}</span></code>.</p>
</div>
<div class="section" id="s-ifchanged">
<span id="s-std:templatetag-ifchanged"></span><span id="ifchanged"></span><span id="std:templatetag-ifchanged"></span><h3><code class="docutils literal notranslate"><span class="pre">ifchanged</span></code><a class="headerlink" href="#ifchanged" title="Lien permanent vers ce titre">¶</a></h3>
<p>Vérifie si une valeur a changé depuis l’itération précédente d’une boucle.</p>
<p>La balise de bloc <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">ifchanged</span> <span class="pre">%}</span></code> est employée dans une boucle. Elle présente deux utilisations possibles.</p>
<ol class="arabic">
<li><p class="first">Vérifie son propre contenu à afficher comparé à son état précédent et n’affiche le contenu que s’il a changé. Par exemple, le code suivant affiche une liste de jours, n’affichant le mois que lorsqu’il change&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>Archive for <span class="cp">{{</span> <span class="nv">year</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>

<span class="cp">{%</span> <span class="k">for</span> <span class="nv">date</span> <span class="k">in</span> <span class="nv">days</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="cp">%}</span><span class="p">&lt;</span><span class="nt">h3</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;F&quot;</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h3</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;M/d&quot;</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">}}</span><span class="s">/&quot;</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;j&quot;</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
</li>
<li><p class="first">Si la balise reçoit une ou plusieurs variables, elle vérifie si l’une des variables a changé. Par exemple, le code suivant affiche la date chaque fois qu’elle change et n’affiche l’heure que si l’heure ou la date a changé&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">date</span> <span class="k">in</span> <span class="nv">days</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">date.date</span> <span class="cp">%}</span> <span class="cp">{{</span> <span class="nv">date.date</span> <span class="cp">}}</span> <span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">date.hour</span> <span class="nv">date.date</span> <span class="cp">%}</span>
        <span class="cp">{{</span> <span class="nv">date.hour</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
</li>
</ol>
<p>La balise <code class="docutils literal notranslate"><span class="pre">ifchanged</span></code> accepte aussi une clause facultative <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></code> qui affiche son contenu si la valeur n’a pas changé&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">match</span> <span class="k">in</span> <span class="nv">matches</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">div</span> <span class="na">style</span><span class="o">=</span><span class="s">&quot;background-color:</span>
<span class="s">        </span><span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">match.ballot_id</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">            </span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s2">&quot;red&quot;</span> <span class="s2">&quot;blue&quot;</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">        </span><span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">            gray</span>
<span class="s">        </span><span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">    &quot;</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">match</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-include">
<span id="s-std:templatetag-include"></span><span id="include"></span><span id="std:templatetag-include"></span><h3><code class="docutils literal notranslate"><span class="pre">include</span></code><a class="headerlink" href="#include" title="Lien permanent vers ce titre">¶</a></h3>
<p>Charge un gabarit et l’affiche avec le contexte en cours. C’est une manière d’inclure d’autres gabarits dans un gabarit.</p>
<p>Le nom du gabarit peut être soit une variable, soit une chaîne fixe entre guillemets, simples ou doubles.</p>
<p>Cet exemple inclut le contenu du gabarit <code class="docutils literal notranslate"><span class="pre">&quot;foo/bar.html&quot;</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">&quot;foo/bar.html&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>En principe, le nom du gabarit est relatif au répertoire racine du chargeur de gabarits. Un paramètre textuel peut aussi être un chemin relatif commençant par <code class="docutils literal notranslate"><span class="pre">./</span></code> ou <code class="docutils literal notranslate"><span class="pre">../</span></code>, comme expliqué dans la documentation de la balise <a class="reference internal" href="#std:templatetag-extends"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">extends</span></code></a>.</p>
<p>Cet exemple inclut le contenu du gabarit dont le nom est contenu dans la variable <code class="docutils literal notranslate"><span class="pre">template_name</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="nv">template_name</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>La variable peut aussi être tout objet possédant une méthode <code class="docutils literal notranslate"><span class="pre">render()</span></code> acceptant un contexte. Cela permet de référencer un objet <code class="docutils literal notranslate"><span class="pre">Template</span></code> compilé dans votre contexte.</p>
<p>De plus, la variable peut être un itérable de noms de gabarits, auquel cas le premier pouvant être chargé sera utilisé, selon <a class="reference internal" href="../../topics/templates.html#django.template.loader.select_template" title="django.template.loader.select_template"><code class="xref py py-func docutils literal notranslate"><span class="pre">select_template()</span></code></a>.</p>
<p>Un gabarit inclus est affiché dans le contexte du gabarit qui l’inclut. Cet exemple produit le résultat <code class="docutils literal notranslate"><span class="pre">&quot;Hello,</span> <span class="pre">John!&quot;</span></code>:</p>
<ul>
<li><p class="first">Contexte&nbsp;: la variable <code class="docutils literal notranslate"><span class="pre">person</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;John&quot;</span></code> et la variable <code class="docutils literal notranslate"><span class="pre">greeting</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Hello&quot;</span></code>.</p>
</li>
<li><p class="first">Gabarit&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">&quot;name_snippet.html&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
</li>
<li><p class="first">Le gabarit <code class="docutils literal notranslate"><span class="pre">name_snippet.html</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">greeting</span> <span class="cp">}}</span>, <span class="cp">{{</span> <span class="nv">person</span><span class="o">|</span><span class="nf">default</span><span class="s2">:&quot;friend&quot;</span> <span class="cp">}}</span>!
</pre></div>
</div>
</li>
</ul>
<p>Vous pouvez transmettre du contexte supplémentaire au gabarit en utilisant des paramètres nommés&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">&quot;name_snippet.html&quot;</span> <span class="k">with</span> <span class="nv">person</span><span class="o">=</span><span class="s2">&quot;Jane&quot;</span> <span class="nv">greeting</span><span class="o">=</span><span class="s2">&quot;Hello&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Si vous souhaitez rendre le contexte avec seulement les variables indiquées (ou même sans variable du tout), utilisez l’option <code class="docutils literal notranslate"><span class="pre">only</span></code>. Aucune autre variable n’est alors disponible dans le gabarit inclus&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">&quot;name_snippet.html&quot;</span> <span class="k">with</span> <span class="nv">greeting</span><span class="o">=</span><span class="s2">&quot;Hi&quot;</span> <span class="nv">only</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>La balise <a class="reference internal" href="#std:templatetag-include"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">include</span></code></a> doit être considérée comme une implémentation de «&nbsp;effectuer le rendu de ce sous-gabarit et inclure le HTML résultant&nbsp;», et non pas «&nbsp;analyser ce sous-gabarit et inclure son contenu comme s’il faisait partie du parent&nbsp;». Cela signifie qu’il n’y a pas d’état partagé entre les gabarits inclus, chaque inclusion faisant l’objet d’un processus de rendu totalement autonome.</p>
<p class="last">Les blocs sont évalués <em>avant</em> d’être inclus. Cela signifie qu’un gabarit qui inclut des blocs d’un autre contiendra des blocs qui ont <em>déjà été évalués et produits</em>, et non pas des blocs qui peuvent être surchargés, par exemple par un gabarit d’extension.</p>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 3.1:</span> <p>La prise en charge d’un objet itérable de noms de gabarits a été ajoutée.</p>
</div>
</div>
<div class="section" id="s-load">
<span id="s-std:templatetag-load"></span><span id="load"></span><span id="std:templatetag-load"></span><h3><code class="docutils literal notranslate"><span class="pre">load</span></code><a class="headerlink" href="#load" title="Lien permanent vers ce titre">¶</a></h3>
<p>Charge un lot de balises de gabarit personnalisées.</p>
<p>Par exemple, le gabarit suivant charge toutes les balises et tous les filtres enregistrés dans <code class="docutils literal notranslate"><span class="pre">somelibrary</span></code> et dans <code class="docutils literal notranslate"><span class="pre">otherlibrary</span></code> se trouvant dans le paquet <code class="docutils literal notranslate"><span class="pre">package</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">somelibrary</span> <span class="nv">package.otherlibrary</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Vous pouvez aussi charger de manière sélective des filtres ou des balises à partir d’une bibliothèque avec le paramètre <code class="docutils literal notranslate"><span class="pre">from</span></code>. Dans cet exemple, les balises/filtres de gabarit <code class="docutils literal notranslate"><span class="pre">foo</span></code> et <code class="docutils literal notranslate"><span class="pre">bar</span></code> sont chargés à partir de <code class="docutils literal notranslate"><span class="pre">somelibrary</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">foo</span> <span class="nv">bar</span> <span class="nv">from</span> <span class="nv">somelibrary</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Voir <a class="reference internal" href="../../howto/custom-template-tags.html"><span class="doc">Bibliothèques de balises et de filtres personnalisés</span></a> pour plus d’informations.</p>
</div>
<div class="section" id="s-lorem">
<span id="s-std:templatetag-lorem"></span><span id="lorem"></span><span id="std:templatetag-lorem"></span><h3><code class="docutils literal notranslate"><span class="pre">lorem</span></code><a class="headerlink" href="#lorem" title="Lien permanent vers ce titre">¶</a></h3>
<p>Affiche du texte latin aléatoire «&nbsp;lorem ipsum&nbsp;». C’est pratique pour produire des données d’exemple dans les gabarits.</p>
<p>Utilisation&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">lorem</span> <span class="o">[</span><span class="nv">count</span><span class="o">]</span> <span class="o">[</span><span class="nv">method</span><span class="o">]</span> <span class="o">[</span><span class="nv">random</span><span class="o">]</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>La balise <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">lorem</span> <span class="pre">%}</span></code> peut être utilisée avec zéro, un, deux ou trois paramètres. Ces paramètres sont&nbsp;:</p>
<table class="docutils">
<colgroup>
<col width="15%" />
<col width="85%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Paramètre</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">count</span></code></td>
<td>Un nombre (ou une variable) contenant le nombre de paragraphes ou de mots à produire (1 par défaut).</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">method</span></code></td>
<td>Indiquez <code class="docutils literal notranslate"><span class="pre">w</span></code> pour des mots, <code class="docutils literal notranslate"><span class="pre">p</span></code> pour des paragraphes HTML ou <code class="docutils literal notranslate"><span class="pre">b</span></code> pour des blocs de paragraphe de texte brut (<code class="docutils literal notranslate"><span class="pre">b</span></code> est la valeur par défaut).</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">random</span></code></td>
<td>Le mot <code class="docutils literal notranslate"><span class="pre">random</span></code>, quand il est indiqué, n’utilise pas le paragraphe connu («&nbsp;Lorem ipsum dolor sit amet…&nbsp;») lors de la production de texte.</td>
</tr>
</tbody>
</table>
<p>Exemples&nbsp;:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">lorem</span> <span class="pre">%}</span></code> produit le paragraphe habituel «&nbsp;lorem ipsum&nbsp;».</li>
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">lorem</span> <span class="pre">3</span> <span class="pre">p</span> <span class="pre">%}</span></code> produit le paragraphe habituel « lorem ipsum » ainsi que deux paragraphes aléatoires, tous insérés dans des balises HTML <code class="docutils literal notranslate"><span class="pre">&lt;p&gt;</span></code>.</li>
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">lorem</span> <span class="pre">2</span> <span class="pre">w</span> <span class="pre">random</span> <span class="pre">%}</span></code> produit deux mots latins aléatoires.</li>
</ul>
</div>
<div class="section" id="s-now">
<span id="s-std:templatetag-now"></span><span id="now"></span><span id="std:templatetag-now"></span><h3><code class="docutils literal notranslate"><span class="pre">now</span></code><a class="headerlink" href="#now" title="Lien permanent vers ce titre">¶</a></h3>
<p>Affiche l’heure ou la date actuelle, en utilisant un format correspondant à la chaîne donnée. Cette chaîne peut contenir des spécificateurs de format comme décrit dans la section du filtre <a class="reference internal" href="#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a>.</p>
<p>Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>It is <span class="cp">{%</span> <span class="k">now</span> <span class="s2">&quot;jS F Y H:i&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Notez que vous pouvez échapper par barre oblique inverse une chaîne de format si vous souhaitez utiliser la valeur «&nbsp;brute&nbsp;». Dans cet exemple, «&nbsp;f&nbsp;» et «&nbsp;o&nbsp;» sont tous deux échappés par barre oblique inverse, sinon ils seraient chacun interprétés comme une chaîne de format pour afficher respectivement l’année et l’heure&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>It is the <span class="cp">{%</span> <span class="k">now</span> <span class="s2">&quot;jS \o\f F&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Le résultat affiché est «&nbsp;It is the 4th of September&nbsp;».</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Le format indiqué peut aussi correspondre à l’un des formats prédéfinis <a class="reference internal" href="../settings.html#std:setting-DATE_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DATE_FORMAT</span></code></a>, <a class="reference internal" href="../settings.html#std:setting-DATETIME_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DATETIME_FORMAT</span></code></a>, <a class="reference internal" href="../settings.html#std:setting-SHORT_DATE_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">SHORT_DATE_FORMAT</span></code></a> ou <a class="reference internal" href="../settings.html#std:setting-SHORT_DATETIME_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">SHORT_DATETIME_FORMAT</span></code></a>. Les formats prédéfinis peuvent varier en fonction de la langue active et de l’activation de la <a class="reference internal" href="../../topics/i18n/formatting.html"><span class="doc">Régionalisation des formats</span></a>, par ex.&nbsp;:</p>
<div class="last highlight-html+django notranslate"><div class="highlight"><pre><span></span>It is <span class="cp">{%</span> <span class="k">now</span> <span class="s2">&quot;SHORT_DATETIME_FORMAT&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<p>Vous pouvez également utiliser la syntaxe <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">now</span> <span class="pre">&quot;Y&quot;</span> <span class="pre">as</span> <span class="pre">current_year</span> <span class="pre">%}</span></code> pour stocker le résultat dans une variable (une chaîne de caractères). Cela peut être utile lorsqu’on veut utiliser <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">now</span> <span class="pre">%}</span></code> dans une balise de gabarit telle que <a class="reference internal" href="../../topics/i18n/translation.html#std:templatetag-blocktranslate"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">blocktranslate</span></code></a> par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">now</span> <span class="s2">&quot;Y&quot;</span> <span class="k">as</span> <span class="nv">current_year</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">blocktranslate</span> <span class="cp">%}</span>Copyright <span class="cp">{{</span> <span class="nv">current_year</span> <span class="cp">}}{%</span> <span class="k">endblocktranslate</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-regroup">
<span id="s-std:templatetag-regroup"></span><span id="regroup"></span><span id="std:templatetag-regroup"></span><h3><code class="docutils literal notranslate"><span class="pre">regroup</span></code><a class="headerlink" href="#regroup" title="Lien permanent vers ce titre">¶</a></h3>
<p>Regroupe une liste d’objets semblables par un attribut commun.</p>
<p>La meilleure façon d’illustrer cette balise complexe est de donner un exemple&nbsp;: disons que <code class="docutils literal notranslate"><span class="pre">cities</span></code> contient une liste de villes représentées par des dictionnaires contenant les clés <code class="docutils literal notranslate"><span class="pre">&quot;name&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;population&quot;</span></code> et <code class="docutils literal notranslate"><span class="pre">&quot;country&quot;</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">cities</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Mumbai&#39;</span><span class="p">,</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span> <span class="s1">&#39;19,000,000&#39;</span><span class="p">,</span> <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="s1">&#39;India&#39;</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Calcutta&#39;</span><span class="p">,</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span> <span class="s1">&#39;15,000,000&#39;</span><span class="p">,</span> <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="s1">&#39;India&#39;</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;New York&#39;</span><span class="p">,</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span> <span class="s1">&#39;20,000,000&#39;</span><span class="p">,</span> <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="s1">&#39;USA&#39;</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Chicago&#39;</span><span class="p">,</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span> <span class="s1">&#39;7,000,000&#39;</span><span class="p">,</span> <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="s1">&#39;USA&#39;</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Tokyo&#39;</span><span class="p">,</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span> <span class="s1">&#39;33,000,000&#39;</span><span class="p">,</span> <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="s1">&#39;Japan&#39;</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>…et vous aimeriez afficher une liste hiérarchique triée par pays, comme ceci&nbsp;:</p>
<ul class="simple">
<li>India<ul>
<li>Mumbai: 19,000,000</li>
<li>Calcutta: 15,000,000</li>
</ul>
</li>
<li>USA<ul>
<li>New York: 20,000,000</li>
<li>Chicago: 7,000,000</li>
</ul>
</li>
<li>Japan<ul>
<li>Tokyo: 33,000,000</li>
</ul>
</li>
</ul>
<p>Vous pouvez utiliser la balise <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> pour grouper la liste des villes par pays. L’extrait de code de gabarit suivant effectue cela&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">cities</span> <span class="nv">by</span> <span class="nv">country</span> <span class="k">as</span> <span class="nv">country_list</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">country</span> <span class="k">in</span> <span class="nv">country_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">country.grouper</span> <span class="cp">}}</span>
    <span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">for</span> <span class="nv">city</span> <span class="k">in</span> <span class="nv">country.list</span> <span class="cp">%}</span>
          <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">city.name</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">city.population</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Parcourons cet exemple. <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> accepte trois paramètres&nbsp;: la liste que vous souhaitez regrouper, l’attribut sur lequel grouper et le nom de la liste résultante. Dans ce cas, nous regroupons la liste <code class="docutils literal notranslate"><span class="pre">cities</span></code> par l’attribut <code class="docutils literal notranslate"><span class="pre">country</span></code> et nommons le résultat <code class="docutils literal notranslate"><span class="pre">country_list</span></code>.</p>
<p><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> produit une liste (dans ce cas, <code class="docutils literal notranslate"><span class="pre">country_list</span></code>) d”<strong>objets groupes</strong>. Les objets groupes sont des instances de <a class="reference external" href="https://docs.python.org/3/library/collections.html#collections.namedtuple" title="(disponible dans Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuple()</span></code></a> contenant deux champs&nbsp;:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">grouper</span></code>, l’élément qui a servi au groupement (par ex. la chaîne «&nbsp;India&nbsp;» ou «&nbsp;Japan&nbsp;»).</li>
<li><code class="docutils literal notranslate"><span class="pre">list</span></code>, une liste de tous les éléments du groupe (par ex. une liste de toutes les villes ayant <code class="docutils literal notranslate"><span class="pre">country='India'</span></code>).</li>
</ul>
<p>Comme <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> produit des objets <a class="reference external" href="https://docs.python.org/3/library/collections.html#collections.namedtuple" title="(disponible dans Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuple()</span></code></a>, il est également possible d’écrire l’exemple précédent comme ceci&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">cities</span> <span class="nv">by</span> <span class="nv">country</span> <span class="k">as</span> <span class="nv">country_list</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">country</span><span class="o">,</span> <span class="nv">local_cities</span> <span class="k">in</span> <span class="nv">country_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">country</span> <span class="cp">}}</span>
    <span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">for</span> <span class="nv">city</span> <span class="k">in</span> <span class="nv">local_cities</span> <span class="cp">%}</span>
          <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">city.name</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">city.population</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Notez que <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> ne trie pas ce qu’il reçoit&nbsp;! Notre exemple compte sur le fait que la liste <code class="docutils literal notranslate"><span class="pre">cities</span></code> est préalablement triée par <code class="docutils literal notranslate"><span class="pre">country</span></code>. Si la liste <code class="docutils literal notranslate"><span class="pre">cities</span></code> n’était <em>pas</em> triée par pays, le regroupement aurait naïvement affiché plus d’un groupe par pays. Par exemple, si la liste <code class="docutils literal notranslate"><span class="pre">cities</span></code> contenait ceci (notez que les pays ne sont pas regroupés)&nbsp;:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">cities</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Mumbai&#39;</span><span class="p">,</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span> <span class="s1">&#39;19,000,000&#39;</span><span class="p">,</span> <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="s1">&#39;India&#39;</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;New York&#39;</span><span class="p">,</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span> <span class="s1">&#39;20,000,000&#39;</span><span class="p">,</span> <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="s1">&#39;USA&#39;</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Calcutta&#39;</span><span class="p">,</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span> <span class="s1">&#39;15,000,000&#39;</span><span class="p">,</span> <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="s1">&#39;India&#39;</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Chicago&#39;</span><span class="p">,</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span> <span class="s1">&#39;7,000,000&#39;</span><span class="p">,</span> <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="s1">&#39;USA&#39;</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Tokyo&#39;</span><span class="p">,</span> <span class="s1">&#39;population&#39;</span><span class="p">:</span> <span class="s1">&#39;33,000,000&#39;</span><span class="p">,</span> <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="s1">&#39;Japan&#39;</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Avec ce contenu dans <code class="docutils literal notranslate"><span class="pre">cities</span></code>, l’exemple de code de gabarit pour <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> ci-dessus produirait le résultat suivant&nbsp;:</p>
<ul class="simple">
<li>India<ul>
<li>Mumbai: 19,000,000</li>
</ul>
</li>
<li>USA<ul>
<li>New York: 20,000,000</li>
</ul>
</li>
<li>India<ul>
<li>Calcutta: 15,000,000</li>
</ul>
</li>
<li>USA<ul>
<li>Chicago: 7,000,000</li>
</ul>
</li>
<li>Japan<ul>
<li>Tokyo: 33,000,000</li>
</ul>
</li>
</ul>
<p>La solution la plus simple à ce problème est de s’assurer au niveau de la vue que les données sont triées d’après le critère que vous voulez utiliser pour l’affichage.</p>
<p>Un autre solution est de trier les données dans le gabarit avec le filtre <a class="reference internal" href="#std:templatefilter-dictsort"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">dictsort</span></code></a>, pour autant que les données soient constituées d’une liste de dictionnaires&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">cities</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:&quot;country&quot;</span> <span class="nv">by</span> <span class="nv">country</span> <span class="k">as</span> <span class="nv">country_list</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="section" id="s-grouping-on-other-properties">
<span id="grouping-on-other-properties"></span><h4>Groupement selon d’autres propriétés<a class="headerlink" href="#grouping-on-other-properties" title="Lien permanent vers ce titre">¶</a></h4>
<p>Toute consultation d’attribut acceptée dans les gabarits est utilisable comme attribut de groupement pour la balise <code class="docutils literal notranslate"><span class="pre">regroup</span></code>, y compris les méthodes, les attributs, les clés de dictionnaire et les éléments de liste. Par exemple, si le champ «&nbsp;country&nbsp;» est une clé étrangère vers une classe possédant un attribut «&nbsp;description&nbsp;», vous pourriez écrire&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">cities</span> <span class="nv">by</span> <span class="nv">country.description</span> <span class="k">as</span> <span class="nv">country_list</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Ou si <code class="docutils literal notranslate"><span class="pre">country</span></code> est un champ contenant des choix (<code class="docutils literal notranslate"><span class="pre">choices</span></code>), il disposera d’une méthode <a class="reference internal" href="../models/instances.html#django.db.models.Model.get_FOO_display" title="django.db.models.Model.get_FOO_display"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_FOO_display()</span></code></a> disponible comme attribut vous permettant de regrouper sur la chaîne visible plutôt que sur la clé du choix&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">cities</span> <span class="nv">by</span> <span class="nv">get_country_display</span> <span class="k">as</span> <span class="nv">country_list</span> <span class="cp">%}</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">country.grouper</span> <span class="pre">}}</span></code> contient maintenant la partie valeur des choix <code class="docutils literal notranslate"><span class="pre">choices</span></code> plutôt que leur clé.</p>
</div>
</div>
<div class="section" id="s-resetcycle">
<span id="s-std:templatetag-resetcycle"></span><span id="resetcycle"></span><span id="std:templatetag-resetcycle"></span><h3><code class="docutils literal notranslate"><span class="pre">resetcycle</span></code><a class="headerlink" href="#resetcycle" title="Lien permanent vers ce titre">¶</a></h3>
<p>Réinitialise un <a class="reference internal" href="#cycle">cycle</a> précédent afin qu’il recommence depuis son premier élément à la prochaine occurrence. Sans paramètre, <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">resetcycle</span> <span class="pre">%}</span></code> réinitialise le dernier <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></code> défini dans le gabarit.</p>
<p>Exemple d’utilisation&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">coach</span> <span class="k">in</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">coach.name</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">athlete</span> <span class="k">in</span> <span class="nv">coach.athlete_set.all</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">&#39;odd&#39;</span> <span class="s1">&#39;even&#39;</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">athlete.name</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">resetcycle</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Cet exemple produit le code HTML suivant&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>José Mourinho<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;odd&quot;</span><span class="p">&gt;</span>Thibaut Courtois<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;even&quot;</span><span class="p">&gt;</span>John Terry<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;odd&quot;</span><span class="p">&gt;</span>Eden Hazard<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>Carlo Ancelotti<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;odd&quot;</span><span class="p">&gt;</span>Manuel Neuer<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;even&quot;</span><span class="p">&gt;</span>Thomas Müller<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Remarquez comme le premier bloc se termine avec <code class="docutils literal notranslate"><span class="pre">class=&quot;odd&quot;</span></code> et le nouveau qui commence avec <code class="docutils literal notranslate"><span class="pre">class=&quot;odd&quot;</span></code>. Sans la balise <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">resetcycle</span> <span class="pre">%}</span></code>, le second bloc aurait commencé avec <code class="docutils literal notranslate"><span class="pre">class=&quot;even&quot;</span></code>.</p>
<p>Il est aussi possible de réinitialiser les balises <code class="docutils literal notranslate"><span class="pre">cycle</span></code> nommées&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">&#39;odd&#39;</span> <span class="s1">&#39;even&#39;</span> <span class="k">as</span> <span class="nv">stripe</span> <span class="cp">%}</span><span class="s"> </span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">&#39;major&#39;</span> <span class="s1">&#39;minor&#39;</span> <span class="s1">&#39;minor&#39;</span> <span class="s1">&#39;minor&#39;</span> <span class="s1">&#39;minor&#39;</span> <span class="k">as</span> <span class="nv">tick</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>
        <span class="cp">{{</span> <span class="nv">item.data</span> <span class="cp">}}</span>
    <span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">item.category</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">item.category</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nb">forloop</span><span class="nv">.first</span> <span class="cp">%}{%</span> <span class="k">resetcycle</span> <span class="nv">tick</span> <span class="cp">%}{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Dans cet exemple, nous avons à la fois les lignes alternées pair/impair et une ligne «&nbsp;principale&nbsp;» toutes les cinq lignes. Seul le cycle des cinq lignes est réinitialisé lorsqu’une catégorie change.</p>
</div>
<div class="section" id="s-spaceless">
<span id="s-std:templatetag-spaceless"></span><span id="spaceless"></span><span id="std:templatetag-spaceless"></span><h3><code class="docutils literal notranslate"><span class="pre">spaceless</span></code><a class="headerlink" href="#spaceless" title="Lien permanent vers ce titre">¶</a></h3>
<p>Supprime les espaces entre les balises HTML. Les caractères de tabulation et les sauts de ligne sont aussi considérés comme des espaces.</p>
<p>Exemple d’utilisation&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;foo/&quot;</span><span class="p">&gt;</span>Foo<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Cet exemple produit le code HTML suivant&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;foo/&quot;</span><span class="p">&gt;</span>Foo<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Seuls les espaces entre les <em>balises</em> elles-mêmes sont supprimés, pas ceux entre les balises et le texte. Dans cet exemple, les espaces autour de <code class="docutils literal notranslate"><span class="pre">Hello</span></code> ne seront pas enlevés&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">strong</span><span class="p">&gt;</span>
        Hello
    <span class="p">&lt;/</span><span class="nt">strong</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-templatetag">
<span id="s-std:templatetag-templatetag"></span><span id="templatetag"></span><span id="std:templatetag-templatetag"></span><h3><code class="docutils literal notranslate"><span class="pre">templatetag</span></code><a class="headerlink" href="#templatetag" title="Lien permanent vers ce titre">¶</a></h3>
<p>Affiche l’une des séquences de caractères utilisées dans la syntaxe des balises de gabarit.</p>
<p>Comme le système des gabarits n’a pas de concept d”«&nbsp;échappement&nbsp;», si vous souhaitez afficher des parties de syntaxe utilisées dans les balises de gabarit, vous devez utiliser la balise <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">templatetag</span> <span class="pre">%}</span></code>.</p>
<p>Le paramètre indique quelle séquence de gabarit vous voulez afficher&nbsp;:</p>
<table class="docutils">
<colgroup>
<col width="72%" />
<col width="28%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Paramètre</th>
<th class="head">Affiche</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">openblock</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{%</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">closeblock</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">%}</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">openvariable</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">closevariable</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">}}</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">openbrace</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">closebrace</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">}</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">opencomment</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{#</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">closecomment</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">#}</span></code></td>
</tr>
</tbody>
</table>
<p>Exemple d’utilisation&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">templatetag</span> <span class="nv">openblock</span> <span class="cp">%}</span> url &#39;entry_list&#39; <span class="cp">{%</span> <span class="k">templatetag</span> <span class="nv">closeblock</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-url">
<span id="s-std:templatetag-url"></span><span id="url"></span><span id="std:templatetag-url"></span><h3><code class="docutils literal notranslate"><span class="pre">url</span></code><a class="headerlink" href="#url" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie un référence de chemin absolu (une URL sans le nom de domaine) correspondant à une vue et des paramètres facultatifs. Tout caractère spécial dans le chemin résultant sera codé avec  <a class="reference internal" href="../utils.html#django.utils.encoding.iri_to_uri" title="django.utils.encoding.iri_to_uri"><code class="xref py py-func docutils literal notranslate"><span class="pre">iri_to_uri()</span></code></a>.</p>
<p>C’est une manière d’afficher des liens sans trahir le principe DRY («&nbsp;ne pas se répéter&nbsp;»), évitant de devoir figer les URL dans les gabarits&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;some-url-name&#39;</span> <span class="nv">v1</span> <span class="nv">v2</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Le premier paramètre est un <a class="reference internal" href="../../topics/http/urls.html#naming-url-patterns"><span class="std std-ref">nom de motif d’URL</span></a>. Cela peut être une chaîne littérale entre guillemets ou une variable de contexte. Des paramètres supplémentaires sont facultatifs et sont fournis sous forme de valeurs séparées par des espaces qui seront utilisées comme paramètres de l’URL. L’exemple ci-dessus montre comment passer des paramètres positionnels. Il est aussi possible d’utiliser la syntaxe par mot-clé</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;some-url-name&#39;</span> <span class="nv">arg1</span><span class="o">=</span><span class="nv">v1</span> <span class="nv">arg2</span><span class="o">=</span><span class="nv">v2</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Ne mélangez pas la syntaxe positionnelle avec la syntaxe par mot-clé dans le même appel. Tous les paramètres requis par la configuration d’URL doivent être présents.</p>
<p>Par exemple, supposons que vous avez une vue, <code class="docutils literal notranslate"><span class="pre">app_views.client</span></code>, dont la configuration d’URL accepte un identifiant de client (ici, <code class="docutils literal notranslate"><span class="pre">client()</span></code> est une méthode dans le fichier de vues <code class="docutils literal notranslate"><span class="pre">app_views.py</span></code>). La ligne de configuration d’URL pourrait ressembler à ceci&nbsp;:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">path</span><span class="p">(</span><span class="s1">&#39;client/&lt;int:id&gt;/&#39;</span><span class="p">,</span> <span class="n">app_views</span><span class="o">.</span><span class="n">client</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;app-views-client&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Si la configuration d’URL de cette application est incluse dans la configuration d’URL du projet par un chemin tel que celui-ci&nbsp;:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">path</span><span class="p">(</span><span class="s1">&#39;clients/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s1">&#39;project_name.app_name.urls&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>…vous pouvez alors dans le gabarit créer un lien vers cette vue comme ceci&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;app-views-client&#39;</span> <span class="nv">client.id</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>La balise de gabarit affichera le texte <code class="docutils literal notranslate"><span class="pre">/clients/client/123/</span></code>.</p>
<p>Notez que si l’URL que vous résolvez n’existe pas, vous obtiendrez une exception <a class="reference internal" href="../exceptions.html#django.urls.NoReverseMatch" title="django.urls.NoReverseMatch"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NoReverseMatch</span></code></a>, ce qui provoquera l’affichage d’une page d’erreur sur votre site.</p>
<p>Si vous aimeriez récupérer une URL sans l’afficher, vous pouvez effectuer un appel un peu différent&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;some-url-name&#39;</span> <span class="nv">arg</span> <span class="nv">arg2</span> <span class="k">as</span> <span class="nv">the_url</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="p">&gt;</span>I&#39;m linking to <span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>La portée de la variable créée par la syntaxe <code class="docutils literal notranslate"><span class="pre">as</span> <span class="pre">var</span></code> est le <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></code> dans lequel la balise <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">%}</span></code> apparaît.</p>
<p>Cette syntaxe <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">...</span> <span class="pre">as</span> <span class="pre">var</span> <span class="pre">%}</span></code> ne produira <em>pas</em> d’erreur si la vue est manquante. En pratique, vous allez utiliser cela pour des liens vers des vues qui sont facultatives&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;some-url-name&#39;</span> <span class="k">as</span> <span class="nv">the_url</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">the_url</span> <span class="cp">%}</span>
  <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="p">&gt;</span>Link to optional stuff<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Si vous souhaitez récupérer une URL avec espace de noms, indiquez le nom entièrement qualifié&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;myapp:view-name&#39;</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>La <a class="reference internal" href="../../topics/http/urls.html#topics-http-reversing-url-namespaces"><span class="std std-ref">stratégie habituelle de résolution d’URL avec espace de noms</span></a> sera respectée, y compris l’usage d’indicateurs fournis par le contexte comme l’application actuelle.</p>
<div class="admonition warning">
<p class="first admonition-title">Avertissement</p>
<p class="last">N’oubliez pas d’entourer le motif d’URL <code class="docutils literal notranslate"><span class="pre">name</span></code> par des guillemets, sinon la valeur sera interprétée comme une variable de contexte&nbsp;!</p>
</div>
</div>
<div class="section" id="s-verbatim">
<span id="s-std:templatetag-verbatim"></span><span id="verbatim"></span><span id="std:templatetag-verbatim"></span><h3><code class="docutils literal notranslate"><span class="pre">verbatim</span></code><a class="headerlink" href="#verbatim" title="Lien permanent vers ce titre">¶</a></h3>
<p>Empêche le moteur de gabarits d’effectuer le rendu du contenu de cette balise de bloc.</p>
<p>C’est souvent employé pour permettre une couche de gabarit JavaScript qui entre en conflit avec la syntaxe Django. Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">verbatim</span> <span class="cp">%}</span>
    <span class="cp">{{</span><span class="k">if</span> <span class="nv">dying</span><span class="cp">}}</span>Still alive.<span class="cp">{{</span><span class="o">/</span><span class="k">if</span><span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endverbatim</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Vous pouvez aussi désigner une balise de fermeture spécifique, permettant l’utilisation de <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">endverbatim</span> <span class="pre">%}</span></code> comme faisant partie du contenu non rendu&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">verbatim</span> <span class="nv">myblock</span> <span class="cp">%}</span>
    Avoid template rendering via the <span class="cp">{%</span> <span class="k">verbatim</span> <span class="cp">%}{%</span> <span class="k">endverbatim</span> <span class="cp">%}</span> block.
<span class="cp">{%</span> <span class="k">endverbatim</span> <span class="nv">myblock</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-widthratio">
<span id="s-std:templatetag-widthratio"></span><span id="widthratio"></span><span id="std:templatetag-widthratio"></span><h3><code class="docutils literal notranslate"><span class="pre">widthratio</span></code><a class="headerlink" href="#widthratio" title="Lien permanent vers ce titre">¶</a></h3>
<p>Pour la création de graphiques en barres et autres usages similaires, cette balise calcule le rapport entre une valeur donnée et une valeur maximum, puis applique ce rapport à une constante.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;bar.png&quot;</span> <span class="na">alt</span><span class="o">=</span><span class="s">&quot;Bar&quot;</span>
     <span class="na">height</span><span class="o">=</span><span class="s">&quot;10&quot;</span> <span class="na">width</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">widthratio</span> <span class="nv">this_value</span> <span class="nv">max_value</span> <span class="nv">max_width</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">this_value</span></code> vaut 175, <code class="docutils literal notranslate"><span class="pre">max_value</span></code> 200 et <code class="docutils literal notranslate"><span class="pre">max_width</span></code> 100, la largeur de l’image de l’exemple ci-dessus sera de 88 pixels (parce que 175/200 = 0,875&nbsp;; 0,875 * 100 = 87,5 qui est arrondi à 88).</p>
<p>Dans certains cas, il peut être souhaitable de capturer le résultat de <code class="docutils literal notranslate"><span class="pre">widthratio</span></code> dans une variable. Cela peut par exemple être utile dans une balise <a class="reference internal" href="../../topics/i18n/translation.html#std:templatetag-blocktranslate"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">blocktranslate</span></code></a>, comme ceci&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">widthratio</span> <span class="nv">this_value</span> <span class="nv">max_value</span> <span class="nv">max_width</span> <span class="k">as</span> <span class="nv">width</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">blocktranslate</span> <span class="cp">%}</span>The width is: <span class="cp">{{</span> <span class="nv">width</span> <span class="cp">}}{%</span> <span class="k">endblocktranslate</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-with">
<span id="s-std:templatetag-with"></span><span id="with"></span><span id="std:templatetag-with"></span><h3><code class="docutils literal notranslate"><span class="pre">with</span></code><a class="headerlink" href="#with" title="Lien permanent vers ce titre">¶</a></h3>
<p>Place le résultat d’une expression complexe sous un nom plus simple. C’est utile lors de l’accès multiple à une méthode coûteuse (par ex. avec accès à la base de données).</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">with</span> <span class="nv">total</span><span class="o">=</span><span class="nv">business.employees.count</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">total</span> <span class="cp">}}</span> employee<span class="cp">{{</span> <span class="nv">total</span><span class="o">|</span><span class="nf">pluralize</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>La variable créée (dans l’exemple ci-dessus, <code class="docutils literal notranslate"><span class="pre">total</span></code>) est uniquement disponible entre les balises <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">with</span> <span class="pre">%}</span></code> et <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">endwith</span> <span class="pre">%}</span></code>.</p>
<p>Vous pouvez créer plus d’une variable de contexte&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">with</span> <span class="nv">alpha</span><span class="o">=</span><span class="m">1</span> <span class="nv">beta</span><span class="o">=</span><span class="m">2</span> <span class="cp">%}</span>
    ...
<span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">L’ancien format plus verbeux est toujours pris en charge&nbsp;: <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">with</span> <span class="pre">business.employees.count</span> <span class="pre">as</span> <span class="pre">total</span> <span class="pre">%}</span></code></p>
</div>
</div>
</div>
<div class="section" id="s-built-in-filter-reference">
<span id="s-ref-templates-builtins-filters"></span><span id="built-in-filter-reference"></span><span id="ref-templates-builtins-filters"></span><h2>Référence des filtres intégrés<a class="headerlink" href="#built-in-filter-reference" title="Lien permanent vers ce titre">¶</a></h2>
<div class="section" id="s-add">
<span id="s-std:templatefilter-add"></span><span id="add"></span><span id="std:templatefilter-add"></span><h3><code class="docutils literal notranslate"><span class="pre">add</span></code><a class="headerlink" href="#add" title="Lien permanent vers ce titre">¶</a></h3>
<p>Ajoute le paramètre à la valeur.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">add</span><span class="s2">:&quot;2&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">4</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">6</span></code>.</p>
<p>Ce filtre essaie d’abord de forcer les deux valeurs à des nombres entiers. Si cela échoue, il essaie tout de même d’additionner les deux valeurs. Cela fonctionne avec certains types de données (chaînes, listes, etc.) et échoue avec d’autres. Si cette deuxième tentative échoue, le résultat sera une chaîne vide.</p>
<p>Par exemple, si nous avons&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">first</span><span class="o">|</span><span class="nf">add</span><span class="o">:</span><span class="nv">second</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>et que <code class="docutils literal notranslate"><span class="pre">first</span></code> vaut <code class="docutils literal notranslate"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code> et <code class="docutils literal notranslate"><span class="pre">second</span></code> vaut <code class="docutils literal notranslate"><span class="pre">[4,</span> <span class="pre">5,</span> <span class="pre">6]</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3,</span> <span class="pre">4,</span> <span class="pre">5,</span> <span class="pre">6]</span></code>.</p>
<div class="admonition warning">
<p class="first admonition-title">Avertissement</p>
<p class="last">Les chaînes pouvant être forcées en nombre entier seront <strong>additionnées</strong> et non pas concaténées, comme dans le premier exemple ci-dessus.</p>
</div>
</div>
<div class="section" id="s-addslashes">
<span id="s-std:templatefilter-addslashes"></span><span id="addslashes"></span><span id="std:templatefilter-addslashes"></span><h3><code class="docutils literal notranslate"><span class="pre">addslashes</span></code><a class="headerlink" href="#addslashes" title="Lien permanent vers ce titre">¶</a></h3>
<p>Ajoute des barres obliques inverses avant les guillemets. Utile pour échapper des chaînes en CSV, par exemple.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">addslashes</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;J'utilise</span> <span class="pre">Django&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;J\'utilise</span> <span class="pre">Django&quot;</span></code>.</p>
</div>
<div class="section" id="s-capfirst">
<span id="s-std:templatefilter-capfirst"></span><span id="capfirst"></span><span id="std:templatefilter-capfirst"></span><h3><code class="docutils literal notranslate"><span class="pre">capfirst</span></code><a class="headerlink" href="#capfirst" title="Lien permanent vers ce titre">¶</a></h3>
<p>Met en majuscule le premier caractère de la valeur. Si le premier caractère n’est pas une lettre, ce filtre n’a aucun effet.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">capfirst</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;django&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;Django&quot;</span></code>.</p>
</div>
<div class="section" id="s-center">
<span id="s-std:templatefilter-center"></span><span id="center"></span><span id="std:templatefilter-center"></span><h3><code class="docutils literal notranslate"><span class="pre">center</span></code><a class="headerlink" href="#center" title="Lien permanent vers ce titre">¶</a></h3>
<p>Centre la valeur dans un champ de largeur donnée.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>&quot;<span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">center</span><span class="s2">:&quot;15&quot;</span> <span class="cp">}}</span>&quot;
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Django&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;</span>&#160;&#160;&#160;&#160; <span class="pre">Django</span>&#160;&#160;&#160; <span class="pre">&quot;</span></code>.</p>
</div>
<div class="section" id="s-cut">
<span id="s-std:templatefilter-cut"></span><span id="cut"></span><span id="std:templatefilter-cut"></span><h3><code class="docutils literal notranslate"><span class="pre">cut</span></code><a class="headerlink" href="#cut" title="Lien permanent vers ce titre">¶</a></h3>
<p>Enlève toutes les valeurs en paramètre de la chaîne à filtrer.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">cut</span><span class="s2">:&quot; &quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Chaîne</span> <span class="pre">avec</span> <span class="pre">des</span> <span class="pre">espaces&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;Chaîneavecdesespaces&quot;</span></code>.</p>
</div>
<div class="section" id="s-date">
<span id="s-std:templatefilter-date"></span><span id="date"></span><span id="std:templatefilter-date"></span><h3><code class="docutils literal notranslate"><span class="pre">date</span></code><a class="headerlink" href="#date" title="Lien permanent vers ce titre">¶</a></h3>
<p>Met en forme une date selon le format indiqué.</p>
<p>Utilise un format semblable à la fonction <code class="docutils literal notranslate"><span class="pre">date()</span></code> de PHP (<a class="reference external" href="https://php.net/date">https://php.net/date</a>) avec certaines différences.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Ces caractères de format ne sont pas utilisés dans Django en dehors des gabarits. Ils ont été conçus pour être compatibles avec PHP afin de faciliter la transition aux rédacteurs habitués à ce langage.</p>
</div>
<p id="date-and-time-formatting-specifiers">Chaînes de format disponibles&nbsp;:</p>
<table class="docutils">
<colgroup>
<col width="12%" />
<col width="31%" />
<col width="57%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Caractère de format</th>
<th class="head">Description</th>
<th class="head">Exemple d’affichage</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><strong>Jour</strong></td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">d</span></code></td>
<td>Jour du mois, 2 chiffres complétés si nécessaire par un zéro.</td>
<td><code class="docutils literal notranslate"><span class="pre">'01'</span></code> à <code class="docutils literal notranslate"><span class="pre">'31'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">j</span></code></td>
<td>Jour du mois sans zéros initiaux.</td>
<td><code class="docutils literal notranslate"><span class="pre">'1'</span></code> à <code class="docutils literal notranslate"><span class="pre">'31'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">D</span></code></td>
<td>Jour de la semaine, abréviation de 3 caractères.</td>
<td><code class="docutils literal notranslate"><span class="pre">'ven'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">l</span></code></td>
<td>Jour de la semaine, texte complet.</td>
<td><code class="docutils literal notranslate"><span class="pre">'vendredi'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">S</span></code></td>
<td>Suffixe ordinal anglais pour le jour du mois, sur 2 caractères.</td>
<td><code class="docutils literal notranslate"><span class="pre">'st'</span></code>, <code class="docutils literal notranslate"><span class="pre">'nd'</span></code>, <code class="docutils literal notranslate"><span class="pre">'rd'</span></code> ou <code class="docutils literal notranslate"><span class="pre">'th'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">w</span></code></td>
<td>Jour de la semaine, chiffre sans zéro initial.</td>
<td><code class="docutils literal notranslate"><span class="pre">'0'</span></code> (dimanche) à <code class="docutils literal notranslate"><span class="pre">'6'</span></code> (samedi)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">z</span></code></td>
<td>Jour de l’année.</td>
<td><code class="docutils literal notranslate"><span class="pre">1</span></code> à <code class="docutils literal notranslate"><span class="pre">366</span></code></td>
</tr>
<tr class="row-even"><td><strong>Semaine</strong></td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">W</span></code></td>
<td>Numéro de semaine dans l’année selon ISO-8601, les semaines commençant le lundi.</td>
<td><code class="docutils literal notranslate"><span class="pre">1</span></code>, <code class="docutils literal notranslate"><span class="pre">53</span></code></td>
</tr>
<tr class="row-even"><td><strong>Mois</strong></td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">m</span></code></td>
<td>Mois, 2 chiffres, avec zéro initial si nécessaire.</td>
<td><code class="docutils literal notranslate"><span class="pre">'01'</span></code> à <code class="docutils literal notranslate"><span class="pre">'12'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">n</span></code></td>
<td>Mois (chiffres) sans zéro initial.</td>
<td><code class="docutils literal notranslate"><span class="pre">'1'</span></code> à <code class="docutils literal notranslate"><span class="pre">'12'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">M</span></code></td>
<td>Mois, abréviation de 3 caractères.</td>
<td><code class="docutils literal notranslate"><span class="pre">'jan'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">b</span></code></td>
<td>Mois, abréviation de 3 caractères en minuscules.</td>
<td><code class="docutils literal notranslate"><span class="pre">'jan'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">E</span></code></td>
<td>Mois, représentation alternative spécifique à la langue active, habituellement utilisée pour les dates au format étendu.</td>
<td><code class="docutils literal notranslate"><span class="pre">'listopada'</span></code> (pour le polonais, contrairement à <code class="docutils literal notranslate"><span class="pre">'Listopad'</span></code>)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">F</span></code></td>
<td>Mois au format texte long.</td>
<td><code class="docutils literal notranslate"><span class="pre">'janvier'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">N</span></code></td>
<td>Abréviation du mois dans le style Associated Press. Extension propriétaire.</td>
<td><code class="docutils literal notranslate"><span class="pre">'jan.'</span></code>, <code class="docutils literal notranslate"><span class="pre">'fév.'</span></code>, <code class="docutils literal notranslate"><span class="pre">'mars'</span></code>, <code class="docutils literal notranslate"><span class="pre">'mai'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">t</span></code></td>
<td>Nombre de jours du mois indiqué.</td>
<td><code class="docutils literal notranslate"><span class="pre">28</span></code> à <code class="docutils literal notranslate"><span class="pre">31</span></code></td>
</tr>
<tr class="row-odd"><td><strong>Année</strong></td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">y</span></code></td>
<td>Année sur 2 chiffres.</td>
<td><code class="docutils literal notranslate"><span class="pre">'99'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Y</span></code></td>
<td>Année sur 4 chiffres.</td>
<td><code class="docutils literal notranslate"><span class="pre">'1999'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">L</span></code></td>
<td>Valeur booléenne indiquant s’il s’agit d’une année bissextile.</td>
<td><code class="docutils literal notranslate"><span class="pre">True</span></code> ou <code class="docutils literal notranslate"><span class="pre">False</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">o</span></code></td>
<td>Année selon numération des semaines ISO-8601 (W) utilisant la semaine intercalaire. Voir Y pour le format d’année plus habituel.</td>
<td><code class="docutils literal notranslate"><span class="pre">'1999'</span></code></td>
</tr>
<tr class="row-even"><td><strong>Heure</strong></td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">g</span></code></td>
<td>Heure, format sur 12 heures sans zéros initiaux.</td>
<td><code class="docutils literal notranslate"><span class="pre">'1'</span></code> à <code class="docutils literal notranslate"><span class="pre">'12'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">G</span></code></td>
<td>Heure, format sur 24 heures sans zéros initiaux.</td>
<td><code class="docutils literal notranslate"><span class="pre">'0'</span></code> à <code class="docutils literal notranslate"><span class="pre">'23'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">h</span></code></td>
<td>Heure, format sur 12 heures.</td>
<td><code class="docutils literal notranslate"><span class="pre">'01'</span></code> à <code class="docutils literal notranslate"><span class="pre">'12'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">H</span></code></td>
<td>Heure, format sur 24 heures.</td>
<td><code class="docutils literal notranslate"><span class="pre">'00'</span></code> à <code class="docutils literal notranslate"><span class="pre">'23'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">i</span></code></td>
<td>Minutes.</td>
<td><code class="docutils literal notranslate"><span class="pre">'00'</span></code> à <code class="docutils literal notranslate"><span class="pre">'59'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">s</span></code></td>
<td>Secondes, 2 chiffres avec zéro initial.</td>
<td><code class="docutils literal notranslate"><span class="pre">'00'</span></code> à <code class="docutils literal notranslate"><span class="pre">'59'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">u</span></code></td>
<td>Microsecondes.</td>
<td><code class="docutils literal notranslate"><span class="pre">000000</span></code> à <code class="docutils literal notranslate"><span class="pre">999999</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">a</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">'a.m.'</span></code> ou <code class="docutils literal notranslate"><span class="pre">'p.m.'</span></code> (notez que c’est légèrement différent de l’affichage PHP, par la présence de points correspondant au style Associated Press).</td>
<td><code class="docutils literal notranslate"><span class="pre">'a.m.'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">A</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">'AM'</span></code> ou <code class="docutils literal notranslate"><span class="pre">'PM'</span></code>.</td>
<td><code class="docutils literal notranslate"><span class="pre">'AM'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">f</span></code></td>
<td>Heure, en format heures et minutes sur 12 heures, sans affichage des minutes si elles valent 0. Extension propriétaire.</td>
<td><code class="docutils literal notranslate"><span class="pre">'1'</span></code>, <code class="docutils literal notranslate"><span class="pre">'1:30'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">P</span></code></td>
<td>Heure, au format sur 12 heures, minutes et «&nbsp;a.m.&nbsp;»/«&nbsp;p.m.&nbsp;», sans les minutes si elles valent 0&nbsp;; le cas échéant, les chaînes «&nbsp;minuit&nbsp;» et «&nbsp;midi&nbsp;» apparaissent. Extension propriétaire.</td>
<td><code class="docutils literal notranslate"><span class="pre">'1</span> <span class="pre">a.m.'</span></code>, <code class="docutils literal notranslate"><span class="pre">'1:30</span> <span class="pre">p.m.'</span></code>, <code class="docutils literal notranslate"><span class="pre">'minuit'</span></code>, <code class="docutils literal notranslate"><span class="pre">'midi'</span></code>, <code class="docutils literal notranslate"><span class="pre">'12:30</span> <span class="pre">p.m.'</span></code></td>
</tr>
<tr class="row-even"><td><strong>Fuseau horaire</strong></td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">e</span></code></td>
<td>Nom de fuseau horaire. Différents formats peuvent apparaître, ou même une chaîne vide, en fonction de la date/heure.</td>
<td><code class="docutils literal notranslate"><span class="pre">''</span></code>, <code class="docutils literal notranslate"><span class="pre">'GMT'</span></code>, <code class="docutils literal notranslate"><span class="pre">'-500'</span></code>, <code class="docutils literal notranslate"><span class="pre">'US/Eastern'</span></code>, etc.</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">I</span></code></td>
<td>Heure d’été, indication si elle est active ou non.</td>
<td><code class="docutils literal notranslate"><span class="pre">'1'</span></code> ou <code class="docutils literal notranslate"><span class="pre">'0'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">O</span></code></td>
<td>Différence en heures avec le temps de Greenwich.</td>
<td><code class="docutils literal notranslate"><span class="pre">'+0200'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">T</span></code></td>
<td>Fuseau horaire de la machine.</td>
<td><code class="docutils literal notranslate"><span class="pre">'EST'</span></code>, <code class="docutils literal notranslate"><span class="pre">'MDT'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Z</span></code></td>
<td>Décalage de fuseau horaire en secondes. Le décalage pour les fuseaux horaires à l’ouest de UTC est toujours négatif, alors qu’il est positif pour les fuseaux à l’est de UTC.</td>
<td><code class="docutils literal notranslate"><span class="pre">-43200</span></code> à <code class="docutils literal notranslate"><span class="pre">43200</span></code></td>
</tr>
<tr class="row-even"><td><strong>Date/heure</strong></td>
<td>&#160;</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">c</span></code></td>
<td>ISO 8601 format. (Note: unlike other
formatters, such as «&nbsp;Z&nbsp;», «&nbsp;O&nbsp;» or «&nbsp;r&nbsp;»,
the «&nbsp;c&nbsp;» formatter will not add timezone
offset if value is a naive datetime
(see <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.tzinfo" title="(disponible dans Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.tzinfo</span></code></a>).</td>
<td><code class="docutils literal notranslate"><span class="pre">2008-01-02T10:30:00.000123+02:00</span></code> ou <code class="docutils literal notranslate"><span class="pre">2008-01-02T10:30:00.000123</span></code> si la date/heure est naïve</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">r</span></code></td>
<td>Date mise en forme selon <span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc5322.html#section-3.3"><strong>RFC 5322</strong></a>.</td>
<td><code class="docutils literal notranslate"><span class="pre">'ven,</span> <span class="pre">22</span> <span class="pre">Nov</span> <span class="pre">2013</span> <span class="pre">18:41:16</span> <span class="pre">+0100'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">U</span></code></td>
<td>Secondes depuis le temps Unix initial (1er janvier 1970 00:00:00 UTC).</td>
<td>&#160;</td>
</tr>
</tbody>
</table>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;D d M Y&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> est un objet <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(disponible dans Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> (par ex. le résultat de <code class="docutils literal notranslate"><span class="pre">datetime.datetime.now()</span></code>), le résultat sera le texte <code class="docutils literal notranslate"><span class="pre">'mer</span> <span class="pre">09</span> <span class="pre">jan</span> <span class="pre">2008'</span></code>.</p>
<p>Le format indiqué peut correspondre à l’un des formats prédéfinis <a class="reference internal" href="../settings.html#std:setting-DATE_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DATE_FORMAT</span></code></a>, <a class="reference internal" href="../settings.html#std:setting-DATETIME_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DATETIME_FORMAT</span></code></a>, <a class="reference internal" href="../settings.html#std:setting-SHORT_DATE_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">SHORT_DATE_FORMAT</span></code></a> ou <a class="reference internal" href="../settings.html#std:setting-SHORT_DATETIME_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">SHORT_DATETIME_FORMAT</span></code></a>, ou un format personnalisé utilisant les spécificateurs de format décrits dans le tableau ci-dessus. Notez que les formats prédéfinis peuvent varier en fonction de la langue active.</p>
<p>Dans le cas où <a class="reference internal" href="../settings.html#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span></code></a> vaut <code class="docutils literal notranslate"><span class="pre">True</span></code> et que <a class="reference internal" href="../settings.html#std:setting-LANGUAGE_CODE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGE_CODE</span></code></a> est, par exemple, <code class="docutils literal notranslate"><span class="pre">&quot;es&quot;</span></code>, alors pour&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;SHORT_DATE_FORMAT&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>le résultat sera le texte <code class="docutils literal notranslate"><span class="pre">&quot;09/01/2008&quot;</span></code> (le format <code class="docutils literal notranslate"><span class="pre">&quot;SHORT_DATE_FORMAT&quot;</span></code> de la locale <code class="docutils literal notranslate"><span class="pre">es</span></code> fourni avec Django est <code class="docutils literal notranslate"><span class="pre">&quot;d/m/Y&quot;</span></code>).</p>
<p>Lorsqu’aucune chaîne de format n’est indiquée, c’est le format <code class="docutils literal notranslate"><span class="pre">DATE_FORMAT</span></code> qui est utilisé. Avec des réglages semblables à l’exemple précédent&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>produit <code class="docutils literal notranslate"><span class="pre">9</span> <span class="pre">de</span> <span class="pre">Enero</span> <span class="pre">de</span> <span class="pre">2008</span></code> (le format <code class="docutils literal notranslate"><span class="pre">DATE_FORMAT</span></code> de la locale <code class="docutils literal notranslate"><span class="pre">es</span></code> est <code class="docutils literal notranslate"><span class="pre">r'j</span> <span class="pre">\d\e</span> <span class="pre">F</span> <span class="pre">\d\e</span> <span class="pre">Y'</span></code>). « d » et « e » sont tous deux échappés par la barre oblique inverse, car sinon ils seraient considérés comme des marqueurs de format affichant respectivement le jour et le nom du fuseau horaire.</p>
<p>Vous pouvez combiner <code class="docutils literal notranslate"><span class="pre">date</span></code> avec le filtre <a class="reference internal" href="#std:templatefilter-time"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">time</span></code></a> afin de produire une représentation complète d’une valeur <code class="docutils literal notranslate"><span class="pre">datetime</span></code>, par ex.&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;D d M Y&quot;</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:&quot;H:i&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-default">
<span id="s-std:templatefilter-default"></span><span id="default"></span><span id="std:templatefilter-default"></span><h3><code class="docutils literal notranslate"><span class="pre">default</span></code><a class="headerlink" href="#default" title="Lien permanent vers ce titre">¶</a></h3>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> est évaluée à <code class="docutils literal notranslate"><span class="pre">False</span></code>, le paramètre de <code class="docutils literal notranslate"><span class="pre">default</span></code> est affiché. Sinon, c’est la valeur <code class="docutils literal notranslate"><span class="pre">value</span></code> qui est affichée.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><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>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;&quot;</span></code> (la chaîne vide), le résultat sera <code class="docutils literal notranslate"><span class="pre">nothing</span></code>.</p>
</div>
<div class="section" id="s-default-if-none">
<span id="s-std:templatefilter-default_if_none"></span><span id="default-if-none"></span><span id="std:templatefilter-default_if_none"></span><h3><code class="docutils literal notranslate"><span class="pre">default_if_none</span></code><a class="headerlink" href="#default-if-none" title="Lien permanent vers ce titre">¶</a></h3>
<p>Si (et seulement si) <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">None</span></code>,  le paramètre de <code class="docutils literal notranslate"><span class="pre">default</span></code> est affiché. Sinon, c’est la valeur <code class="docutils literal notranslate"><span class="pre">value</span></code> qui est affichée.</p>
<p>Notez que si une chaîne vide est transmise, la valeur par défaut ne sera <em>pas</em> utilisée. Utilisez le filtre <a class="reference internal" href="#std:templatefilter-default"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">default</span></code></a> si vous voulez que la valeur par défaut soit aussi utilisée pour les chaînes vides.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">default_if_none</span><span class="s2">:&quot;nothing&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">None</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">nothing</span></code>.</p>
</div>
<div class="section" id="s-dictsort">
<span id="s-std:templatefilter-dictsort"></span><span id="dictsort"></span><span id="std:templatefilter-dictsort"></span><h3><code class="docutils literal notranslate"><span class="pre">dictsort</span></code><a class="headerlink" href="#dictsort" title="Lien permanent vers ce titre">¶</a></h3>
<p>Accepte une liste de dictionnaires et renvoie cette liste triée par la clé donnée en paramètre.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:&quot;name&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient&nbsp;:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;zed&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">:</span> <span class="mi">19</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;amy&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">:</span> <span class="mi">22</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;joe&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">:</span> <span class="mi">31</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>le résultat sera&nbsp;:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;amy&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">:</span> <span class="mi">22</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;joe&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">:</span> <span class="mi">31</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;zed&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">:</span> <span class="mi">19</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Vous pouvez également faire des choses plus compliquées comme&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">book</span> <span class="k">in</span> <span class="nv">books</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:&quot;author.age&quot;</span> <span class="cp">%}</span>
    * <span class="cp">{{</span> <span class="nv">book.title</span> <span class="cp">}}</span> (<span class="cp">{{</span> <span class="nv">book.author.name</span> <span class="cp">}}</span>)
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">books</span></code> est&nbsp;:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="p">{</span><span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="s1">&#39;1984&#39;</span><span class="p">,</span> <span class="s1">&#39;author&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;George&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">:</span> <span class="mi">45</span><span class="p">}},</span>
    <span class="p">{</span><span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="s1">&#39;Timequake&#39;</span><span class="p">,</span> <span class="s1">&#39;author&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Kurt&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">:</span> <span class="mi">75</span><span class="p">}},</span>
    <span class="p">{</span><span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="s1">&#39;Alice&#39;</span><span class="p">,</span> <span class="s1">&#39;author&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Lewis&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">:</span> <span class="mi">33</span><span class="p">}},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>alors le résultat sera&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>* Alice (Lewis)
* 1984 (George)
* Timequake (Kurt)
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">dictsort</span></code> peut aussi trier une liste de listes (ou tout autre objet implémentant <code class="docutils literal notranslate"><span class="pre">__getitem__()</span></code>) par des éléments à un indice donné. Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">dictsort</span><span class="o">:</span><span class="m">0</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient&nbsp;:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;42&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;string&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>le résultat sera&nbsp;:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;42&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;string&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Vous devez passer l’indice comme nombre entier et pas comme chaîne. Le code suivant produit un résultat vide&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">values</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:&quot;0&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-dictsortreversed">
<span id="s-std:templatefilter-dictsortreversed"></span><span id="dictsortreversed"></span><span id="std:templatefilter-dictsortreversed"></span><h3><code class="docutils literal notranslate"><span class="pre">dictsortreversed</span></code><a class="headerlink" href="#dictsortreversed" title="Lien permanent vers ce titre">¶</a></h3>
<p>Accepte une liste de dictionnaires et renvoie cette liste triée par la clé donnée en paramètre dans l’ordre inverse. Le fonctionnement est le même que le filtre ci-dessus excepté que la valeur renvoyée sera triée dans l’ordre inverse.</p>
</div>
<div class="section" id="s-divisibleby">
<span id="s-std:templatefilter-divisibleby"></span><span id="divisibleby"></span><span id="std:templatefilter-divisibleby"></span><h3><code class="docutils literal notranslate"><span class="pre">divisibleby</span></code><a class="headerlink" href="#divisibleby" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie <code class="docutils literal notranslate"><span class="pre">True</span></code> si la valeur est divisible par le paramètre.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">divisibleby</span><span class="s2">:&quot;3&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">21</span></code>, le résultat sera</p>
</div>
<div class="section" id="s-escape">
<span id="s-std:templatefilter-escape"></span><span id="escape"></span><span id="std:templatefilter-escape"></span><h3><code class="docutils literal notranslate"><span class="pre">escape</span></code><a class="headerlink" href="#escape" title="Lien permanent vers ce titre">¶</a></h3>
<p>Échappe du code HTML. Plus particulièrement, il effectue les remplacements suivants&nbsp;:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">&lt;</span></code> est converti en <code class="docutils literal notranslate"><span class="pre">&amp;lt;</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">&gt;</span></code> est converti en <code class="docutils literal notranslate"><span class="pre">&amp;gt;</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'</span></code> (apostrophe) est converti en <code class="docutils literal notranslate"><span class="pre">&amp;#x27;</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">&quot;</span></code> (guillemet) est converti en <code class="docutils literal notranslate"><span class="pre">&amp;quot;</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">&amp;</span></code> est converti en <code class="docutils literal notranslate"><span class="pre">&amp;amp;</span></code></li>
</ul>
<p>L’application de <code class="docutils literal notranslate"><span class="pre">escape</span></code> à une variable qui doit normalement subir l’échappement automatique de son résultat ne fera qu’effectuer un contrôle d’échappement supplémentaire. On peut donc utiliser cette fonction sans souci même dans un contexte d’échappement automatique. Si vous souhaitez forcer l’échappement réel dans tous les cas, utilisez le filtre  <a class="reference internal" href="#std:templatefilter-force_escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">force_escape</span></code></a>.</p>
<p>Par exemple, vous pouvez appliquer <code class="docutils literal notranslate"><span class="pre">escape</span></code> à certains champs lorsque <a class="reference internal" href="#std:templatetag-autoescape"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">autoescape</span></code></a> n’est pas actif&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">off</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">title</span><span class="o">|</span><span class="nf">escape</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-escapejs">
<span id="s-std:templatefilter-escapejs"></span><span id="escapejs"></span><span id="std:templatefilter-escapejs"></span><h3><code class="docutils literal notranslate"><span class="pre">escapejs</span></code><a class="headerlink" href="#escapejs" title="Lien permanent vers ce titre">¶</a></h3>
<p>Échappe des caractères dans du contenu JavaScript. Cela ne rend <em>pas</em> le texte fiable pour être utilisé dans du HTML ou des gabarits de littéraux JavaScript, mais cela protège des erreurs de syntaxe lors de l’utilisation de gabarits pour générer du JavaScript/JSON.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">escapejs</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;testing\r\njavascript</span> <span class="pre">'string\&quot;</span> <span class="pre">&lt;b&gt;escaping&lt;/b&gt;&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;testing\\u000D\\u000Ajavascript</span> <span class="pre">\\u0027string\\u0022</span> <span class="pre">\\u003Cb\\u003Eescaping\\u003C/b\\u003E&quot;</span></code>.</p>
</div>
<div class="section" id="s-filesizeformat">
<span id="s-std:templatefilter-filesizeformat"></span><span id="filesizeformat"></span><span id="std:templatefilter-filesizeformat"></span><h3><code class="docutils literal notranslate"><span class="pre">filesizeformat</span></code><a class="headerlink" href="#filesizeformat" title="Lien permanent vers ce titre">¶</a></h3>
<p>Met en forme la valeur sous forme de taille de fichier humainement lisible (par ex. <code class="docutils literal notranslate"><span class="pre">'13</span> <span class="pre">Kio'</span></code>, <code class="docutils literal notranslate"><span class="pre">'4.1</span> <span class="pre">Mio'</span></code>, <code class="docutils literal notranslate"><span class="pre">'102</span> <span class="pre">octets'</span></code>, etc).</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">filesizeformat</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>SI <code class="docutils literal notranslate"><span class="pre">value</span></code> contient 123456789, le résultat sera <code class="docutils literal notranslate"><span class="pre">117.7</span> <span class="pre">Mio</span></code>.</p>
<div class="admonition-file-sizes-and-si-units admonition">
<p class="first admonition-title">Tailles de fichiers et unités SI</p>
<p class="last">Strictement parlant, <code class="docutils literal notranslate"><span class="pre">filesizeformat</span></code> ne se conforme pas au Système international d’unités qui recommande l’usage de KiB, MiB, GiB, etc. lorsque la taille d’octet est calculée en puissances de 1024 (ce qui est le cas ici). Django utilise plutôt les noms d’unités traditionnelles (KB, MB, GB, etc.) correspondant aux noms qui sont utilisés plus couramment (NdT&nbsp;: la traduction en français utilise les préfixes SI Kio, Mio, etc.).</p>
</div>
</div>
<div class="section" id="s-first">
<span id="s-std:templatefilter-first"></span><span id="first"></span><span id="std:templatefilter-first"></span><h3><code class="docutils literal notranslate"><span class="pre">first</span></code><a class="headerlink" href="#first" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie le premier élément de la liste.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">first</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient la liste <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">'a'</span></code>.</p>
</div>
<div class="section" id="s-floatformat">
<span id="s-std:templatefilter-floatformat"></span><span id="floatformat"></span><span id="std:templatefilter-floatformat"></span><h3><code class="docutils literal notranslate"><span class="pre">floatformat</span></code><a class="headerlink" href="#floatformat" title="Lien permanent vers ce titre">¶</a></h3>
<p>Quand le paramètre est absent, ce filtre arrondit un nombre à virgule flottante à un chiffre après la virgule, mais seulement s’il y a une partie décimale à afficher. Par exemple&nbsp;:</p>
<table class="docutils">
<colgroup>
<col width="26%" />
<col width="57%" />
<col width="17%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><code class="docutils literal notranslate"><span class="pre">valeur</span></code></th>
<th class="head">Gabarit</th>
<th class="head">Affichage</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34,2</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.26000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34,3</span></code></td>
</tr>
</tbody>
</table>
<p>Quand un paramètre nombre entier est transmis, <code class="docutils literal notranslate"><span class="pre">floatformat</span></code> arrondit le nombre au nombre de chiffres après la virgule indiqué par le paramètre. Par exemple&nbsp;:</p>
<table class="docutils">
<colgroup>
<col width="24%" />
<col width="57%" />
<col width="20%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><code class="docutils literal notranslate"><span class="pre">valeur</span></code></th>
<th class="head">Gabarit</th>
<th class="head">Affichage</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:3</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34,232</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:3</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34,000</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.26000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:3</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34,260</span></code></td>
</tr>
</tbody>
</table>
<p>Il peut être particulièrement utile de transmettre le paramètre 0 (zéro) ce qui arrondit le nombre à virgule au nombre entier le plus proche.</p>
<table class="docutils">
<colgroup>
<col width="22%" />
<col width="59%" />
<col width="19%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><code class="docutils literal notranslate"><span class="pre">valeur</span></code></th>
<th class="head">Gabarit</th>
<th class="head">Affichage</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;0&quot;</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;0&quot;</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">39.56000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;0&quot;</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">40</span></code></td>
</tr>
</tbody>
</table>
<p>Si le paramètre passé à <code class="docutils literal notranslate"><span class="pre">floatformat</span></code> est négatif, le nombre sera arrondi au nombre de chiffres après la virgule indiqué, mais seulement s’il y a une partie décimale à afficher. Par exemple&nbsp;:</p>
<table class="docutils">
<colgroup>
<col width="22%" />
<col width="59%" />
<col width="19%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><code class="docutils literal notranslate"><span class="pre">valeur</span></code></th>
<th class="head">Gabarit</th>
<th class="head">Affichage</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;-3&quot;</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34,232</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;-3&quot;</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.26000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;-3&quot;</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34,260</span></code></td>
</tr>
</tbody>
</table>
<p>L’utilisation de <code class="docutils literal notranslate"><span class="pre">floatformat</span></code> sans paramètre est équivalent à l’utilisation de <code class="docutils literal notranslate"><span class="pre">floatformat</span></code> avec le paramètre <code class="docutils literal notranslate"><span class="pre">-1</span></code>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 3.1:</span> <p>Dans les anciennes versions, un zéro négatif <code class="docutils literal notranslate"><span class="pre">-0</span></code> était renvoyé pour les nombres négatifs arrondis à 0.</p>
</div>
</div>
<div class="section" id="s-force-escape">
<span id="s-std:templatefilter-force_escape"></span><span id="force-escape"></span><span id="std:templatefilter-force_escape"></span><h3><code class="docutils literal notranslate"><span class="pre">force_escape</span></code><a class="headerlink" href="#force-escape" title="Lien permanent vers ce titre">¶</a></h3>
<p>Applique l’échappement HTML à une chaîne de caractères (voir le filtre <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> pour les détails). Ce filtre est appliqué <em>immédiatement</em> et renvoie une nouvelle chaîne échappée. Cela peut être utile dans les rares cas où vous avez besoin de plusieurs échappements successifs ou que vous vouliez appliquer d’autres filtres au résultat échappé. En temps normal, l’emploi du filtre <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> doit suffire.</p>
<p>Par exemple, si vous souhaitez échapper les balises HTML <code class="docutils literal notranslate"><span class="pre">&lt;p&gt;</span></code> produites par le filtre <a class="reference internal" href="#std:templatefilter-linebreaks"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">linebreaks</span></code></a>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">off</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">body</span><span class="o">|</span><span class="nf">linebreaks</span><span class="o">|</span><span class="nf">force_escape</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-get-digit">
<span id="s-std:templatefilter-get_digit"></span><span id="get-digit"></span><span id="std:templatefilter-get_digit"></span><h3><code class="docutils literal notranslate"><span class="pre">get_digit</span></code><a class="headerlink" href="#get-digit" title="Lien permanent vers ce titre">¶</a></h3>
<p>Étant donné un nombre entier, renvoie le chiffre indiqué, où 1 est le chiffre le plus à droite, 2 le deuxième plus à droite, etc. Renvoie la valeur originale si les entrées ne sont pas valables (si la valeur ou le paramètre ne sont pas des nombres entiers ou si le paramètre est plus petit que 1). Sinon, le résultat est toujours un nombre entier.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">get_digit</span><span class="s2">:&quot;2&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>SI <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">123456789</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">8</span></code>.</p>
</div>
<div class="section" id="s-iriencode">
<span id="s-std:templatefilter-iriencode"></span><span id="iriencode"></span><span id="std:templatefilter-iriencode"></span><h3><code class="docutils literal notranslate"><span class="pre">iriencode</span></code><a class="headerlink" href="#iriencode" title="Lien permanent vers ce titre">¶</a></h3>
<p>Convertit un IRI (Internationalized Resource Identifier) en une chaîne pouvant être incluse dans une URL. C’est nécessaire quand vous essayez d’insérer des chaînes contenant des caractères non ASCII dans des URL.</p>
<p>Vous pouvez sans problème utiliser ce filtre sur une chaîne ayant déjà passé par le filtre <a class="reference internal" href="#std:templatefilter-urlencode"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">urlencode</span></code></a>.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">iriencode</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;?test=1&amp;moi=2&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;?test=1&amp;amp;moi=2&quot;</span></code>.</p>
</div>
<div class="section" id="s-join">
<span id="s-std:templatefilter-join"></span><span id="join"></span><span id="std:templatefilter-join"></span><h3><code class="docutils literal notranslate"><span class="pre">join</span></code><a class="headerlink" href="#join" title="Lien permanent vers ce titre">¶</a></h3>
<p>Concatène les éléments d’une liste avec une chaîne de liaison, comme Python le fait avec <code class="docutils literal notranslate"><span class="pre">str.join(list)</span></code>.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">join</span><span class="s2">:&quot; // &quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient la liste <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;a</span> <span class="pre">//</span> <span class="pre">b</span> <span class="pre">//</span> <span class="pre">c&quot;</span></code>.</p>
</div>
<div class="section" id="s-json-script">
<span id="s-std:templatefilter-json_script"></span><span id="json-script"></span><span id="std:templatefilter-json_script"></span><h3><code class="docutils literal notranslate"><span class="pre">json_script</span></code><a class="headerlink" href="#json-script" title="Lien permanent vers ce titre">¶</a></h3>
<p>Transforme un objet Python en JSON de manière sûre, enveloppé dans une balise <code class="docutils literal notranslate"><span class="pre">&lt;script&gt;</span></code>, pour utilisation à partir de JavaScript.</p>
<p><strong>Paramètre</strong> : « id » HTML de la balise <code class="docutils literal notranslate"><span class="pre">&lt;script&gt;</span></code>.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">json_script</span><span class="s2">:&quot;hello-data&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient le dictionnaire <code class="docutils literal notranslate"><span class="pre">{'hello':</span> <span class="pre">'world'}</span></code>, le résultat sera :</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">script</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;hello-data&quot;</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;application/json&quot;</span><span class="p">&gt;{</span><span class="s2">&quot;hello&quot;</span><span class="o">:</span> <span class="s2">&quot;world&quot;</span><span class="p">}&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>On peut accéder aux données résultantes en JavaScript comme ceci :</p>
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="kd">const</span> <span class="nx">value</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;hello-data&#39;</span><span class="p">).</span><span class="nx">textContent</span><span class="p">);</span>
</pre></div>
</div>
<p>Les attaques XSS sont évitées en échappant les caractères « &lt; », « &gt; » et « &amp; ». Par exemple, si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">{'hello':</span> <span class="pre">'world&lt;/script&gt;&amp;amp;'}</span></code>, le résultat sera :</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">script</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;hello-data&quot;</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;application/json&quot;</span><span class="p">&gt;{</span><span class="s2">&quot;hello&quot;</span><span class="o">:</span> <span class="s2">&quot;world\\u003C/script\\u003E\\u0026amp;&quot;</span><span class="p">}&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Ceci est compatible avec une politique Content Security stricte qui interdit l’exécution de script dans les pages. Cela maintient également une séparation propre entre des données passives et du code exécutable.</p>
</div>
<div class="section" id="s-last">
<span id="s-std:templatefilter-last"></span><span id="last"></span><span id="std:templatefilter-last"></span><h3><code class="docutils literal notranslate"><span class="pre">last</span></code><a class="headerlink" href="#last" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie le dernier élément de la liste.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">last</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient la liste <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>, le résultat sera <code class="docutils literal notranslate"><span class="pre">'d'</span></code>.</p>
</div>
<div class="section" id="s-length">
<span id="s-std:templatefilter-length"></span><span id="length"></span><span id="std:templatefilter-length"></span><h3><code class="docutils literal notranslate"><span class="pre">length</span></code><a class="headerlink" href="#length" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie la longueur de la valeur. Cela fonctionne aussi bien pour du texte que des listes.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><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>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <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> ou <code class="docutils literal notranslate"><span class="pre">&quot;abcd&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">4</span></code>.</p>
<p>Le filtre renvoie <code class="docutils literal notranslate"><span class="pre">0</span></code> pour une variable non définie.</p>
</div>
<div class="section" id="s-length-is">
<span id="s-std:templatefilter-length_is"></span><span id="length-is"></span><span id="std:templatefilter-length_is"></span><h3><code class="docutils literal notranslate"><span class="pre">length_is</span></code><a class="headerlink" href="#length-is" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie <code class="docutils literal notranslate"><span class="pre">True</span></code> si la longueur de la valeur correspond au paramètre, sinon <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">length_is</span><span class="s2">:&quot;4&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <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> ou <code class="docutils literal notranslate"><span class="pre">&quot;abcd&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
</div>
<div class="section" id="s-linebreaks">
<span id="s-std:templatefilter-linebreaks"></span><span id="linebreaks"></span><span id="std:templatefilter-linebreaks"></span><h3><code class="docutils literal notranslate"><span class="pre">linebreaks</span></code><a class="headerlink" href="#linebreaks" title="Lien permanent vers ce titre">¶</a></h3>
<p>Remplace les sauts de ligne dans du texte brut par le code HTML approprié&nbsp;; un saut de ligne simple devient un saut de ligne HTML (<code class="docutils literal notranslate"><span class="pre">&lt;br&gt;</span></code>) et un saut de ligne suivi d’une ligne vide devient un saut de paragraphe (<code class="docutils literal notranslate"><span class="pre">&lt;/p&gt;</span></code>).</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">linebreaks</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Joël\nest</span> <span class="pre">une</span> <span class="pre">limace&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&lt;p&gt;Joël&lt;br&gt;est</span> <span class="pre">une</span> <span class="pre">limace&lt;/p&gt;</span></code>.</p>
</div>
<div class="section" id="s-linebreaksbr">
<span id="s-std:templatefilter-linebreaksbr"></span><span id="linebreaksbr"></span><span id="std:templatefilter-linebreaksbr"></span><h3><code class="docutils literal notranslate"><span class="pre">linebreaksbr</span></code><a class="headerlink" href="#linebreaksbr" title="Lien permanent vers ce titre">¶</a></h3>
<p>Convertit tous les sauts de ligne d’un contenu de texte brut en sauts de ligne HTML (<code class="docutils literal notranslate"><span class="pre">&lt;br&gt;</span></code>).</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">linebreaksbr</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Joël\nest</span> <span class="pre">une</span> <span class="pre">limace&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">Joël&lt;br&gt;est</span> <span class="pre">une</span> <span class="pre">limace</span></code>.</p>
</div>
<div class="section" id="s-linenumbers">
<span id="s-std:templatefilter-linenumbers"></span><span id="linenumbers"></span><span id="std:templatefilter-linenumbers"></span><h3><code class="docutils literal notranslate"><span class="pre">linenumbers</span></code><a class="headerlink" href="#linenumbers" title="Lien permanent vers ce titre">¶</a></h3>
<p>Affiche un texte en numérotant ses lignes.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">linenumbers</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>one
two
three
</pre></div>
</div>
<p>le résultat sera&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>1. one
2. two
3. three
</pre></div>
</div>
</div>
<div class="section" id="s-ljust">
<span id="s-std:templatefilter-ljust"></span><span id="ljust"></span><span id="std:templatefilter-ljust"></span><h3><code class="docutils literal notranslate"><span class="pre">ljust</span></code><a class="headerlink" href="#ljust" title="Lien permanent vers ce titre">¶</a></h3>
<p>Aligne à gauche la valeur dans un champ de largeur donnée.</p>
<p><strong>Paramètre&nbsp;:</strong> taille de champ</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>&quot;<span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">ljust</span><span class="s2">:&quot;10&quot;</span> <span class="cp">}}</span>&quot;
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Django&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;Django</span>&#160;&#160;&#160; <span class="pre">&quot;</span></code>.</p>
</div>
<div class="section" id="s-lower">
<span id="s-std:templatefilter-lower"></span><span id="lower"></span><span id="std:templatefilter-lower"></span><h3><code class="docutils literal notranslate"><span class="pre">lower</span></code><a class="headerlink" href="#lower" title="Lien permanent vers ce titre">¶</a></h3>
<p>Convertit une chaîne tout en minuscules.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">J'aime</span> <span class="pre">FOLLEMENT</span> <span class="pre">cet</span> <span class="pre">album!</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">j'aime</span> <span class="pre">follement</span> <span class="pre">cet</span> <span class="pre">album!</span></code>.</p>
</div>
<div class="section" id="s-make-list">
<span id="s-std:templatefilter-make_list"></span><span id="make-list"></span><span id="std:templatefilter-make_list"></span><h3><code class="docutils literal notranslate"><span class="pre">make_list</span></code><a class="headerlink" href="#make-list" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie la valeur transformée en liste. Pour une chaîne, ce sera une liste de caractères. Pour un nombre entier, le paramètre est d’abord forcé en chaîne avant d’en faire une liste.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">make_list</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient la chaîne <code class="docutils literal notranslate"><span class="pre">&quot;Joël&quot;</span></code>, le résultat sera la liste <code class="docutils literal notranslate"><span class="pre">['J',</span> <span class="pre">'o',</span> <span class="pre">'ë',</span> <span class="pre">'l']</span></code>. Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">123</span></code>, le résultat sera la liste <code class="docutils literal notranslate"><span class="pre">['1',</span> <span class="pre">'2',</span> <span class="pre">'3']</span></code>.</p>
</div>
<div class="section" id="s-phone2numeric">
<span id="s-std:templatefilter-phone2numeric"></span><span id="phone2numeric"></span><span id="std:templatefilter-phone2numeric"></span><h3><code class="docutils literal notranslate"><span class="pre">phone2numeric</span></code><a class="headerlink" href="#phone2numeric" title="Lien permanent vers ce titre">¶</a></h3>
<p>Convertit un numéro de téléphone (pouvant contenir des lettres) en son équivalent numérique.</p>
<p>La valeur de départ ne doit pas forcément être un numéro de téléphone valable. Toute chaîne de caractères sera transformée sans renâcler.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">phone2numeric</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">800-COLLECT</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">800-2655328</span></code>.</p>
</div>
<div class="section" id="s-pluralize">
<span id="s-std:templatefilter-pluralize"></span><span id="pluralize"></span><span id="std:templatefilter-pluralize"></span><h3><code class="docutils literal notranslate"><span class="pre">pluralize</span></code><a class="headerlink" href="#pluralize" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie un suffixe de pluriel si la valeur est différente de <code class="docutils literal notranslate"><span class="pre">1</span></code>, <code class="docutils literal notranslate"><span class="pre">'1'</span></code> ou d’un objet de longueur 1. Par défaut, le suffixe est <code class="docutils literal notranslate"><span class="pre">'s'</span></code>.</p>
<p>Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>You have <span class="cp">{{</span> <span class="nv">num_messages</span> <span class="cp">}}</span> message<span class="cp">{{</span> <span class="nv">num_messages</span><span class="o">|</span><span class="nf">pluralize</span> <span class="cp">}}</span>.
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">num_messages</span></code> contient <code class="docutils literal notranslate"><span class="pre">1</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">You</span> <span class="pre">have</span> <span class="pre">1</span> <span class="pre">message.</span></code> Si <code class="docutils literal notranslate"><span class="pre">num_messages</span></code> contient <code class="docutils literal notranslate"><span class="pre">2</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">You</span> <span class="pre">have</span> <span class="pre">2</span> <span class="pre">messages.</span></code></p>
<p>Pour les mots exigeant un suffixe autre que <code class="docutils literal notranslate"><span class="pre">'s'</span></code>, vous pouvez indiquer un suffixe alternatif en paramètre du filtre.</p>
<p>Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>You have <span class="cp">{{</span> <span class="nv">num_walruses</span> <span class="cp">}}</span> walrus<span class="cp">{{</span> <span class="nv">num_walruses</span><span class="o">|</span><span class="nf">pluralize</span><span class="s2">:&quot;es&quot;</span> <span class="cp">}}</span>.
</pre></div>
</div>
<p>Pour les mots dont le pluriel n’est pas un simple suffixe, vous pouvez indiquer à la fois un suffixe singulier et un suffixe pluriel, séparés par une virgule.</p>
<p>Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>You have <span class="cp">{{</span> <span class="nv">num_cherries</span> <span class="cp">}}</span> cherr<span class="cp">{{</span> <span class="nv">num_cherries</span><span class="o">|</span><span class="nf">pluralize</span><span class="s2">:&quot;y,ies&quot;</span> <span class="cp">}}</span>.
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Utilisez <a class="reference internal" href="../../topics/i18n/translation.html#std:templatetag-blocktranslate"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">blocktranslate</span></code></a> pour mettre au pluriel des chaînes à traduire.</p>
</div>
</div>
<div class="section" id="s-pprint">
<span id="s-std:templatefilter-pprint"></span><span id="pprint"></span><span id="std:templatefilter-pprint"></span><h3><code class="docutils literal notranslate"><span class="pre">pprint</span></code><a class="headerlink" href="#pprint" title="Lien permanent vers ce titre">¶</a></h3>
<p>Un adaptateur autour de <a class="reference external" href="https://docs.python.org/3/library/pprint.html#pprint.pprint" title="(disponible dans Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">pprint.pprint()</span></code></a>, à réserver vraiment pour le débogage.</p>
</div>
<div class="section" id="s-random">
<span id="s-std:templatefilter-random"></span><span id="random"></span><span id="std:templatefilter-random"></span><h3><code class="docutils literal notranslate"><span class="pre">random</span></code><a class="headerlink" href="#random" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie un élément aléatoire dans la liste indiquée.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">random</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient la liste <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>, le résultat pourrait être <code class="docutils literal notranslate"><span class="pre">&quot;b&quot;</span></code>.</p>
</div>
<div class="section" id="s-rjust">
<span id="s-std:templatefilter-rjust"></span><span id="rjust"></span><span id="std:templatefilter-rjust"></span><h3><code class="docutils literal notranslate"><span class="pre">rjust</span></code><a class="headerlink" href="#rjust" title="Lien permanent vers ce titre">¶</a></h3>
<p>Aligne à droite la valeur dans un champ de largeur donnée.</p>
<p><strong>Paramètre&nbsp;:</strong> taille de champ</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>&quot;<span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">rjust</span><span class="s2">:&quot;10&quot;</span> <span class="cp">}}</span>&quot;
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Django&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;</span>&#160;&#160;&#160; <span class="pre">Django&quot;</span></code>.</p>
</div>
<div class="section" id="s-safe">
<span id="s-std:templatefilter-safe"></span><span id="safe"></span><span id="std:templatefilter-safe"></span><h3><code class="docutils literal notranslate"><span class="pre">safe</span></code><a class="headerlink" href="#safe" title="Lien permanent vers ce titre">¶</a></h3>
<p>Marque une chaîne comme n’ayant plus besoin d’être échappée avant d’être affichée dans du code HTML. Lorsque l’échappement automatique est désactivé, ce filtre n’a aucun effet.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Si vous enchaînez des filtres, un filtre appliqué après <code class="docutils literal notranslate"><span class="pre">safe</span></code> peut de nouveau rendre le contenu non fiable. Par exemple, le code suivant affiche la variable telle quelle, non échappée&nbsp;:</p>
<div class="last highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">var</span><span class="o">|</span><span class="nf">safe</span><span class="o">|</span><span class="nf">escape</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-safeseq">
<span id="s-std:templatefilter-safeseq"></span><span id="safeseq"></span><span id="std:templatefilter-safeseq"></span><h3><code class="docutils literal notranslate"><span class="pre">safeseq</span></code><a class="headerlink" href="#safeseq" title="Lien permanent vers ce titre">¶</a></h3>
<p>Applique le filtre <a class="reference internal" href="#std:templatefilter-safe"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">safe</span></code></a> à chaque élément d’une liste. Utile conjointement avec d’autres filtres qui agissent sur les listes, comme <a class="reference internal" href="#std:templatefilter-join"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">join</span></code></a>. Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">some_list</span><span class="o">|</span><span class="nf">safeseq</span><span class="o">|</span><span class="nf">join</span><span class="s2">:&quot;, &quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Vous n’auriez pas pu utiliser directement le filtre <a class="reference internal" href="#std:templatefilter-safe"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">safe</span></code></a> dans ce cas, car cela aurait d’abord converti la variable en chaîne au lieu d’agir sur les éléments individuels de la liste.</p>
</div>
<div class="section" id="s-slice">
<span id="s-std:templatefilter-slice"></span><span id="slice"></span><span id="std:templatefilter-slice"></span><h3><code class="docutils literal notranslate"><span class="pre">slice</span></code><a class="headerlink" href="#slice" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie un segment de la liste.</p>
<p>Utilise la même syntaxe que la segmentation de listes Python. Voir <a class="reference external" href="https://www.diveinto.org/python3/native-datatypes.html#slicinglists">https://www.diveinto.org/python3/native-datatypes.html#slicinglists</a> pour une introduction (en anglais).</p>
<p>Exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">some_list</span><span class="o">|</span><span class="nf">slice</span><span class="s2">:&quot;:2&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">some_list</span></code> contient <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b']</span></code>.</p>
</div>
<div class="section" id="s-slugify">
<span id="s-std:templatefilter-slugify"></span><span id="slugify"></span><span id="std:templatefilter-slugify"></span><h3><code class="docutils literal notranslate"><span class="pre">slugify</span></code><a class="headerlink" href="#slugify" title="Lien permanent vers ce titre">¶</a></h3>
<p>Convertit en ASCII. Convertit les espaces en tirets. Enlève les caractères qui ne sont ni alphanumériques, ni soulignements, ni tirets. Convertit en minuscules. Les espaces en début et fin de chaîne sont aussi enlevés.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">slugify</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Joël</span> <span class="pre">est</span> <span class="pre">une</span> <span class="pre">limace&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">joel-est-une-limace</span></code>.</p>
</div>
<div class="section" id="s-stringformat">
<span id="s-std:templatefilter-stringformat"></span><span id="stringformat"></span><span id="std:templatefilter-stringformat"></span><h3><code class="docutils literal notranslate"><span class="pre">stringformat</span></code><a class="headerlink" href="#stringformat" title="Lien permanent vers ce titre">¶</a></h3>
<p>Met en forme la variable en fonction du paramètre, une spécification de chaîne de format. Celle-ci utilise la syntaxe <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#old-string-formatting" title="(disponible dans Python v3.9)"><span>printf-style String Formatting</span></a>, à l’exclusion du caractère «&nbsp;%&nbsp;» initial.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">stringformat</span><span class="s2">:&quot;E&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>SI <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">10</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">1.000000E+01</span></code>.</p>
</div>
<div class="section" id="s-striptags">
<span id="s-std:templatefilter-striptags"></span><span id="striptags"></span><span id="std:templatefilter-striptags"></span><h3><code class="docutils literal notranslate"><span class="pre">striptags</span></code><a class="headerlink" href="#striptags" title="Lien permanent vers ce titre">¶</a></h3>
<p>Fait tout son possible pour supprimer toutes les balises [X]HTML.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><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>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;&lt;b&gt;Joël&lt;/b&gt;</span> <span class="pre">&lt;button&gt;est&lt;/button&gt;</span> <span class="pre">une</span> <span class="pre">&lt;span&gt;limace&lt;/span&gt;&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;Joël</span> <span class="pre">est</span> <span class="pre">une</span> <span class="pre">limace&quot;</span></code>.</p>
<div class="admonition-no-safety-guarantee admonition">
<p class="first admonition-title">Aucune garantie de sécurité</p>
<p class="last">Notez que <code class="docutils literal notranslate"><span class="pre">striptags</span></code> ne donne aucune garantie que le contenu résultant ne contienne plus de balisage HTML, plus particulièrement lorsque le contenu en entrée n’est pas du code HTML valide. N’appliquez donc <strong>JAMAIS</strong> le filtre  <code class="docutils literal notranslate"><span class="pre">safe</span></code> sur du contenu provenant de <code class="docutils literal notranslate"><span class="pre">striptags</span></code>. Si vous recherchez une solution plus solide, vous pouvez utiliser la bibliothèque Python <code class="docutils literal notranslate"><span class="pre">bleach</span></code>, notamment sa méthode <a class="reference external" href="https://bleach.readthedocs.io/en/latest/clean.html">clean</a>.</p>
</div>
</div>
<div class="section" id="s-time">
<span id="s-std:templatefilter-time"></span><span id="time"></span><span id="std:templatefilter-time"></span><h3><code class="docutils literal notranslate"><span class="pre">time</span></code><a class="headerlink" href="#time" title="Lien permanent vers ce titre">¶</a></h3>
<p>Met en forme une heure selon le format indiqué.</p>
<p>Le format indiqué peut être le format prédéfini <a class="reference internal" href="../settings.html#std:setting-TIME_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TIME_FORMAT</span></code></a> ou un format personnalisé, comme pour le filtre <a class="reference internal" href="#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a>. Notez que le format prédéfini est dépendant de la langue active.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:&quot;H:i&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> est équivalent à <code class="docutils literal notranslate"><span class="pre">datetime.datetime.now()</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;01:23&quot;</span></code>.</p>
<p>Notez que vous pouvez échapper par barre oblique inverse une chaîne de format si vous souhaitez utiliser la valeur «&nbsp;brute&nbsp;». Dans cet exemple, «&nbsp;h&nbsp;» et «&nbsp;m&nbsp;» sont tous deux échappés par barre oblique inverse, sinon ils seraient chacun interprétés comme une chaîne de format pour afficher respectivement l’heure et le mois&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:&quot;H\h i\m&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Le résultat affiché est « 01h 23m »..</p>
<p>Un autre exemple&nbsp;:</p>
<p>En supposant que <a class="reference internal" href="../settings.html#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span></code></a> vaille <code class="docutils literal notranslate"><span class="pre">True</span></code> et que <a class="reference internal" href="../settings.html#std:setting-LANGUAGE_CODE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGE_CODE</span></code></a> vaille par exemple <code class="docutils literal notranslate"><span class="pre">&quot;de&quot;</span></code>, alors pour&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:&quot;TIME_FORMAT&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;01:23&quot;</span></code> (le spécificateur de format <code class="docutils literal notranslate"><span class="pre">&quot;TIME_FORMAT&quot;</span></code> de la locale <code class="docutils literal notranslate"><span class="pre">de</span></code> fournie par Django contient <code class="docutils literal notranslate"><span class="pre">&quot;H:i&quot;</span></code>).</p>
<p>Le filtre <code class="docutils literal notranslate"><span class="pre">time</span></code> n’accepte dans la chaîne de format que les paramètres concernant l’heure du jour, et non la date. Si vous avez besoin de mettre en forme une date, utilisez plutôt le filtre <a class="reference internal" href="#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> (éventuellement avec <a class="reference internal" href="#std:templatefilter-time"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">time</span></code></a> si vous avez besoin de produire une valeur <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(disponible dans Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> complète).</p>
<p>Il existe une exception à la règle ci-dessus&nbsp;: lorsqu’il reçoit une valeur <code class="docutils literal notranslate"><span class="pre">datetime</span></code> contenant une information de fuseau horaire (une instance <code class="docutils literal notranslate"><span class="pre">datetime</span></code> <a class="reference internal" href="../../topics/i18n/timezones.html#naive-vs-aware-datetimes"><span class="std std-ref">consciente</span></a>), le filtre <code class="docutils literal notranslate"><span class="pre">time</span></code> accepte les <a class="reference internal" href="#date-and-time-formatting-specifiers"><span class="std std-ref">indicateurs de format</span></a> relatifs aux fuseaux horaires <code class="docutils literal notranslate"><span class="pre">'e'</span></code>, <code class="docutils literal notranslate"><span class="pre">'O'</span></code> , <code class="docutils literal notranslate"><span class="pre">'T'</span></code> et <code class="docutils literal notranslate"><span class="pre">'Z'</span></code>.</p>
<p>Lorsqu’aucune chaîne de format n’est indiquée, c’est le format <code class="docutils literal notranslate"><span class="pre">TIME_FORMAT</span></code> qui est utilisé&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>est identique à&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:&quot;TIME_FORMAT&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-timesince">
<span id="s-std:templatefilter-timesince"></span><span id="timesince"></span><span id="std:templatefilter-timesince"></span><h3><code class="docutils literal notranslate"><span class="pre">timesince</span></code><a class="headerlink" href="#timesince" title="Lien permanent vers ce titre">¶</a></h3>
<p>Met en forme une date de manière relative à cette date (par ex. «&nbsp;4 jours, 6 heures&nbsp;»).</p>
<p>Accepte un paramètre facultatif sous forme de variable contenant la date à utiliser comme point de comparaison (sans paramètre, le point de comparaison est <em>maintenant</em>). Par exemple, si <code class="docutils literal notranslate"><span class="pre">blog_date</span></code> est une instance de date représentant minuit le 1 juin 2006 et que <code class="docutils literal notranslate"><span class="pre">comment_date</span></code> est une instance de date représentant le 1 juin 2006 à 08:00, le code suivant renverra «&nbsp;8 heures&nbsp;»:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">blog_date</span><span class="o">|</span><span class="nf">timesince</span><span class="o">:</span><span class="nv">comment_date</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>La comparaison entre des dates/heures naïves et conscientes (en rapport au fuseau horaire) renvoie une chaîne vide.</p>
<p>Les minutes sont la plus petite unité utilisée, et «&nbsp;0 minute&nbsp;» est renvoyé pour toute date dans le futur en fonction du point de comparaison.</p>
</div>
<div class="section" id="s-timeuntil">
<span id="s-std:templatefilter-timeuntil"></span><span id="timeuntil"></span><span id="std:templatefilter-timeuntil"></span><h3><code class="docutils literal notranslate"><span class="pre">timeuntil</span></code><a class="headerlink" href="#timeuntil" title="Lien permanent vers ce titre">¶</a></h3>
<p>Semblable à <code class="docutils literal notranslate"><span class="pre">timesince</span></code>, sauf qu’il mesure le temps depuis maintenant jusqu’à la date ou date/heure indiquée. Par exemple, si nous sommes le 1er juin 2006 et que <code class="docutils literal notranslate"><span class="pre">conference_date</span></code> est une instance de date contenant le 29 juin 2006, <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">conference_date|timeuntil</span> <span class="pre">}}</span></code> renverra «&nbsp;4 semaines&nbsp;».</p>
<p>Accepte un paramètre facultatif sous forme de variable contenant la date à utiliser comme point de comparaison (au lieu de <em>maintenant</em>). Si <code class="docutils literal notranslate"><span class="pre">from_date</span></code> contient le 22 juin 2006, le code suivant renverra «&nbsp;1 semaine&nbsp;»&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">conference_date</span><span class="o">|</span><span class="nf">timeuntil</span><span class="o">:</span><span class="nv">from_date</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>La comparaison entre des dates/heures naïves et conscientes (en rapport au fuseau horaire) renvoie une chaîne vide.</p>
<p>Les minutes sont la plus petite unité utilisée, et « 0 minute » est renvoyé pour toute date dans le passé en fonction du point de comparaison.</p>
</div>
<div class="section" id="s-title">
<span id="s-std:templatefilter-title"></span><span id="title"></span><span id="std:templatefilter-title"></span><h3><code class="docutils literal notranslate"><span class="pre">title</span></code><a class="headerlink" href="#title" title="Lien permanent vers ce titre">¶</a></h3>
<p>Convertit une chaîne en casse de titre de type anglophone en mettant en majuscule la première lettre de chaque mot et en minuscules les autres lettres. Cette balise ne fait aucun effort pour conserver en minuscules les mots «&nbsp;utilitaires&nbsp;».</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">title</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;mon</span> <span class="pre">PREMIER</span> <span class="pre">article&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;Mon</span> <span class="pre">Premier</span> <span class="pre">Article&quot;</span></code>.</p>
</div>
<div class="section" id="s-truncatechars">
<span id="s-std:templatefilter-truncatechars"></span><span id="truncatechars"></span><span id="std:templatefilter-truncatechars"></span><h3><code class="docutils literal notranslate"><span class="pre">truncatechars</span></code><a class="headerlink" href="#truncatechars" title="Lien permanent vers ce titre">¶</a></h3>
<p>Tronque une chaîne si elle est plus longue que le nombre de caractères indiqué. Les chaînes tronquées se terminent par un caractère points de suspension («&nbsp;…&nbsp;»).</p>
<p><strong>Paramètre&nbsp;:</strong> le nombre maximum de caractères du résultat</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">truncatechars</span><span class="o">:</span><span class="m">7</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Joël</span> <span class="pre">est</span> <span class="pre">une</span> <span class="pre">limace&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;Joël</span> <span class="pre">e…&quot;</span></code>.</p>
</div>
<div class="section" id="s-truncatechars-html">
<span id="s-std:templatefilter-truncatechars_html"></span><span id="truncatechars-html"></span><span id="std:templatefilter-truncatechars_html"></span><h3><code class="docutils literal notranslate"><span class="pre">truncatechars_html</span></code><a class="headerlink" href="#truncatechars-html" title="Lien permanent vers ce titre">¶</a></h3>
<p>Semblable à <a class="reference internal" href="#std:templatefilter-truncatechars"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">truncatechars</span></code></a>, mais en tenant compte des balises HTML. Toute balise ouverte dans la chaîne et non fermée avant le point de troncature est immédiatement fermée après la troncature.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">truncatechars_html</span><span class="o">:</span><span class="m">7</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;&lt;p&gt;Joël</span> <span class="pre">est</span> <span class="pre">une</span> <span class="pre">limace&lt;/p&gt;&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;&lt;p&gt;Joël</span> <span class="pre">e…&lt;/p&gt;&quot;</span></code>.</p>
<p>Les sauts de ligne dans le contenu HTML sont préservés.</p>
</div>
<div class="section" id="s-truncatewords">
<span id="s-std:templatefilter-truncatewords"></span><span id="truncatewords"></span><span id="std:templatefilter-truncatewords"></span><h3><code class="docutils literal notranslate"><span class="pre">truncatewords</span></code><a class="headerlink" href="#truncatewords" title="Lien permanent vers ce titre">¶</a></h3>
<p>Tronque une chaîne après un certain nombre de mots.</p>
<p><strong>Paramètre&nbsp;:</strong> le nombre maximum de mots du résultat</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">truncatewords</span><span class="o">:</span><span class="m">2</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Joël</span> <span class="pre">est</span> <span class="pre">une</span> <span class="pre">limace&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;Joël</span> <span class="pre">est</span> <span class="pre">…&quot;</span></code>.</p>
<p>Les sauts de ligne à l’intérieur de la valeur seront enlevés.</p>
</div>
<div class="section" id="s-truncatewords-html">
<span id="s-std:templatefilter-truncatewords_html"></span><span id="truncatewords-html"></span><span id="std:templatefilter-truncatewords_html"></span><h3><code class="docutils literal notranslate"><span class="pre">truncatewords_html</span></code><a class="headerlink" href="#truncatewords-html" title="Lien permanent vers ce titre">¶</a></h3>
<p>Semblable à <a class="reference internal" href="#std:templatefilter-truncatewords"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">truncatewords</span></code></a>, mais en tenant compte des balises HTML. Toute balise ouverte dans la chaîne et non fermée avant le point de troncature est immédiatement fermée après la troncature.</p>
<p>Ce filtre est moins efficace que <a class="reference internal" href="#std:templatefilter-truncatewords"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">truncatewords</span></code></a>, il ne devrait donc être employé que lorsque l’on agit sur du texte contenant du HTML.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">truncatewords_html</span><span class="o">:</span><span class="m">2</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;&lt;p&gt;Joël</span> <span class="pre">est</span> <span class="pre">une</span> <span class="pre">limace&lt;/p&gt;&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;&lt;p&gt;Joël</span> <span class="pre">est</span> <span class="pre">…&lt;/p&gt;&quot;</span></code>.</p>
<p>Les sauts de ligne dans le contenu HTML sont préservés.</p>
</div>
<div class="section" id="s-unordered-list">
<span id="s-std:templatefilter-unordered_list"></span><span id="unordered-list"></span><span id="std:templatefilter-unordered_list"></span><h3><code class="docutils literal notranslate"><span class="pre">unordered_list</span></code><a class="headerlink" href="#unordered-list" title="Lien permanent vers ce titre">¶</a></h3>
<p>Accepte des listes imbriquées et les traite récursivement pour obtenir une liste HTML non ordonnée, <strong>sans</strong> les balises &lt;ul&gt; ouvrantes et fermantes.</p>
<p>Il est nécessaire que la liste soit dans le bon format. Par exemple, si <code class="docutils literal notranslate"><span class="pre">var</span></code> contient <code class="docutils literal notranslate"><span class="pre">['States',</span> <span class="pre">['Kansas',</span> <span class="pre">['Lawrence',</span> <span class="pre">'Topeka'],</span> <span class="pre">'Illinois']]</span></code>, le résultat de <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">var|unordered_list</span> <span class="pre">}}</span></code> sera&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>States
<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Kansas
        <span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
                <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Lawrence<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
                <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Topeka<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Illinois<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-upper">
<span id="s-std:templatefilter-upper"></span><span id="upper"></span><span id="std:templatefilter-upper"></span><h3><code class="docutils literal notranslate"><span class="pre">upper</span></code><a class="headerlink" href="#upper" title="Lien permanent vers ce titre">¶</a></h3>
<p>Convertit une chaîne tout en majuscules.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">upper</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">&quot;Joël</span> <span class="pre">est</span> <span class="pre">une</span> <span class="pre">limace&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;JOËL</span> <span class="pre">EST</span> <span class="pre">UNE</span> <span class="pre">LIMACE&quot;</span></code>.</p>
</div>
<div class="section" id="s-urlencode">
<span id="s-std:templatefilter-urlencode"></span><span id="urlencode"></span><span id="std:templatefilter-urlencode"></span><h3><code class="docutils literal notranslate"><span class="pre">urlencode</span></code><a class="headerlink" href="#urlencode" title="Lien permanent vers ce titre">¶</a></h3>
<p>Échappe une valeur pour être incluse dans une URL.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlencode</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">&quot;https://www.example.org/foo?a=b&amp;c=d&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;https%3A//www.example.org/foo%3Fa%3Db%26c%3Dd&quot;</span></code>.</p>
<p>Il est possible de fournir un paramètre facultatif contenant les caractères qui ne doivent pas être échappés.</p>
<p>Sans ce paramètre, le caractère «&nbsp;/&nbsp;» est considéré comme sûr. Il est possible de fournir une chaîne vide pour indiquer que <em>tous</em> les caractères doivent être échappés. Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlencode</span><span class="s2">:&quot;&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">&quot;https://www.example.org/&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;https%3A%2F%2Fwww.example.org%2F&quot;</span></code>.</p>
</div>
<div class="section" id="s-urlize">
<span id="s-std:templatefilter-urlize"></span><span id="urlize"></span><span id="std:templatefilter-urlize"></span><h3><code class="docutils literal notranslate"><span class="pre">urlize</span></code><a class="headerlink" href="#urlize" title="Lien permanent vers ce titre">¶</a></h3>
<p>Convertit les URL et les adresses électroniques dans le texte en hyperliens.</p>
<p>Cette balise de gabarit fonctionne avec des liens préfixés par <code class="docutils literal notranslate"><span class="pre">http://</span></code>, <code class="docutils literal notranslate"><span class="pre">https://</span></code> ou <code class="docutils literal notranslate"><span class="pre">www.</span></code>. Par exemple, <code class="docutils literal notranslate"><span class="pre">https://goo.gl/aia1t</span></code> sera converti mais  <code class="docutils literal notranslate"><span class="pre">goo.gl/aia1t</span></code> ne le sera pas.</p>
<p>Il convertit également les liens «&nbsp;noms de domaine&nbsp;» s’ils se terminent par l’un des domaines de premier niveau de base (<code class="docutils literal notranslate"><span class="pre">.com</span></code>, <code class="docutils literal notranslate"><span class="pre">.edu</span></code>, <code class="docutils literal notranslate"><span class="pre">.gov</span></code>, <code class="docutils literal notranslate"><span class="pre">.int</span></code>, <code class="docutils literal notranslate"><span class="pre">.mil</span></code>, <code class="docutils literal notranslate"><span class="pre">.net</span></code> et <code class="docutils literal notranslate"><span class="pre">.org</span></code>). Par exemple, <code class="docutils literal notranslate"><span class="pre">djangoproject.com</span></code> sera converti.</p>
<p>Les liens peuvent se terminer par de la ponctuation (points, virgules, parenthèse fermante) et peuvent commencer par de la ponctuation (parenthèse ouvrante)&nbsp;; <code class="docutils literal notranslate"><span class="pre">urlize</span></code> n’en sera pas perturbé pour autant.</p>
<p>Les liens générés par <code class="docutils literal notranslate"><span class="pre">urlize</span></code> reçoivent l’attribut <code class="docutils literal notranslate"><span class="pre">rel=&quot;nofollow&quot;</span></code>.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlize</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">&quot;Visitez</span> <span class="pre">www.djangoproject.com&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;Visitez</span> <span class="pre">&lt;a</span> <span class="pre">href=&quot;http://www.djangoproject.com&quot;</span> <span class="pre">rel=&quot;nofollow&quot;&gt;www.djangoproject.com&lt;/a&gt;&quot;</span></code>.</p>
<p>En plus des liens de type Web, <code class="docutils literal notranslate"><span class="pre">urlize</span></code> convertit aussi les adresses électroniques en liens <code class="docutils literal notranslate"><span class="pre">mailto:</span></code>. Si <code class="docutils literal notranslate"><span class="pre">value</span></code> contient <code class="docutils literal notranslate"><span class="pre">&quot;Envoyez</span> <span class="pre">les</span> <span class="pre">questions</span> <span class="pre">à</span> <span class="pre">foo&#64;example.com&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;Envoyez</span> <span class="pre">les</span> <span class="pre">questions</span> <span class="pre">à</span> <span class="pre">&lt;a</span> <span class="pre">href=&quot;mailto:foo&#64;example.com&quot;&gt;foo&#64;example.com&lt;/a&gt;&quot;</span></code>.</p>
<p>Le filtre <code class="docutils literal notranslate"><span class="pre">urlize</span></code> accepte également un paramètre facultatif <code class="docutils literal notranslate"><span class="pre">autoescape</span></code>. Si celui-ci vaut <code class="docutils literal notranslate"><span class="pre">True</span></code>, le texte du lien et les URL seront échappés par le filtre <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> de Django. La valeur par défaut de <code class="docutils literal notranslate"><span class="pre">autoescape</span></code> est <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Si <code class="docutils literal notranslate"><span class="pre">urlize</span></code> est appliqué à du texte qui contient déjà des balises HTML ou à des adresses électroniques contenant des guillemets simples (<code class="docutils literal notranslate"><span class="pre">'</span></code>), les choses ne se passeront pas comme prévu. N’appliquez ce filtre qu’à du texte pur.</p>
</div>
</div>
<div class="section" id="s-urlizetrunc">
<span id="s-std:templatefilter-urlizetrunc"></span><span id="urlizetrunc"></span><span id="std:templatefilter-urlizetrunc"></span><h3><code class="docutils literal notranslate"><span class="pre">urlizetrunc</span></code><a class="headerlink" href="#urlizetrunc" title="Lien permanent vers ce titre">¶</a></h3>
<p>Convertit les URL et les adresses électroniques dans le texte en hyperliens tout comme <a class="reference internal" href="#urlize">urlize</a>, mais tronque le texte des URL plus long que la limite du nombre de caractères indiquée.</p>
<p><strong>Paramètre&nbsp;:</strong> le nombre maximum de caractères que le texte du lien contiendra après avoir été tronqué si nécessaire, y compris l’ellipse ajoutée par la troncature, le cas échéant.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlizetrunc</span><span class="o">:</span><span class="m">15</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">&quot;Visitez</span> <span class="pre">www.djangoproject.com&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">'Visitez</span> <span class="pre">&lt;a</span> <span class="pre">href=&quot;http://www.djangoproject.com&quot;</span> <span class="pre">rel=&quot;nofollow&quot;&gt;www.djangoproj…&lt;/a&gt;'</span></code>.</p>
<p>Comme pour <a class="reference internal" href="#urlize">urlize</a>, ce filtre ne doit être appliqué qu’à du texte pur.</p>
</div>
<div class="section" id="s-wordcount">
<span id="s-std:templatefilter-wordcount"></span><span id="wordcount"></span><span id="std:templatefilter-wordcount"></span><h3><code class="docutils literal notranslate"><span class="pre">wordcount</span></code><a class="headerlink" href="#wordcount" title="Lien permanent vers ce titre">¶</a></h3>
<p>Renvoie le nombre de mots.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">wordcount</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">&quot;Joël</span> <span class="pre">est</span> <span class="pre">une</span> <span class="pre">limace&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">4</span></code>.</p>
</div>
<div class="section" id="s-wordwrap">
<span id="s-std:templatefilter-wordwrap"></span><span id="wordwrap"></span><span id="std:templatefilter-wordwrap"></span><h3><code class="docutils literal notranslate"><span class="pre">wordwrap</span></code><a class="headerlink" href="#wordwrap" title="Lien permanent vers ce titre">¶</a></h3>
<p>Ajoute des sauts de ligne d’après une longueur de ligne maximale.</p>
<p><strong>Paramètre&nbsp;:</strong> le nombre de caractères après lequel un saut de ligne est rajouté dans le texte.</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">wordwrap</span><span class="o">:</span><span class="m">5</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">value</span></code> vaut <code class="docutils literal notranslate"><span class="pre">Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug</span></code>, le résultat sera&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>Joel
is a
slug
</pre></div>
</div>
</div>
<div class="section" id="s-yesno">
<span id="s-std:templatefilter-yesno"></span><span id="yesno"></span><span id="std:templatefilter-yesno"></span><h3><code class="docutils literal notranslate"><span class="pre">yesno</span></code><a class="headerlink" href="#yesno" title="Lien permanent vers ce titre">¶</a></h3>
<p>Fait correspondre les valeurs vrai (<code class="docutils literal notranslate"><span class="pre">True</span></code>), faux (<code class="docutils literal notranslate"><span class="pre">False</span></code>) et facultativement <code class="docutils literal notranslate"><span class="pre">None</span></code> respectivement aux chaînes de caractères «&nbsp;yes&nbsp;», «&nbsp;no&nbsp;», «&nbsp;maybe&nbsp;», ou à une liste personnalisée de chaînes séparées par des virgules, et renvoie la chaîne correspondant à la valeur transmise&nbsp;:</p>
<p>Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">yesno</span><span class="s2">:&quot;yeah,no,maybe&quot;</span> <span class="cp">}}</span>
</pre></div>
</div>
<table class="docutils">
<colgroup>
<col width="13%" />
<col width="29%" />
<col width="57%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Valeur</th>
<th class="head">Paramètre</th>
<th class="head">Affiche</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">True</span></code></td>
<td>&#160;</td>
<td><code class="docutils literal notranslate"><span class="pre">yes</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">True</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">&quot;yeah,no,maybe&quot;</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">yeah</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">False</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">&quot;yeah,no,maybe&quot;</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">no</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">None</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">&quot;yeah,no,maybe&quot;</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">maybe</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">None</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">&quot;yeah,no&quot;</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">no</span></code> (convertit <code class="docutils literal notranslate"><span class="pre">None</span></code> en <code class="docutils literal notranslate"><span class="pre">False</span></code> si aucune correspondance n’est indiquée pour <code class="docutils literal notranslate"><span class="pre">None</span></code>)</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="s-internationalization-tags-and-filters">
<span id="internationalization-tags-and-filters"></span><h2>Balises et filtres d’internationalisation<a class="headerlink" href="#internationalization-tags-and-filters" title="Lien permanent vers ce titre">¶</a></h2>
<p>Django fournit des balises et des filtres de gabarit pour contrôler chaque aspect de <a class="reference internal" href="../../topics/i18n/index.html"><span class="doc">l’internationalization</span></a> dans les gabarits. Ils permettent un contrôle plus fin sur les traductions, la mise en forme et les conversions de fuseaux horaires.</p>
<div class="section" id="s-i18n">
<span id="i18n"></span><h3><code class="docutils literal notranslate"><span class="pre">i18n</span></code><a class="headerlink" href="#i18n" title="Lien permanent vers ce titre">¶</a></h3>
<p>Cette bibliothèque permet d’indiquer du texte à traduire dans les gabarits. Pour l’activer, définissez <a class="reference internal" href="../settings.html#std:setting-USE_I18N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_I18N</span></code></a> à <code class="docutils literal notranslate"><span class="pre">True</span></code> puis chargez-la avec <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">i18n</span> <span class="pre">%}</span></code>.</p>
<p>Voir <a class="reference internal" href="../../topics/i18n/translation.html#specifying-translation-strings-in-template-code"><span class="std std-ref">Internationalisation&nbsp;: dans le code des gabarits</span></a>.</p>
</div>
<div class="section" id="s-l10n">
<span id="l10n"></span><h3><code class="docutils literal notranslate"><span class="pre">l10n</span></code><a class="headerlink" href="#l10n" title="Lien permanent vers ce titre">¶</a></h3>
<p>Cette bibliothèque permet de contrôler la régionalisation des valeurs dans les gabarits. Il suffit de charger la bibliothèque en utilisant <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">l10n</span> <span class="pre">%}</span></code>, mais il est souvent préférable de définir aussi <a class="reference internal" href="../settings.html#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span></code></a> à <code class="docutils literal notranslate"><span class="pre">True</span></code> pour que la régionalisation soit active par défaut.</p>
<p>Voir <a class="reference internal" href="../../topics/i18n/formatting.html#topic-l10n-templates"><span class="std std-ref">Contrôle de la régionalisation dans les gabarits</span></a>.</p>
</div>
<div class="section" id="s-tz">
<span id="tz"></span><h3><code class="docutils literal notranslate"><span class="pre">tz</span></code><a class="headerlink" href="#tz" title="Lien permanent vers ce titre">¶</a></h3>
<p>Cette bibliothèque permet de contrôler les conversions de fuseaux horaires dans les gabarits. Comme <code class="docutils literal notranslate"><span class="pre">l10n</span></code>, il suffit de charger la bibliothèque avec <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">tz</span> <span class="pre">%}</span></code>, mais il est préférable de définir aussi <a class="reference internal" href="../settings.html#std:setting-USE_TZ"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_TZ</span></code></a> à <code class="docutils literal notranslate"><span class="pre">True</span></code> pour que la conversion en temps locale se fasse par défaut.</p>
<p>Voir <a class="reference internal" href="../../topics/i18n/timezones.html#time-zones-in-templates"><span class="std std-ref">Affichage de fuseaux horaires conscients dans les gabarits</span></a>.</p>
</div>
</div>
<div class="section" id="s-other-tags-and-filters-libraries">
<span id="other-tags-and-filters-libraries"></span><h2>Autres bibliothèques de balises et de filtres<a class="headerlink" href="#other-tags-and-filters-libraries" title="Lien permanent vers ce titre">¶</a></h2>
<p>Django contient quelques autres bibliothèques de balises de gabarit que vous devez activer explicitement dans votre réglage <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> et qu’il fut charger dans les gabarits avec la balise <a class="reference internal" href="#std:templatetag-load"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">%}</span></code></a>.</p>
<div class="section" id="s-django-contrib-humanize">
<span id="django-contrib-humanize"></span><h3><code class="docutils literal notranslate"><span class="pre">django.contrib.humanize</span></code><a class="headerlink" href="#django-contrib-humanize" title="Lien permanent vers ce titre">¶</a></h3>
<p>Un ensemble de filtres de gabarit utiles pour ajouter une touche «&nbsp;humaine&nbsp;» aux données. Voir <a class="reference internal" href="../contrib/humanize.html"><span class="doc">django.contrib.humanize</span></a>.</p>
</div>
<div class="section" id="s-static">
<span id="static"></span><h3><code class="docutils literal notranslate"><span class="pre">static</span></code><a class="headerlink" href="#static" title="Lien permanent vers ce titre">¶</a></h3>
<div class="section" id="s-std:templatetag-static">
<span id="s-id6"></span><span id="std:templatetag-static"></span><span id="id6"></span><h4><code class="docutils literal notranslate"><span class="pre">static</span></code><a class="headerlink" href="#std:templatetag-static" title="Lien permanent vers ce titre">¶</a></h4>
<p>Pour créer des liens vers les fichiers statiques enregistrés dans <a class="reference internal" href="../settings.html#std:setting-STATIC_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_ROOT</span></code></a>, Django fournit une balise de gabarit <a class="reference internal" href="#std:templatetag-static"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">static</span></code></a>. Si l’application <a class="reference internal" href="../contrib/staticfiles.html#module-django.contrib.staticfiles" title="django.contrib.staticfiles: An app for handling static files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.staticfiles</span></code></a> est installée, la balise servira les fichiers en utilisant la méthode <code class="docutils literal notranslate"><span class="pre">url()</span></code> du stockage présent dans <a class="reference internal" href="../settings.html#std:setting-STATICFILES_STORAGE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_STORAGE</span></code></a>. Par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">static</span> <span class="s1">&#39;images/hi.jpg&#39;</span> <span class="cp">%}</span><span class="s">&quot;</span> <span class="na">alt</span><span class="o">=</span><span class="s">&quot;Hi!&quot;</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Elle est aussi capable d’exploiter des variables de contexte standards&nbsp;; par exemple en supposant qu’une variable <code class="docutils literal notranslate"><span class="pre">user_stylesheet</span></code> est transmise au gabarit&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">link</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;stylesheet&quot;</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">static</span> <span class="nv">user_stylesheet</span> <span class="cp">%}</span><span class="s">&quot;</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text/css&quot;</span> <span class="na">media</span><span class="o">=</span><span class="s">&quot;screen&quot;</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Si vous souhaitez récupérer une URL statique sans l’afficher, vous pouvez effectuer un appel un peu différent&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">static</span> <span class="s2">&quot;images/hi.jpg&quot;</span> <span class="k">as</span> <span class="nv">myphoto</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">myphoto</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="p">&gt;</span>
</pre></div>
</div>
<div class="admonition-using-jinja2-templates admonition">
<p class="first admonition-title">Utilisation de gabarits Jinja2</p>
<p class="last">Voir <a class="reference internal" href="../../topics/templates.html#django.template.backends.jinja2.Jinja2" title="django.template.backends.jinja2.Jinja2"><code class="xref py py-class docutils literal notranslate"><span class="pre">Jinja2</span></code></a> pour des informations sur l’utilisation de la balise <code class="docutils literal notranslate"><span class="pre">static</span></code> avec Jinja2.</p>
</div>
</div>
<div class="section" id="s-get-static-prefix">
<span id="s-std:templatetag-get_static_prefix"></span><span id="get-static-prefix"></span><span id="std:templatetag-get_static_prefix"></span><h4><code class="docutils literal notranslate"><span class="pre">get_static_prefix</span></code><a class="headerlink" href="#get-static-prefix" title="Lien permanent vers ce titre">¶</a></h4>
<p>La préférence devrait être donnée à la balise de gabarit <a class="reference internal" href="#std:templatetag-static"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">static</span></code></a>, mais si vous avez besoin d’un contrôle plus fin sur l’endroit et la manière dont <a class="reference internal" href="../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> est injecté dans le gabarit, vous pouvez utiliser la balise de gabarit <a class="reference internal" href="#std:templatetag-get_static_prefix"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">get_static_prefix</span></code></a>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">get_static_prefix</span> <span class="cp">%}</span><span class="s">images/hi.jpg&quot;</span> <span class="na">alt</span><span class="o">=</span><span class="s">&quot;Hi!&quot;</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Il existe aussi une seconde forme utilisable pour empêcher du traitement inutile si vous avez besoin de la même valeur plusieurs fois&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">get_static_prefix</span> <span class="k">as</span> <span class="nv">STATIC_PREFIX</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">STATIC_PREFIX</span> <span class="cp">}}</span><span class="s">images/hi.jpg&quot;</span> <span class="na">alt</span><span class="o">=</span><span class="s">&quot;Hi!&quot;</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{{</span> <span class="nv">STATIC_PREFIX</span> <span class="cp">}}</span><span class="s">images/hi2.jpg&quot;</span> <span class="na">alt</span><span class="o">=</span><span class="s">&quot;Hello!&quot;</span><span class="p">&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-get-media-prefix">
<span id="s-std:templatetag-get_media_prefix"></span><span id="get-media-prefix"></span><span id="std:templatetag-get_media_prefix"></span><h4><code class="docutils literal notranslate"><span class="pre">get_media_prefix</span></code><a class="headerlink" href="#get-media-prefix" title="Lien permanent vers ce titre">¶</a></h4>
<p>Semblable à <a class="reference internal" href="#std:templatetag-get_static_prefix"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">get_static_prefix</span></code></a>, <code class="docutils literal notranslate"><span class="pre">get_media_prefix</span></code> crée une variable de gabarit contenant le préfixe de média <a class="reference internal" href="../settings.html#std:setting-MEDIA_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_URL</span></code></a>, par exemple&nbsp;:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">body</span> <span class="na">data-media-url</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">get_media_prefix</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>En stockant la valeur dans un attribut de données, nous nous assurons qu’elle soit proprement échappée si nous souhaitons l’utiliser dans un contexte JavaScript.</p>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Balises et filtres de gabarit intégrés</a><ul>
<li><a class="reference internal" href="#built-in-tag-reference">Référence des balises intégrées</a><ul>
<li><a class="reference internal" href="#autoescape"><code class="docutils literal notranslate"><span class="pre">autoescape</span></code></a></li>
<li><a class="reference internal" href="#block"><code class="docutils literal notranslate"><span class="pre">block</span></code></a></li>
<li><a class="reference internal" href="#comment"><code class="docutils literal notranslate"><span class="pre">comment</span></code></a></li>
<li><a class="reference internal" href="#csrf-token"><code class="docutils literal notranslate"><span class="pre">csrf_token</span></code></a></li>
<li><a class="reference internal" href="#cycle"><code class="docutils literal notranslate"><span class="pre">cycle</span></code></a></li>
<li><a class="reference internal" href="#debug"><code class="docutils literal notranslate"><span class="pre">debug</span></code></a></li>
<li><a class="reference internal" href="#extends"><code class="docutils literal notranslate"><span class="pre">extends</span></code></a></li>
<li><a class="reference internal" href="#filter"><code class="docutils literal notranslate"><span class="pre">filter</span></code></a></li>
<li><a class="reference internal" href="#firstof"><code class="docutils literal notranslate"><span class="pre">firstof</span></code></a></li>
<li><a class="reference internal" href="#for"><code class="docutils literal notranslate"><span class="pre">for</span></code></a></li>
<li><a class="reference internal" href="#for-empty"><code class="docutils literal notranslate"><span class="pre">for</span></code> … <code class="docutils literal notranslate"><span class="pre">empty</span></code></a></li>
<li><a class="reference internal" href="#if"><code class="docutils literal notranslate"><span class="pre">if</span></code></a><ul>
<li><a class="reference internal" href="#boolean-operators">Opérateurs booléens</a><ul>
<li><a class="reference internal" href="#operator">Opérateur <code class="docutils literal notranslate"><span class="pre">==</span></code></a></li>
<li><a class="reference internal" href="#id1">Opérateur <code class="docutils literal notranslate"><span class="pre">!=</span></code></a></li>
<li><a class="reference internal" href="#id2">Opérateur <code class="docutils literal notranslate"><span class="pre">&lt;</span></code></a></li>
<li><a class="reference internal" href="#id3">Opérateur <code class="docutils literal notranslate"><span class="pre">&gt;</span></code></a></li>
<li><a class="reference internal" href="#id4">Opérateur <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code></a></li>
<li><a class="reference internal" href="#id5">Opérateur <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code></a></li>
<li><a class="reference internal" href="#in-operator">Opérateur <code class="docutils literal notranslate"><span class="pre">in</span></code></a></li>
<li><a class="reference internal" href="#not-in-operator">Opérateur <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code></a></li>
<li><a class="reference internal" href="#is-operator">Opérateur <code class="docutils literal notranslate"><span class="pre">is</span></code></a></li>
<li><a class="reference internal" href="#is-not-operator">Opérateur <code class="docutils literal notranslate"><span class="pre">is</span> <span class="pre">not</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#filters">Filtres</a></li>
<li><a class="reference internal" href="#complex-expressions">Expressions complexes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#ifequal-and-ifnotequal"><code class="docutils literal notranslate"><span class="pre">ifequal</span></code> et <code class="docutils literal notranslate"><span class="pre">ifnotequal</span></code></a></li>
<li><a class="reference internal" href="#ifchanged"><code class="docutils literal notranslate"><span class="pre">ifchanged</span></code></a></li>
<li><a class="reference internal" href="#include"><code class="docutils literal notranslate"><span class="pre">include</span></code></a></li>
<li><a class="reference internal" href="#load"><code class="docutils literal notranslate"><span class="pre">load</span></code></a></li>
<li><a class="reference internal" href="#lorem"><code class="docutils literal notranslate"><span class="pre">lorem</span></code></a></li>
<li><a class="reference internal" href="#now"><code class="docutils literal notranslate"><span class="pre">now</span></code></a></li>
<li><a class="reference internal" href="#regroup"><code class="docutils literal notranslate"><span class="pre">regroup</span></code></a><ul>
<li><a class="reference internal" href="#grouping-on-other-properties">Groupement selon d’autres propriétés</a></li>
</ul>
</li>
<li><a class="reference internal" href="#resetcycle"><code class="docutils literal notranslate"><span class="pre">resetcycle</span></code></a></li>
<li><a class="reference internal" href="#spaceless"><code class="docutils literal notranslate"><span class="pre">spaceless</span></code></a></li>
<li><a class="reference internal" href="#templatetag"><code class="docutils literal notranslate"><span class="pre">templatetag</span></code></a></li>
<li><a class="reference internal" href="#url"><code class="docutils literal notranslate"><span class="pre">url</span></code></a></li>
<li><a class="reference internal" href="#verbatim"><code class="docutils literal notranslate"><span class="pre">verbatim</span></code></a></li>
<li><a class="reference internal" href="#widthratio"><code class="docutils literal notranslate"><span class="pre">widthratio</span></code></a></li>
<li><a class="reference internal" href="#with"><code class="docutils literal notranslate"><span class="pre">with</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#built-in-filter-reference">Référence des filtres intégrés</a><ul>
<li><a class="reference internal" href="#add"><code class="docutils literal notranslate"><span class="pre">add</span></code></a></li>
<li><a class="reference internal" href="#addslashes"><code class="docutils literal notranslate"><span class="pre">addslashes</span></code></a></li>
<li><a class="reference internal" href="#capfirst"><code class="docutils literal notranslate"><span class="pre">capfirst</span></code></a></li>
<li><a class="reference internal" href="#center"><code class="docutils literal notranslate"><span class="pre">center</span></code></a></li>
<li><a class="reference internal" href="#cut"><code class="docutils literal notranslate"><span class="pre">cut</span></code></a></li>
<li><a class="reference internal" href="#date"><code class="docutils literal notranslate"><span class="pre">date</span></code></a></li>
<li><a class="reference internal" href="#default"><code class="docutils literal notranslate"><span class="pre">default</span></code></a></li>
<li><a class="reference internal" href="#default-if-none"><code class="docutils literal notranslate"><span class="pre">default_if_none</span></code></a></li>
<li><a class="reference internal" href="#dictsort"><code class="docutils literal notranslate"><span class="pre">dictsort</span></code></a></li>
<li><a class="reference internal" href="#dictsortreversed"><code class="docutils literal notranslate"><span class="pre">dictsortreversed</span></code></a></li>
<li><a class="reference internal" href="#divisibleby"><code class="docutils literal notranslate"><span class="pre">divisibleby</span></code></a></li>
<li><a class="reference internal" href="#escape"><code class="docutils literal notranslate"><span class="pre">escape</span></code></a></li>
<li><a class="reference internal" href="#escapejs"><code class="docutils literal notranslate"><span class="pre">escapejs</span></code></a></li>
<li><a class="reference internal" href="#filesizeformat"><code class="docutils literal notranslate"><span class="pre">filesizeformat</span></code></a></li>
<li><a class="reference internal" href="#first"><code class="docutils literal notranslate"><span class="pre">first</span></code></a></li>
<li><a class="reference internal" href="#floatformat"><code class="docutils literal notranslate"><span class="pre">floatformat</span></code></a></li>
<li><a class="reference internal" href="#force-escape"><code class="docutils literal notranslate"><span class="pre">force_escape</span></code></a></li>
<li><a class="reference internal" href="#get-digit"><code class="docutils literal notranslate"><span class="pre">get_digit</span></code></a></li>
<li><a class="reference internal" href="#iriencode"><code class="docutils literal notranslate"><span class="pre">iriencode</span></code></a></li>
<li><a class="reference internal" href="#join"><code class="docutils literal notranslate"><span class="pre">join</span></code></a></li>
<li><a class="reference internal" href="#json-script"><code class="docutils literal notranslate"><span class="pre">json_script</span></code></a></li>
<li><a class="reference internal" href="#last"><code class="docutils literal notranslate"><span class="pre">last</span></code></a></li>
<li><a class="reference internal" href="#length"><code class="docutils literal notranslate"><span class="pre">length</span></code></a></li>
<li><a class="reference internal" href="#length-is"><code class="docutils literal notranslate"><span class="pre">length_is</span></code></a></li>
<li><a class="reference internal" href="#linebreaks"><code class="docutils literal notranslate"><span class="pre">linebreaks</span></code></a></li>
<li><a class="reference internal" href="#linebreaksbr"><code class="docutils literal notranslate"><span class="pre">linebreaksbr</span></code></a></li>
<li><a class="reference internal" href="#linenumbers"><code class="docutils literal notranslate"><span class="pre">linenumbers</span></code></a></li>
<li><a class="reference internal" href="#ljust"><code class="docutils literal notranslate"><span class="pre">ljust</span></code></a></li>
<li><a class="reference internal" href="#lower"><code class="docutils literal notranslate"><span class="pre">lower</span></code></a></li>
<li><a class="reference internal" href="#make-list"><code class="docutils literal notranslate"><span class="pre">make_list</span></code></a></li>
<li><a class="reference internal" href="#phone2numeric"><code class="docutils literal notranslate"><span class="pre">phone2numeric</span></code></a></li>
<li><a class="reference internal" href="#pluralize"><code class="docutils literal notranslate"><span class="pre">pluralize</span></code></a></li>
<li><a class="reference internal" href="#pprint"><code class="docutils literal notranslate"><span class="pre">pprint</span></code></a></li>
<li><a class="reference internal" href="#random"><code class="docutils literal notranslate"><span class="pre">random</span></code></a></li>
<li><a class="reference internal" href="#rjust"><code class="docutils literal notranslate"><span class="pre">rjust</span></code></a></li>
<li><a class="reference internal" href="#safe"><code class="docutils literal notranslate"><span class="pre">safe</span></code></a></li>
<li><a class="reference internal" href="#safeseq"><code class="docutils literal notranslate"><span class="pre">safeseq</span></code></a></li>
<li><a class="reference internal" href="#slice"><code class="docutils literal notranslate"><span class="pre">slice</span></code></a></li>
<li><a class="reference internal" href="#slugify"><code class="docutils literal notranslate"><span class="pre">slugify</span></code></a></li>
<li><a class="reference internal" href="#stringformat"><code class="docutils literal notranslate"><span class="pre">stringformat</span></code></a></li>
<li><a class="reference internal" href="#striptags"><code class="docutils literal notranslate"><span class="pre">striptags</span></code></a></li>
<li><a class="reference internal" href="#time"><code class="docutils literal notranslate"><span class="pre">time</span></code></a></li>
<li><a class="reference internal" href="#timesince"><code class="docutils literal notranslate"><span class="pre">timesince</span></code></a></li>
<li><a class="reference internal" href="#timeuntil"><code class="docutils literal notranslate"><span class="pre">timeuntil</span></code></a></li>
<li><a class="reference internal" href="#title"><code class="docutils literal notranslate"><span class="pre">title</span></code></a></li>
<li><a class="reference internal" href="#truncatechars"><code class="docutils literal notranslate"><span class="pre">truncatechars</span></code></a></li>
<li><a class="reference internal" href="#truncatechars-html"><code class="docutils literal notranslate"><span class="pre">truncatechars_html</span></code></a></li>
<li><a class="reference internal" href="#truncatewords"><code class="docutils literal notranslate"><span class="pre">truncatewords</span></code></a></li>
<li><a class="reference internal" href="#truncatewords-html"><code class="docutils literal notranslate"><span class="pre">truncatewords_html</span></code></a></li>
<li><a class="reference internal" href="#unordered-list"><code class="docutils literal notranslate"><span class="pre">unordered_list</span></code></a></li>
<li><a class="reference internal" href="#upper"><code class="docutils literal notranslate"><span class="pre">upper</span></code></a></li>
<li><a class="reference internal" href="#urlencode"><code class="docutils literal notranslate"><span class="pre">urlencode</span></code></a></li>
<li><a class="reference internal" href="#urlize"><code class="docutils literal notranslate"><span class="pre">urlize</span></code></a></li>
<li><a class="reference internal" href="#urlizetrunc"><code class="docutils literal notranslate"><span class="pre">urlizetrunc</span></code></a></li>
<li><a class="reference internal" href="#wordcount"><code class="docutils literal notranslate"><span class="pre">wordcount</span></code></a></li>
<li><a class="reference internal" href="#wordwrap"><code class="docutils literal notranslate"><span class="pre">wordwrap</span></code></a></li>
<li><a class="reference internal" href="#yesno"><code class="docutils literal notranslate"><span class="pre">yesno</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#internationalization-tags-and-filters">Balises et filtres d’internationalisation</a><ul>
<li><a class="reference internal" href="#i18n"><code class="docutils literal notranslate"><span class="pre">i18n</span></code></a></li>
<li><a class="reference internal" href="#l10n"><code class="docutils literal notranslate"><span class="pre">l10n</span></code></a></li>
<li><a class="reference internal" href="#tz"><code class="docutils literal notranslate"><span class="pre">tz</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-tags-and-filters-libraries">Autres bibliothèques de balises et de filtres</a><ul>
<li><a class="reference internal" href="#django-contrib-humanize"><code class="docutils literal notranslate"><span class="pre">django.contrib.humanize</span></code></a></li>
<li><a class="reference internal" href="#static"><code class="docutils literal notranslate"><span class="pre">static</span></code></a><ul>
<li><a class="reference internal" href="#std:templatetag-static"><code class="docutils literal notranslate"><span class="pre">static</span></code></a></li>
<li><a class="reference internal" href="#get-static-prefix"><code class="docutils literal notranslate"><span class="pre">get_static_prefix</span></code></a></li>
<li><a class="reference internal" href="#get-media-prefix"><code class="docutils literal notranslate"><span class="pre">get_media_prefix</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Sujet précédent</h4>
  <p class="topless"><a href="language.html"
                        title="Chapitre précédent">Le langage de gabarit de Django</a></p>
  <h4>Sujet suivant</h4>
  <p class="topless"><a href="api.html"
                        title="Chapitre suivant">Le langage de gabarit de Django&nbsp;: pour les programmeurs Python</a></p>
  <div role="note" aria-label="source link">
    <h3>Cette page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/templates/builtins.txt"
            rel="nofollow">Montrer le code source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Recherche rapide</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">avr. 06, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="language.html" title="Le langage de gabarit de Django">previous</a>
     |
    <a href="../index.html" title="Référence de l’API" accesskey="U">up</a>
   |
    <a href="api.html" title="Le langage de gabarit de Django&amp;nbsp;: pour les programmeurs Python">next</a> &raquo;</div>
    </div>
  </div>

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