<!DOCTYPE html>

<html lang="fr" data-content_root="../../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>Balises et filtres de gabarit intégrés &#8212; Documentation Django 6.0.7</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=34bb78ad" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css?v=bf4d74af" />
    <script src="../../_static/documentation_options.js?v=5190fde6"></script>
    <script src="../../_static/doctools.js?v=9bcbadda"></script>
    <script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../../_static/translations.js?v=e6b791cb"></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" />
  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Documentation Django 6.0.7</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 : 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">
            
  <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 vers cette rubrique">¶</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>
<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 vers cette rubrique">¶</a></h2>
<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 vers cette rubrique">¶</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>Exemple d’utilisation :</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>
<p>Lorsque l’échappement automatique est actif, tout contenu dérivé de variables est soumis à l’échappement HTML avant d’être affiché (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 «sûres» quant à l’échappement. Ce marquage pourrait avoir été appliqué par le code qui a défini la variable, par l’utilisation des filtres <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>, ou encore par le fait qu’il s’agit d’un résultat d’un filtre précédent ayant marqué la chaîne comme «sûre».</p>
<p>Dans le contexte de la désactivation de l’échappement automatique, le chaînage de filtres, y compris <a class="reference internal" href="#std-templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a>, peut aboutir à des résultats inattendus (mais documentés) comme dans l’exemple suivant :</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">my_list</span><span class="o">|</span><span class="nf">join</span><span class="s2">:&quot;, &quot;</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>
<p>Le code ci-dessus joindra les éléments de <code class="docutils literal notranslate"><span class="pre">my_list</span></code> sans échappement. La raison en est que la séquence de la chaîne de filtres exécute d’abord <a class="reference internal" href="#std-templatefilter-join"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">join</span></code></a> sur <code class="docutils literal notranslate"><span class="pre">my_list</span></code> (sans appliquer d’échappement à chacun de ses éléments car <code class="docutils literal notranslate"><span class="pre">autoescape</span></code> est désactivé (<code class="docutils literal notranslate"><span class="pre">off</span></code>)), et marque le résultat comme sûr. Ensuite, ce résultat sûr sera transmis au 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> qui n’applique pas d’échappement supplémentaire à du contenu considéré comme déjà sûr.</p>
<p>Afin d’échapper proprement chaque élément d’une séquence, utilisez le filtre <a class="reference internal" href="#std-templatefilter-escapeseq"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escapeseq</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">my_list</span><span class="o">|</span><span class="nf">escapeseq</span><span class="o">|</span><span class="nf">join</span><span class="s2">:&quot;, &quot;</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
</section>
<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 vers cette rubrique">¶</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</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>
</section>
<section id="s-csp-nonce-attr">
<span id="s-std-templatetag-csp_nonce_attr"></span><span id="csp-nonce-attr"></span><span id="std-templatetag-csp_nonce_attr"></span><h3><code class="docutils literal notranslate"><span class="pre">csp_nonce_attr</span></code><a class="headerlink" href="#csp-nonce-attr" title="Lien vers cette rubrique">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 6.1.</span> </div>
<p>When called without arguments, renders a <code class="docutils literal notranslate"><span class="pre">nonce=&quot;&lt;value&gt;&quot;</span></code> HTML attribute
when the <a class="reference internal" href="api.html#django.template.context_processors.csp" title="django.template.context_processors.csp"><code class="xref py py-func docutils literal notranslate"><span class="pre">csp()</span></code></a> context processor is
configured, and an empty string otherwise. Use it in <code class="docutils literal notranslate"><span class="pre">&lt;script&gt;</span></code> or
<code class="docutils literal notranslate"><span class="pre">&lt;link&gt;</span></code> elements to allow them under a nonce-based Content Security Policy:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;/path/to/script.js&quot;</span> <span class="cp">{%</span> <span class="k">csp_nonce_attr</span> <span class="cp">%}</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</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;/path/to/style.css&quot;</span> <span class="cp">{%</span> <span class="k">csp_nonce_attr</span> <span class="cp">%}</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>When called with a <a class="reference internal" href="../../topics/forms/media.html#django.forms.Media" title="django.forms.Media"><code class="xref py py-class docutils literal notranslate"><span class="pre">Media</span></code></a> object as the argument,
renders its assets with the CSP nonce applied to each <code class="docutils literal notranslate"><span class="pre">&lt;script&gt;</span></code> and
<code class="docutils literal notranslate"><span class="pre">&lt;link&gt;</span></code> element:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">csp_nonce_attr</span> <span class="nv">form.media</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>In both forms, if the context processor is not configured, assets are rendered
without a nonce attribute.</p>
<p>See <a class="reference internal" href="../csp.html#csp-nonce"><span class="std std-ref">Nonce usage</span></a> for full configuration details.</p>
</section>
<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 vers cette rubrique">¶</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 :</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 :</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>Variables included in the cycle will be escaped. You can disable auto-escaping
with:</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 :</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 « as », comme ceci :</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 :</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 :</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 :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Affiche un gros bloc d’informations de débogage, y compris le contexte actuel et les modules importés. <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">debug</span> <span class="pre">%}</span></code> n’affiche rien lorsque le réglage <a class="reference internal" href="../settings.html#std-setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> vaut <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Indique que ce gabarit étend un gabarit parent.</p>
<p>Cette balise peut être utilisée de deux façons :</p>
<ul class="simple">
<li><p><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.</p></li>
<li><p><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.</p></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 :</p>
<div class="highlight-text 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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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="admonition-title">Note</p>
<p>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>
</section>
<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 vers cette rubrique">¶</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 :</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 :</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 :</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 :</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>
</section>
<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 vers cette rubrique">¶</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 {% for obj in list reversed %}`.</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 :</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 :</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 :</p>
<table class="docutils">
<thead>
<tr class="row-odd"><th class="head"><p>Variable</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">forloop.counter</span></code></p></td>
<td><p>L’itération actuelle de la boucle (index à partir de 1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">forloop.counter0</span></code></p></td>
<td><p>L’itération actuelle de la boucle (index à partir de 0)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">forloop.revcounter</span></code></p></td>
<td><p>Le nombre d’itérations à partir de la fin de la boucle (index à partir de 1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">forloop.revcounter0</span></code></p></td>
<td><p>Le nombre d’itérations à partir de la fin de la boucle (index à partir de 0)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">forloop.first</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">True</span></code> si la boucle est dans sa première itération</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">forloop.last</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">True</span></code> si la boucle est dans sa dernière itération</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">forloop.length</span></code></p></td>
<td><p>The length of the loop</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">forloop.parentloop</span></code></p></td>
<td><p>Pour les boucles imbriquées, il s’agit de la boucle de niveau supérieur</p></td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<span class="title">Changed in Django 6.0:</span> <p>The variable <code class="docutils literal notranslate"><span class="pre">forloop.length</span></code> was added.</p>
</div>
</section>
<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 vers cette rubrique">¶</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 :</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 :</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>
</section>
<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 vers cette rubrique">¶</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é :</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>
<section id="s-boolean-operators">
<span id="boolean-operators"></span><h4>Opérateurs booléens<a class="headerlink" href="#boolean-operators" title="Lien vers cette rubrique">¶</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 :</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 :</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 :</p>
<div class="highlight-default 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><span class="p">:</span>
    <span class="o">...</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 :</p>
<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 vers cette rubrique">¶</a></h5>
<p>Égalité. Exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h5>
<p>Inégalité. Exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h5>
<p>Inférieur à. Exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h5>
<p>Supérieur à. Exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h5>
<p>Inférieur ou égal à. Exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h5>
<p>Supérieur ou égal à. Exemple :</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>
</section>
<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 vers cette rubrique">¶</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>
</section>
<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 vers cette rubrique">¶</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>
</section>
<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 vers cette rubrique">¶</a></h5>
<p>Identité d’objet. Teste si deux valeurs sont le même objet. Exemple :</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
</section>
<section id="s-filters">
<span id="filters"></span><h4>Filtres<a class="headerlink" href="#filters" title="Lien vers cette rubrique">¶</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 :</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>
</section>
<section id="s-complex-expressions">
<span id="complex-expressions"></span><h4>Expressions complexes<a class="headerlink" href="#complex-expressions" title="Lien vers cette rubrique">¶</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 :</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">or</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">and</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">not</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">in</span></code></p></li>
<li><p><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></p></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 :</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 :</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 :</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 :</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>
</section>
</section>
<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 vers cette rubrique">¶</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>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 :</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>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é :</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é :</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="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="cp">{%</span> <span class="k">else</span> <span class="cp">%}</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">    &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>
</section>
<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 vers cette rubrique">¶</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>The template name can either be a variable or a hardcoded (quoted) string,
in either single or double quotes.</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>Contexte : 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>Gabarit :</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>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 :</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 :</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="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 « effectuer le rendu de ce sous-gabarit et inclure le HTML résultant », et non pas « analyser ce sous-gabarit et inclure son contenu comme s’il faisait partie du parent ». 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>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>
</section>
<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 vers cette rubrique">¶</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Affiche du texte latin aléatoire « lorem ipsum ». C’est pratique pour produire des données d’exemple dans les gabarits.</p>
<p>Utilisation :</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 :</p>
<table class="docutils">
<thead>
<tr class="row-odd"><th class="head"><p>Paramètre</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">count</span></code></p></td>
<td><p>Un nombre (ou une variable) contenant le nombre de paragraphes ou de mots à produire (1 par défaut).</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">method</span></code></p></td>
<td><p>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).</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">random</span></code></p></td>
<td><p>Le mot <code class="docutils literal notranslate"><span class="pre">random</span></code>, quand il est indiqué, n’utilise pas le paragraphe connu (« Lorem ipsum dolor sit amet… ») lors de la production de texte.</p></td>
</tr>
</tbody>
</table>
<p>Exemples :</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">lorem</span> <span class="pre">%}</span></code> produit le paragraphe habituel « lorem ipsum ».</p></li>
<li><p><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>.</p></li>
<li><p><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.</p></li>
</ul>
</section>
<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 vers cette rubrique">¶</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 :</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 « brute ». Dans cet exemple, « f » et « o » 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 :</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 « It is the 4th of September ».</p>
<div class="admonition note">
<p class="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. :</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;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 :</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>
</section>
<section id="s-partial">
<span id="s-std-templatetag-partial"></span><span id="partial"></span><span id="std-templatetag-partial"></span><h3><code class="docutils literal notranslate"><span class="pre">partial</span></code><a class="headerlink" href="#partial" title="Lien vers cette rubrique">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 6.0.</span> </div>
<p>Renders a template fragment that was defined with <a class="reference internal" href="#std-templatetag-partialdef"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">partialdef</span></code></a>, inserting
the matching partial at this location.</p>
<p>Utilisation :</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">partial</span> <span class="nv">partial_name</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">partial_name</span></code> argument is the name of the template fragment to render.</p>
<p>In the following example, a partial named <code class="docutils literal notranslate"><span class="pre">button</span></code> is defined and then
rendered three times:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">partialdef</span> <span class="nv">button</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">button</span><span class="p">&gt;</span>Submit<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endpartialdef</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">partial</span> <span class="nv">button</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">partial</span> <span class="nv">button</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">partial</span> <span class="nv">button</span> <span class="cp">%}</span>
</pre></div>
</div>
</section>
<section id="s-partialdef">
<span id="s-std-templatetag-partialdef"></span><span id="partialdef"></span><span id="std-templatetag-partialdef"></span><h3><code class="docutils literal notranslate"><span class="pre">partialdef</span></code><a class="headerlink" href="#partialdef" title="Lien vers cette rubrique">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 6.0.</span> </div>
<p>Defines a reusable template fragment that can be rendered multiple times within
the same template or accessed directly via <a class="reference internal" href="language.html#template-partials-direct-access"><span class="std std-ref">template loading or inclusion</span></a>.</p>
<p>Utilisation :</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">partialdef</span> <span class="nv">partial_name</span> <span class="cp">%}</span>
    <span class="c">{# Reusable content. #}</span>
<span class="cp">{%</span> <span class="k">endpartialdef</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">partial_name</span></code> argument is required and must be a valid template
identifier.</p>
<p>In the following example, a new fragment named <code class="docutils literal notranslate"><span class="pre">card</span></code> is defined:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">partialdef</span> <span class="nv">card</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;card&quot;</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">h3</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">title</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h3</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="nv">content</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">div</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endpartialdef</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>This partial can then be rendered using the <a class="reference internal" href="#std-templatetag-partial"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">partial</span></code></a> tag:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">partial</span> <span class="nv">card</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">partial</span> <span class="nv">card</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>To <a class="reference internal" href="language.html#template-partials-inline"><span class="std std-ref">render a fragment immediately in place</span></a>,
use the <code class="docutils literal notranslate"><span class="pre">inline</span></code> option. The partial is still stored and can be reused later.</p>
</section>
<section id="s-querystring">
<span id="s-std-templatetag-querystring"></span><span id="querystring"></span><span id="std-templatetag-querystring"></span><h3><code class="docutils literal notranslate"><span class="pre">querystring</span></code><a class="headerlink" href="#querystring" title="Lien vers cette rubrique">¶</a></h3>
<p>Produit une chaîne de requête codée pour les URL sur la base des paramètres fournis.</p>
<p>This tag accepts positional arguments, which must be mappings (such as
<a class="reference internal" href="../request-response.html#django.http.QueryDict" title="django.http.QueryDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">QueryDict</span></code></a> or <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(disponible dans Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>). If no positional arguments
are provided, <a class="reference internal" href="../request-response.html#django.http.HttpRequest.GET" title="django.http.HttpRequest.GET"><code class="xref py py-attr docutils literal notranslate"><span class="pre">request.GET</span></code></a> is used as the
default to construct the query string.</p>
<p>Positional arguments are processed sequentially, while keyword arguments are
treated as key-value pairs, applied last. Later arguments take precedence over
earlier ones, ensuring the most recent pairs are reflected in the final result.</p>
<p>The result always includes a leading <code class="docutils literal notranslate"><span class="pre">&quot;?&quot;</span></code> since this tag is mainly used for
links, and an empty result could prevent the page from reloading as expected.</p>
<div class="admonition-using-request-get-as-default admonition">
<p class="admonition-title">Utilisation de <code class="docutils literal notranslate"><span class="pre">request.GET</span></code> comme valeur par défaut</p>
<p>Pour utiliser <code class="docutils literal notranslate"><span class="pre">request.GET</span></code> comme instance <code class="docutils literal notranslate"><span class="pre">QueryDict</span></code> par défaut, le processeur de contexte <code class="docutils literal notranslate"><span class="pre">django.template.context_processors.request</span></code> doit être actif. Sinon, vous devez soit passer explicitement un objet <code class="docutils literal notranslate"><span class="pre">request</span></code> dans le contexte du gabarit, soit fournir une instance <code class="docutils literal notranslate"><span class="pre">QueryDict</span></code> en appelant cette balise.</p>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 6.0:</span> <p>A <code class="docutils literal notranslate"><span class="pre">?</span></code> was prepended to the query string for empty results.</p>
</div>
<section id="s-basic-usage">
<span id="basic-usage"></span><h4>Utilisation de base<a class="headerlink" href="#basic-usage" title="Lien vers cette rubrique">¶</a></h4>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">querystring</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Outputs the current query string verbatim. So if the query string in the
request is <code class="docutils literal notranslate"><span class="pre">?color=green</span></code>, the output would be <code class="docutils literal notranslate"><span class="pre">?color=green</span></code>. If the
current query string is empty, the output will be <code class="docutils literal notranslate"><span class="pre">?</span></code>.</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">querystring</span> <span class="nv">size</span><span class="o">=</span><span class="s2">&quot;M&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Produit la chaîne de requête actuelle en y ajoutant le paramètre <code class="docutils literal notranslate"><span class="pre">size</span></code>. En reprenant l’exemple précédent, le résultat sera <code class="docutils literal notranslate"><span class="pre">?couleur=vert&amp;size=M</span></code>.</p>
</section>
<section id="s-setting-items">
<span id="setting-items"></span><h4>Définition d’éléments<a class="headerlink" href="#setting-items" title="Lien vers cette rubrique">¶</a></h4>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">querystring</span> <span class="nv">color</span><span class="o">=</span><span class="s2">&quot;red&quot;</span> <span class="nv">size</span><span class="o">=</span><span class="s2">&quot;S&quot;</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Ajoute ou modifie des paramètres dans la chaîne de requête. Chaque argument nommé sera ajouté à la chaîne de requête, remplaçant toute valeur éventuelle existante pour cette clé. Par exemple, si la chaîne de requête actuelle est <code class="docutils literal notranslate"><span class="pre">?color=green</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">?color=red&amp;size=S</span></code>.</p>
</section>
<section id="s-removing-items">
<span id="removing-items"></span><h4>Suppression d’éléments<a class="headerlink" href="#removing-items" title="Lien vers cette rubrique">¶</a></h4>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">querystring</span> <span class="nv">color</span><span class="o">=</span><span class="kp">None</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>En passant la valeur <code class="docutils literal notranslate"><span class="pre">None</span></code>, le paramètre en question est supprimé de la chaîne de requête. Par exemple, si la chaîne de requête actuelle est <code class="docutils literal notranslate"><span class="pre">?color=green&amp;size=M</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">?size=M</span></code>.</p>
</section>
<section id="s-handling-lists">
<span id="handling-lists"></span><h4>Comportement avec des listes<a class="headerlink" href="#handling-lists" title="Lien vers cette rubrique">¶</a></h4>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">querystring</span> <span class="nv">color</span><span class="o">=</span><span class="nv">my_list</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Si <code class="docutils literal notranslate"><span class="pre">my_list</span></code> est <code class="docutils literal notranslate"><span class="pre">[&quot;red&quot;,</span> <span class="pre">&quot;blue&quot;]</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">?color=red&amp;color=blue</span></code>, conservant la structure de la liste dans la chaîne de requête.</p>
</section>
<section id="s-customizing-the-base-querydict">
<span id="customizing-the-base-querydict"></span><h4>Customizing the base QueryDict<a class="headerlink" href="#customizing-the-base-querydict" title="Lien vers cette rubrique">¶</a></h4>
<p>You can pass custom <code class="docutils literal notranslate"><span class="pre">QueryDict</span></code> or <code class="docutils literal notranslate"><span class="pre">dict</span></code> instances as positional arguments
to replace <code class="docutils literal notranslate"><span class="pre">request.GET</span></code>. When multiple arguments are provided, key-value
pairs from later arguments take precedence over earlier ones.</p>
<p>For example, if <code class="docutils literal notranslate"><span class="pre">my_query_dict</span></code> is <code class="docutils literal notranslate"><span class="pre">&lt;QueryDict:</span> <span class="pre">{'color':</span> <span class="pre">['blue'],</span> <span class="pre">'size':</span>
<span class="pre">['S']}&gt;</span></code> and <code class="docutils literal notranslate"><span class="pre">my_dict</span></code> is <code class="docutils literal notranslate"><span class="pre">{'color':</span> <span class="pre">'orange',</span> <span class="pre">'fabric':</span> <span class="pre">'silk',</span> <span class="pre">'type':</span>
<span class="pre">'dress'}</span></code>, this outputs <code class="docutils literal notranslate"><span class="pre">?color=orange&amp;size=S&amp;fabric=silk</span></code>.</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">querystring</span> <span class="nv">my_query_dict</span> <span class="nv">my_dict</span> <span class="nv">size</span><span class="o">=</span><span class="s2">&quot;S&quot;</span> <span class="nv">type</span><span class="o">=</span><span class="kp">None</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>If all keys are removed by setting them to <code class="docutils literal notranslate"><span class="pre">None</span></code>, this outputs <code class="docutils literal notranslate"><span class="pre">?</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">querystring</span> <span class="nv">my_query_dict</span> <span class="nv">my_dict</span> <span class="nv">color</span><span class="o">=</span><span class="kp">None</span> <span class="nv">size</span><span class="o">=</span><span class="kp">None</span> <span class="nv">fabric</span><span class="o">=</span><span class="kp">None</span> <span class="nv">type</span><span class="o">=</span><span class="kp">None</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Similarly, if all positional arguments are empty and keyword arguments do not
contribute any new params, the output will also be <code class="docutils literal notranslate"><span class="pre">?</span></code>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 6.0:</span> <p>Support for multiple positional mapping arguments was added.</p>
</div>
</section>
<section id="s-dynamic-usage">
<span id="dynamic-usage"></span><h4>Utilisation dynamique<a class="headerlink" href="#dynamic-usage" title="Lien vers cette rubrique">¶</a></h4>
<p>Un cas d’utilisation fréquent de cette balise est de préserver la chaîne de requête actuelle lors de l’affichage d’une page de résultats, tout en ajoutant un lien vers les pages de résultats précédente et suivante. Par exemple, si la pagination est actuellement sur la page 3 et que la chaîne de requête actuelle est <code class="docutils literal notranslate"><span class="pre">?color=blue&amp;size=M&amp;page=3</span></code>, le code suivant produira <code class="docutils literal notranslate"><span class="pre">?color=blue&amp;size=M&amp;page=4</span></code>:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">querystring</span> <span class="nv">page</span><span class="o">=</span><span class="nv">page.next_page_number</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Il est également possible de stocker la valeur dans une variable. Par exemple, si vous avez besoin de plusieurs liens vers la même page, définissez la variable comme ceci :</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">querystring</span> <span class="nv">page</span><span class="o">=</span><span class="nv">page.next_page_number</span> <span class="k">as</span> <span class="nv">next_page</span> <span class="cp">%}</span>
</pre></div>
</div>
</section>
</section>
<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 vers cette rubrique">¶</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 : 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="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Mumbai&quot;</span><span class="p">,</span> <span class="s2">&quot;population&quot;</span><span class="p">:</span> <span class="s2">&quot;19,000,000&quot;</span><span class="p">,</span> <span class="s2">&quot;country&quot;</span><span class="p">:</span> <span class="s2">&quot;India&quot;</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Calcutta&quot;</span><span class="p">,</span> <span class="s2">&quot;population&quot;</span><span class="p">:</span> <span class="s2">&quot;15,000,000&quot;</span><span class="p">,</span> <span class="s2">&quot;country&quot;</span><span class="p">:</span> <span class="s2">&quot;India&quot;</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;New York&quot;</span><span class="p">,</span> <span class="s2">&quot;population&quot;</span><span class="p">:</span> <span class="s2">&quot;20,000,000&quot;</span><span class="p">,</span> <span class="s2">&quot;country&quot;</span><span class="p">:</span> <span class="s2">&quot;USA&quot;</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Chicago&quot;</span><span class="p">,</span> <span class="s2">&quot;population&quot;</span><span class="p">:</span> <span class="s2">&quot;7,000,000&quot;</span><span class="p">,</span> <span class="s2">&quot;country&quot;</span><span class="p">:</span> <span class="s2">&quot;USA&quot;</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Tokyo&quot;</span><span class="p">,</span> <span class="s2">&quot;population&quot;</span><span class="p">:</span> <span class="s2">&quot;33,000,000&quot;</span><span class="p">,</span> <span class="s2">&quot;country&quot;</span><span class="p">:</span> <span class="s2">&quot;Japan&quot;</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 :</p>
<ul class="simple">
<li><p>India</p>
<ul>
<li><p>Mumbai: 19,000,000</p></li>
<li><p>Calcutta: 15,000,000</p></li>
</ul>
</li>
<li><p>USA</p>
<ul>
<li><p>New York: 20,000,000</p></li>
<li><p>Chicago: 7,000,000</p></li>
</ul>
</li>
<li><p>Japan</p>
<ul>
<li><p>Tokyo: 33,000,000</p></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 :</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 : 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> produces a list (in this case, <code class="docutils literal notranslate"><span class="pre">country_list</span></code>) of
<strong>group objects</strong>. Group objects are instances of
<a class="reference external" href="https://docs.python.org/3/library/collections.html#collections.namedtuple" title="(disponible dans Python v3.14)"><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuple()</span></code></a> with two fields:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">grouper</span></code>, l’élément qui a servi au groupement (par ex. la chaîne « India » ou « Japan »).</p></li>
<li><p><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>).</p></li>
</ul>
<p>Because <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> produces <a class="reference external" href="https://docs.python.org/3/library/collections.html#collections.namedtuple" title="(disponible dans Python v3.14)"><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuple()</span></code></a> objects,
you can also write the previous example as:</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 ! 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) :</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="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Mumbai&quot;</span><span class="p">,</span> <span class="s2">&quot;population&quot;</span><span class="p">:</span> <span class="s2">&quot;19,000,000&quot;</span><span class="p">,</span> <span class="s2">&quot;country&quot;</span><span class="p">:</span> <span class="s2">&quot;India&quot;</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;New York&quot;</span><span class="p">,</span> <span class="s2">&quot;population&quot;</span><span class="p">:</span> <span class="s2">&quot;20,000,000&quot;</span><span class="p">,</span> <span class="s2">&quot;country&quot;</span><span class="p">:</span> <span class="s2">&quot;USA&quot;</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Calcutta&quot;</span><span class="p">,</span> <span class="s2">&quot;population&quot;</span><span class="p">:</span> <span class="s2">&quot;15,000,000&quot;</span><span class="p">,</span> <span class="s2">&quot;country&quot;</span><span class="p">:</span> <span class="s2">&quot;India&quot;</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Chicago&quot;</span><span class="p">,</span> <span class="s2">&quot;population&quot;</span><span class="p">:</span> <span class="s2">&quot;7,000,000&quot;</span><span class="p">,</span> <span class="s2">&quot;country&quot;</span><span class="p">:</span> <span class="s2">&quot;USA&quot;</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Tokyo&quot;</span><span class="p">,</span> <span class="s2">&quot;population&quot;</span><span class="p">:</span> <span class="s2">&quot;33,000,000&quot;</span><span class="p">,</span> <span class="s2">&quot;country&quot;</span><span class="p">:</span> <span class="s2">&quot;Japan&quot;</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 :</p>
<ul class="simple">
<li><p>India</p>
<ul>
<li><p>Mumbai: 19,000,000</p></li>
</ul>
</li>
<li><p>USA</p>
<ul>
<li><p>New York: 20,000,000</p></li>
</ul>
</li>
<li><p>India</p>
<ul>
<li><p>Calcutta: 15,000,000</p></li>
</ul>
</li>
<li><p>USA</p>
<ul>
<li><p>Chicago: 7,000,000</p></li>
</ul>
</li>
<li><p>Japan</p>
<ul>
<li><p>Tokyo: 33,000,000</p></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 :</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>
<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 vers cette rubrique">¶</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 « country » est une clé étrangère vers une classe possédant un attribut « description », vous pourriez écrire :</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 :</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>
</section>
</section>
<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 vers cette rubrique">¶</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 :</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 :</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>Gareth<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>Harry<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<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>Nick<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>John<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>Andrea<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>Melissa<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 :</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 « principale » toutes les cinq lignes. Seul le cycle des cinq lignes est réinitialisé lorsqu’une catégorie change.</p>
</section>
<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 vers cette rubrique">¶</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 :</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 :</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 :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Affiche l’une des séquences de caractères utilisées dans la syntaxe des balises de gabarit.</p>
<p>Le système des gabarits n’a aucun concept d’échappement de caractères individuels. Vous pouvez cependant utiliser la balise <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">templatetag</span> <span class="pre">%}</span></code> pour afficher l’une des combinaisons de caractères des balises de gabarit.</p>
<p>Le paramètre indique quelle séquence de gabarit vous voulez afficher :</p>
<table class="docutils">
<thead>
<tr class="row-odd"><th class="head"><p>Paramètre</p></th>
<th class="head"><p>Affiche</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">openblock</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{%</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">closeblock</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">%}</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">openvariable</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">closevariable</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">}}</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">openbrace</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">closebrace</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">}</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">opencomment</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{#</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">closecomment</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">#}</span></code></p></td>
</tr>
</tbody>
</table>
<p>Exemple d’utilisation :</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>The <span class="cp">{%</span> <span class="k">templatetag</span> <span class="nv">openblock</span> <span class="cp">%}</span> characters open a block.
</pre></div>
</div>
<p>Voir aussi la balise <a class="reference internal" href="#std-templatetag-verbatim"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">verbatim</span></code></a> pour une autre façon d’inclure ces caractères.</p>
</section>
<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 vers cette rubrique">¶</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>This is a way to output links without violating the DRY principle by having to
hardcode URLs in your templates:</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 :</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">path</span><span class="p">(</span><span class="s2">&quot;client/&lt;int:id&gt;/&quot;</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="s2">&quot;app-views-client&quot;</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 :</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">path</span><span class="p">(</span><span class="s2">&quot;clients/&quot;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s2">&quot;project_name.app_name.urls&quot;</span><span class="p">))</span>
</pre></div>
</div>
<p>…vous pouvez alors dans le gabarit créer un lien vers cette vue comme ceci :</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 :</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 :</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é :</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="admonition-title">Avertissement</p>
<p>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 !</p>
</div>
</section>
<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 vers cette rubrique">¶</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 :</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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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 ; 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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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 :</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="admonition-title">Note</p>
<p>L’ancien format plus verbeux est toujours pris en charge : <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>
</section>
</section>
<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 vers cette rubrique">¶</a></h2>
<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 vers cette rubrique">¶</a></h3>
<p>Ajoute le paramètre à la valeur.</p>
<p>Par exemple :</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 :</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="admonition-title">Avertissement</p>
<p>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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Centre la valeur dans un champ de largeur donnée.</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Enlève toutes les valeurs en paramètre de la chaîne à filtrer.</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Met en forme une date selon le format indiqué.</p>
<p>Utilise un format semblable à la fonction <a class="reference internal" href="#id6">date()</a> de PHP avec certaines différences.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>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 :</p>
<table class="docutils">
<thead>
<tr class="row-odd"><th class="head"><p>Caractère de format</p></th>
<th class="head"><p>Description</p></th>
<th class="head"><p>Exemple d’affichage</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><strong>Jour</strong></p></td>
<td></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">d</span></code></p></td>
<td><p>Jour du mois, 2 chiffres complétés si nécessaire par un zéro.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'01'</span></code> à <code class="docutils literal notranslate"><span class="pre">'31'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">j</span></code></p></td>
<td><p>Jour du mois sans zéros initiaux.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'1'</span></code> à <code class="docutils literal notranslate"><span class="pre">'31'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">D</span></code></p></td>
<td><p>Jour de la semaine, abréviation de 3 caractères.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'ven'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">l</span></code></p></td>
<td><p>Jour de la semaine, texte complet.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'vendredi'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">S</span></code></p></td>
<td><p>Suffixe ordinal anglais pour le jour du mois, sur 2 caractères.</p></td>
<td><p><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></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">w</span></code></p></td>
<td><p>Jour de la semaine, chiffre sans zéro initial.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'0'</span></code> (dimanche) à <code class="docutils literal notranslate"><span class="pre">'6'</span></code> (samedi)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">z</span></code></p></td>
<td><p>Jour de l’année.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">1</span></code> à <code class="docutils literal notranslate"><span class="pre">366</span></code></p></td>
</tr>
<tr class="row-even"><td><p><strong>Semaine</strong></p></td>
<td></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">W</span></code></p></td>
<td><p>Numéro de semaine dans l’année selon ISO-8601, les semaines commençant le lundi.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">1</span></code>, <code class="docutils literal notranslate"><span class="pre">53</span></code></p></td>
</tr>
<tr class="row-even"><td><p><strong>Mois</strong></p></td>
<td></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">m</span></code></p></td>
<td><p>Mois, 2 chiffres, avec zéro initial si nécessaire.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'01'</span></code> à <code class="docutils literal notranslate"><span class="pre">'12'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">n</span></code></p></td>
<td><p>Mois (chiffres) sans zéro initial.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'1'</span></code> à <code class="docutils literal notranslate"><span class="pre">'12'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">M</span></code></p></td>
<td><p>Mois, abréviation de 3 caractères.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'jan'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">b</span></code></p></td>
<td><p>Mois, abréviation de 3 caractères en minuscules.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'jan'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">E</span></code></p></td>
<td><p>Mois, représentation alternative spécifique à la langue active, habituellement utilisée pour les dates au format étendu.</p></td>
<td><p><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>)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">F</span></code></p></td>
<td><p>Mois au format texte long.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'janvier'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">N</span></code></p></td>
<td><p>Abréviation du mois dans le style Associated Press. Extension propriétaire.</p></td>
<td><p><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></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">t</span></code></p></td>
<td><p>Nombre de jours du mois indiqué.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">28</span></code> à <code class="docutils literal notranslate"><span class="pre">31</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><strong>Année</strong></p></td>
<td></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">y</span></code></p></td>
<td><p>Année, 2 chiffres avec zéros initiaux.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'00'</span></code> à <code class="docutils literal notranslate"><span class="pre">'99'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">Y</span></code></p></td>
<td><p>Année, 4 chiffres avec zéros initiaux.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'0001'</span></code>, …, <code class="docutils literal notranslate"><span class="pre">'1999'</span></code>, …, <code class="docutils literal notranslate"><span class="pre">'9999'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">L</span></code></p></td>
<td><p>Valeur booléenne indiquant s’il s’agit d’une année bissextile.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">True</span></code> ou <code class="docutils literal notranslate"><span class="pre">False</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">o</span></code></p></td>
<td><p>Année selon numération des semaines ISO-8601 (W) utilisant la semaine intercalaire. Voir Y pour le format d’année plus habituel.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'1999'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><strong>Heure</strong></p></td>
<td></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">g</span></code></p></td>
<td><p>Heure, format sur 12 heures sans zéros initiaux.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'1'</span></code> à <code class="docutils literal notranslate"><span class="pre">'12'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">G</span></code></p></td>
<td><p>Heure, format sur 24 heures sans zéros initiaux.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'0'</span></code> à <code class="docutils literal notranslate"><span class="pre">'23'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">h</span></code></p></td>
<td><p>Heure, format sur 12 heures.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'01'</span></code> à <code class="docutils literal notranslate"><span class="pre">'12'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">H</span></code></p></td>
<td><p>Heure, format sur 24 heures.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'00'</span></code> à <code class="docutils literal notranslate"><span class="pre">'23'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">i</span></code></p></td>
<td><p>Minutes.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'00'</span></code> à <code class="docutils literal notranslate"><span class="pre">'59'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">s</span></code></p></td>
<td><p>Secondes, 2 chiffres avec zéro initial.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'00'</span></code> à <code class="docutils literal notranslate"><span class="pre">'59'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">u</span></code></p></td>
<td><p>Microsecondes.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">000000</span></code> à <code class="docutils literal notranslate"><span class="pre">999999</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">a</span></code></p></td>
<td><p><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).</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'a.m.'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">A</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'AM'</span></code> ou <code class="docutils literal notranslate"><span class="pre">'PM'</span></code>.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'AM'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">f</span></code></p></td>
<td><p>Heure, en format heures et minutes sur 12 heures, sans affichage des minutes si elles valent 0. Extension propriétaire.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'1'</span></code>, <code class="docutils literal notranslate"><span class="pre">'1:30'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">P</span></code></p></td>
<td><p>Heure, au format sur 12 heures, minutes et « a.m. »/« p.m. », sans les minutes si elles valent 0 ; le cas échéant, les chaînes « minuit » et « midi » apparaissent. Extension propriétaire.</p></td>
<td><p><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></p></td>
</tr>
<tr class="row-even"><td><p><strong>Fuseau horaire</strong></p></td>
<td></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">e</span></code></p></td>
<td><p>Nom de fuseau horaire. Différents formats peuvent apparaître, ou même une chaîne vide, en fonction de la date/heure.</p></td>
<td><p><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.</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">I</span></code></p></td>
<td><p>Heure d’été, indication si elle est active ou non.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'1'</span></code> ou <code class="docutils literal notranslate"><span class="pre">'0'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">O</span></code></p></td>
<td><p>Différence en heures avec le temps de Greenwich.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'+0200'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">T</span></code></p></td>
<td><p>Fuseau horaire de la machine.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'EST'</span></code>, <code class="docutils literal notranslate"><span class="pre">'MDT'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">Z</span></code></p></td>
<td><p>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.</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">-43200</span></code> à <code class="docutils literal notranslate"><span class="pre">43200</span></code></p></td>
</tr>
<tr class="row-even"><td><p><strong>Date/heure</strong></p></td>
<td></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">c</span></code></p></td>
<td><p>Format ISO 8601 (note : contrairement à d’autres formats comme « Z », « O » ou « r », le format « c » n’ajoute pas le décalage de fuseau horaire si la valeur est une date/heure naïve (voir <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.tzinfo" title="(disponible dans Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.tzinfo</span></code></a>).</p></td>
<td><p><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</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">r</span></code></p></td>
<td><p>Date mise en forme selon <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html#section-3.3"><strong>RFC 5322</strong></a>.</p></td>
<td><p><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></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">U</span></code></p></td>
<td><p>Secondes depuis le temps Unix initial (1er janvier 1970 00:00:00 UTC).</p></td>
<td></td>
</tr>
</tbody>
</table>
<p>Par exemple :</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>If <code class="docutils literal notranslate"><span class="pre">value</span></code> is a <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(disponible dans Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object (e.g., the result of
<code class="docutils literal notranslate"><span class="pre">datetime.datetime.now()</span></code>), the output will be the string
<code class="docutils literal notranslate"><span class="pre">'Wed</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>En supposant 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;es&quot;</span></code>, alors pour :</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 :</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. :</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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 :</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;zed&quot;</span><span class="p">,</span> <span class="s2">&quot;age&quot;</span><span class="p">:</span> <span class="mi">19</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;amy&quot;</span><span class="p">,</span> <span class="s2">&quot;age&quot;</span><span class="p">:</span> <span class="mi">22</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;joe&quot;</span><span class="p">,</span> <span class="s2">&quot;age&quot;</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 :</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;amy&quot;</span><span class="p">,</span> <span class="s2">&quot;age&quot;</span><span class="p">:</span> <span class="mi">22</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;joe&quot;</span><span class="p">,</span> <span class="s2">&quot;age&quot;</span><span class="p">:</span> <span class="mi">31</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;zed&quot;</span><span class="p">,</span> <span class="s2">&quot;age&quot;</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 :</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 :</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="p">{</span><span class="s2">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;1984&quot;</span><span class="p">,</span> <span class="s2">&quot;author&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;George&quot;</span><span class="p">,</span> <span class="s2">&quot;age&quot;</span><span class="p">:</span> <span class="mi">45</span><span class="p">}},</span>
    <span class="p">{</span><span class="s2">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Timequake&quot;</span><span class="p">,</span> <span class="s2">&quot;author&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Kurt&quot;</span><span class="p">,</span> <span class="s2">&quot;age&quot;</span><span class="p">:</span> <span class="mi">75</span><span class="p">}},</span>
    <span class="p">{</span><span class="s2">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Alice&quot;</span><span class="p">,</span> <span class="s2">&quot;author&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Lewis&quot;</span><span class="p">,</span> <span class="s2">&quot;age&quot;</span><span class="p">:</span> <span class="mi">33</span><span class="p">}},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>le résultat sera :</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 :</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 :</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;42&quot;</span><span class="p">),</span>
    <span class="p">(</span><span class="s2">&quot;c&quot;</span><span class="p">,</span> <span class="s2">&quot;string&quot;</span><span class="p">),</span>
    <span class="p">(</span><span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="s2">&quot;foo&quot;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>le résultat sera :</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span>
    <span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;42&quot;</span><span class="p">),</span>
    <span class="p">(</span><span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="s2">&quot;foo&quot;</span><span class="p">),</span>
    <span class="p">(</span><span class="s2">&quot;c&quot;</span><span class="p">,</span> <span class="s2">&quot;string&quot;</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 :</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>
<p>Le tri par éléments à un indice donné n’est pas pris en charge pour les dictionnaires.</p>
</section>
<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 vers cette rubrique">¶</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Échappe du code HTML. Plus particulièrement, il effectue les remplacements suivants :</p>
<ul class="simple">
<li><p><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></p></li>
<li><p><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></p></li>
<li><p><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></p></li>
<li><p><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></p></li>
<li><p><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></p></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 :</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 class="admonition-chaining-escape-with-other-filters admonition">
<p class="admonition-title">Chaînage de <code class="docutils literal notranslate"><span class="pre">escape</span></code> avec d’autres filtres</p>
<p>Comme mentionné dans la section <a class="reference internal" href="#std-templatetag-autoescape"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">autoescape</span></code></a>, lorsque des filtres incluant <code class="docutils literal notranslate"><span class="pre">escape</span></code> sont enchaînés, le résultat peut être inattendu si des filtres précédents marquent une chaîne potentiellement non sûre comme sûre en raison d’un manque d’échappement causé par le fait qu”<a class="reference internal" href="#std-templatetag-autoescape"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">autoescape</span></code></a> était désactivé (<code class="docutils literal notranslate"><span class="pre">off</span></code>).</p>
<p>Dans de tels cas, le chaînage avec <code class="docutils literal notranslate"><span class="pre">escape</span></code> ne va pas échapper à nouveau des chaînes qui auraient déjà été marquées comme sûres.</p>
<p>Ceci est particulièrement important lors d’utilisation de filtres qui agissent sur des séquences, par exemple <a class="reference internal" href="#std-templatefilter-join"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">join</span></code></a>. Si vous avez besoin d’échapper chaque élément d’une séquence, utilisez le filtre dédié <a class="reference internal" href="#std-templatefilter-escapeseq"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escapeseq</span></code></a>.</p>
</div>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Échappe les caractères à utiliser en tant que chaîne littérale JavaScript, dans des guillemets simples ou doubles, comme ci-dessous. Ce filtre ne rend pas la chaîne sûre pour être utilisée dans des <em>«littéraux de gabarits JavaScript»</em> (la syntaxe JavaScript avec l’apostrophe inversée ou backtick). Aucun autre utilisation que celle mentionnée ci-dessus n’est prise en charge. Il est généralement recommandé de transmettre les données à l’aide des attributs HTML <code class="docutils literal notranslate"><span class="pre">data-</span></code> ou par le filtre <a class="reference internal" href="#std-templatefilter-json_script"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">json_script</span></code></a>, plutôt que de l’intégrer dans du JavaScript.</p>
<p>Par exemple :</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">myValue</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">&#39;</span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">escapejs</span> <span class="cp">}}</span><span class="s1">&#39;</span>
</pre></div>
</div>
</section>
<section id="s-escapeseq">
<span id="s-std-templatefilter-escapeseq"></span><span id="escapeseq"></span><span id="std-templatefilter-escapeseq"></span><h3><code class="docutils literal notranslate"><span class="pre">escapeseq</span></code><a class="headerlink" href="#escapeseq" title="Lien vers cette rubrique">¶</a></h3>
<p>Applique 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> à 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 :</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">my_list</span><span class="o">|</span><span class="nf">escapeseq</span><span class="o">|</span><span class="nf">join</span><span class="s2">:&quot;, &quot;</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
</section>
<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 vers cette rubrique">¶</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 :</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="admonition-title">Tailles de fichiers et unités SI</p>
<p>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 : la traduction en français utilise les préfixes SI Kio, Mio, etc.).</p>
</div>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Renvoie le premier élément de la liste.</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</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 :</p>
<table class="docutils">
<thead>
<tr class="row-odd"><th class="head"><p><code class="docutils literal notranslate"><span class="pre">valeur</span></code></p></th>
<th class="head"><p>Gabarit</p></th>
<th class="head"><p>Affichage</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,2</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">34.26000</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,3</span></code></p></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 :</p>
<table class="docutils">
<thead>
<tr class="row-odd"><th class="head"><p><code class="docutils literal notranslate"><span class="pre">valeur</span></code></p></th>
<th class="head"><p>Gabarit</p></th>
<th class="head"><p>Affichage</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:3</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,232</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:3</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,000</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">34.26000</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:3</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,260</span></code></p></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">
<thead>
<tr class="row-odd"><th class="head"><p><code class="docutils literal notranslate"><span class="pre">valeur</span></code></p></th>
<th class="head"><p>Gabarit</p></th>
<th class="head"><p>Affichage</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;0&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;0&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">39.56000</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;0&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">40</span></code></p></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 :</p>
<table class="docutils">
<thead>
<tr class="row-odd"><th class="head"><p><code class="docutils literal notranslate"><span class="pre">valeur</span></code></p></th>
<th class="head"><p>Gabarit</p></th>
<th class="head"><p>Affichage</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;-3&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,232</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;-3&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">34.26000</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">valeur|floatformat:&quot;-3&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,260</span></code></p></td>
</tr>
</tbody>
</table>
<p>Si l’argument transmis à <code class="docutils literal notranslate"><span class="pre">floatformat</span></code> possède le suffixe <code class="docutils literal notranslate"><span class="pre">g</span></code>, cela forcera le groupement par <a class="reference internal" href="../settings.html#std-setting-THOUSAND_SEPARATOR"><code class="xref std std-setting docutils literal notranslate"><span class="pre">THOUSAND_SEPARATOR</span></code></a> pour la locale active. Par exemple, lorsque la locale active est <code class="docutils literal notranslate"><span class="pre">en</span></code> (anglais) :</p>
<table class="docutils">
<thead>
<tr class="row-odd"><th class="head"><p><code class="docutils literal notranslate"><span class="pre">valeur</span></code></p></th>
<th class="head"><p>Gabarit</p></th>
<th class="head"><p>Affichage</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">34232.34</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:&quot;2g&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,232.34</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">34232.06</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:&quot;g&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,232.1</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">34232.00</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:&quot;-3g&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,232</span></code></p></td>
</tr>
</tbody>
</table>
<p>Le résultat est toujours régionalisé (indépendamment de la balise <a class="reference internal" href="../../topics/i18n/formatting.html#std-templatetag-localize"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">{%</span> <span class="pre">localize</span> <span class="pre">off</span> <span class="pre">%}</span></code></a>) sauf si le paramètre transmis à <code class="docutils literal notranslate"><span class="pre">floatformat</span></code> possède le suffixe <code class="docutils literal notranslate"><span class="pre">u</span></code>, ce qui force la désactivation de la régionalisation. Par exemple, lorsque la langue active est <code class="docutils literal notranslate"><span class="pre">pl</span></code> (polonais) :</p>
<table class="docutils">
<thead>
<tr class="row-odd"><th class="head"><p><code class="docutils literal notranslate"><span class="pre">valeur</span></code></p></th>
<th class="head"><p>Gabarit</p></th>
<th class="head"><p>Affichage</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:&quot;3&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,232</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:&quot;3u&quot;</span> <span class="pre">}}</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">34,232</span></code></p></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>
</section>
<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 vers cette rubrique">¶</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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=I</span> <span class="pre">♥</span> <span class="pre">Django&quot;</span></code>, le résultat sera <code class="docutils literal notranslate"><span class="pre">&quot;?test=I%20%E2%99%A5%20Django&quot;</span></code>.</p>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</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> : l’attribut HTML « id » facultatif de la balise <code class="docutils literal notranslate"><span class="pre">&lt;script&gt;</span></code>.</p>
<p>Par exemple :</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="w"> </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="w"> </span><span class="nx">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">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="w"> </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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Renvoie le dernier élément de la liste.</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Renvoie la longueur de la valeur. Cela fonctionne aussi bien pour du texte que des listes.</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Remplace les sauts de ligne dans du texte brut par le code HTML approprié ; 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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Affiche un texte en numérotant ses lignes.</p>
<p>Par exemple :</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 :</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>one
two
three
</pre></div>
</div>
<p>le résultat sera :</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>1. one
2. two
3. three
</pre></div>
</div>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Aligne à gauche la valeur dans un champ de largeur donnée.</p>
<p><strong>Paramètre :</strong> taille de champ</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Convertit une chaîne tout en minuscules.</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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 :</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 :</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="admonition-title">Note</p>
<p>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>
</section>
<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 vers cette rubrique">¶</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.14)"><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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Renvoie un élément aléatoire dans la liste indiquée.</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Aligne à droite la valeur dans un champ de largeur donnée.</p>
<p><strong>Paramètre :</strong> taille de champ</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</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="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 :</p>
<div class="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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Renvoie un segment de la liste.</p>
<p>Utilise la même syntaxe que la segmentation des listes Python. Voir la <a class="reference external" href="https://docs.python.org/3/tutorial/introduction.html#lists">documentation de Python</a> pour une introduction.</p>
<p>Exemple :</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</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.14)"><span>printf-style String Formatting</span></a>, à l’exclusion du caractère « % » initial.</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Fait tout son possible pour supprimer toutes les balises [X]HTML.</p>
<p>Par exemple :</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="admonition-title">Aucune garantie de sécurité</p>
<p>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, recherchez un outil tiers de nettoyage de HTML.</p>
</div>
</section>
<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 vers cette rubrique">¶</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 :</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 « brute ». Dans cet exemple, « h » et « m » 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 :</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\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 :</p>
<p>En supposant 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 :</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>The <code class="docutils literal notranslate"><span class="pre">time</span></code> filter will only accept parameters in the format string that
relate to the time of day, not the date. If you need to format a <code class="docutils literal notranslate"><span class="pre">date</span></code>
value, use the <a class="reference internal" href="#std-templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> filter instead (or along with <a class="reference internal" href="#std-templatefilter-time"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">time</span></code></a> if
you need to render a full <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(disponible dans Python v3.14)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> value).</p>
<p>Il existe une exception à la règle ci-dessus : 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é :</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 à :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Met en forme une date de manière relative à cette date (par ex. « 4 jours, 6 heures »).</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 « 8 heures »:</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 « 0 minute » est renvoyé pour toute date dans le futur en fonction du point de comparaison.</p>
</section>
<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 vers cette rubrique">¶</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 « 4 semaines ».</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 « 1 semaine » :</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>
</section>
<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 vers cette rubrique">¶</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 « utilitaires ».</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</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 (« … »).</p>
<p><strong>Paramètre :</strong> le nombre maximum de caractères du résultat</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</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 :</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 class="admonition-size-of-input-string admonition">
<p class="admonition-title">Taille de la chaîne d’entrée</p>
<p>Processing large, potentially malformed HTML strings can be
resource-intensive and impact service performance.</p>
</div>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Tronque une chaîne après un certain nombre de mots.</p>
<p><strong>Paramètre :</strong> le nombre maximum de mots du résultat</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</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 :</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 class="admonition-size-of-input-string admonition">
<p class="admonition-title">Taille de la chaîne d’entrée</p>
<p>Processing large, potentially malformed HTML strings can be
resource-intensive and impact service performance.</p>
</div>
</section>
<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 vers cette rubrique">¶</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 <code class="docutils literal notranslate"><span class="pre">&lt;ul&gt;</span></code> 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 :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Convertit une chaîne tout en majuscules.</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Échappe une valeur pour être incluse dans une URL.</p>
<p>Par exemple :</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 « / » 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 :</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>
</section>
<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 vers cette rubrique">¶</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://djangocon.eu</span></code> sera converti mais  <code class="docutils literal notranslate"><span class="pre">djangocon.eu</span></code> ne le sera pas.</p>
<p>Il convertit également les liens « noms de domaine » 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) ; <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 :</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>If <code class="docutils literal notranslate"><span class="pre">value</span></code> is <code class="docutils literal notranslate"><span class="pre">&quot;Check</span> <span class="pre">out</span> <span class="pre">www.djangoproject.com&quot;</span></code>, the output will be:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>Check out <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;http://www.djangoproject.com&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;nofollow&quot;</span><span class="p">&gt;</span>www.djangoproject.com<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</pre></div>
</div>
<div class="deprecated">
<p><span class="versionmodified deprecated">Obsolète depuis la version 6.0: </span>The default protocol when none is provided will change from HTTP to HTTPS
in Django 7.0. Hence, the output will become:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>Check out <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;https://www.djangoproject.com&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;nofollow&quot;</span><span class="p">&gt;</span>www.djangoproject.com<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Set the transitional setting <a class="reference internal" href="../settings.html#std-setting-URLIZE_ASSUME_HTTPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">URLIZE_ASSUME_HTTPS</span></code></a> to <code class="docutils literal notranslate"><span class="pre">True</span></code> to
opt into using HTTPS during the Django 6.x release cycle.</p>
</div>
<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="admonition-title">Note</p>
<p>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>
</section>
<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 vers cette rubrique">¶</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 :</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 :</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>If <code class="docutils literal notranslate"><span class="pre">value</span></code> is <code class="docutils literal notranslate"><span class="pre">&quot;Check</span> <span class="pre">out</span> <span class="pre">www.djangoproject.com&quot;</span></code>, the output would be:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>Check out <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;http://www.djangoproject.com&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;nofollow&quot;</span><span class="p">&gt;</span>www.djangoproj…<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</pre></div>
</div>
<div class="deprecated">
<p><span class="versionmodified deprecated">Obsolète depuis la version 6.0: </span>The default protocol when none is provided will change from HTTP to HTTPS
in Django 7.0. Hence, the output will become:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>Check out <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;https://www.djangoproject.com&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;nofollow&quot;</span><span class="p">&gt;</span>www.djangoproj…<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Set the transitional setting <a class="reference internal" href="../settings.html#std-setting-URLIZE_ASSUME_HTTPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">URLIZE_ASSUME_HTTPS</span></code></a> to <code class="docutils literal notranslate"><span class="pre">True</span></code> to
opt into using HTTPS during the Django 6.x release cycle.</p>
</div>
<p>Comme pour <a class="reference internal" href="#urlize">urlize</a>, ce filtre ne doit être appliqué qu’à du texte pur.</p>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Renvoie le nombre de mots.</p>
<p>Par exemple :</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>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Ajoute des sauts de ligne d’après une longueur de ligne maximale.</p>
<p><strong>Paramètre :</strong> le nombre de caractères après lequel un saut de ligne est rajouté dans le texte.</p>
<p>Par exemple :</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 :</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span>Joel
is a
slug
</pre></div>
</div>
</section>
<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 vers cette rubrique">¶</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 « yes », « no », « maybe », ou à une liste personnalisée de chaînes séparées par des virgules, et renvoie la chaîne correspondant à la valeur transmise :</p>
<p>Par exemple :</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">
<thead>
<tr class="row-odd"><th class="head"><p>Valeur</p></th>
<th class="head"><p>Paramètre</p></th>
<th class="head"><p>Affiche</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">True</span></code></p></td>
<td></td>
<td><p><code class="docutils literal notranslate"><span class="pre">yes</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">True</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">&quot;yeah,no,maybe&quot;</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">yeah</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">False</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">&quot;yeah,no,maybe&quot;</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">no</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">&quot;yeah,no,maybe&quot;</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">maybe</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">&quot;yeah,no&quot;</span></code></p></td>
<td><p><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>)</p></td>
</tr>
</tbody>
</table>
</section>
</section>
<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 vers cette rubrique">¶</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>
<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 vers cette rubrique">¶</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 : dans le code des gabarits</span></a>.</p>
</section>
<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 vers cette rubrique">¶</a></h3>
<p>Cette bibliothèque donne le contrôle sur la régionalisation des valeurs dans les gabarits. Vous devez seulement charger la bibliothèque en écrivant <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">l10n</span> <span class="pre">%}</span></code>.</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>
</section>
<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 vers cette rubrique">¶</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>
</section>
</section>
<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 vers cette rubrique">¶</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>
<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 vers cette rubrique">¶</a></h3>
<p>Un ensemble de filtres de gabarit utiles pour ajouter une touche « humaine » aux données. Voir <a class="reference internal" href="../contrib/humanize.html"><span class="doc">django.contrib.humanize</span></a>.</p>
</section>
<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 vers cette rubrique">¶</a></h3>
<section id="s-std-templatetag-static">
<span id="s-id7"></span><span id="std-templatetag-static"></span><span id="id7"></span><h4><code class="docutils literal notranslate"><span class="pre">static</span></code><a class="headerlink" href="#std-templatetag-static" title="Lien vers cette rubrique">¶</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 pointé par <code class="docutils literal notranslate"><span class="pre">staticfiles</span></code> dans <a class="reference internal" href="../settings.html#std-setting-STORAGES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STORAGES</span></code></a>. Par exemple :</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 ; par exemple en supposant qu’une variable <code class="docutils literal notranslate"><span class="pre">user_stylesheet</span></code> est transmise au gabarit :</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">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 :</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="na">alt</span><span class="o">=</span><span class="s">&quot;Hi!&quot;</span><span class="p">&gt;</span>
</pre></div>
</div>
<div class="admonition-using-jinja2-templates admonition">
<p class="admonition-title">Utilisation de gabarits Jinja2</p>
<p>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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
<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 vers cette rubrique">¶</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 :</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>
</section>
</section>
</section>
</section>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3><a href="../../contents.html">Table des matières</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="#csp-nonce-attr"><code class="docutils literal notranslate"><span class="pre">csp_nonce_attr</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="#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="#partial"><code class="docutils literal notranslate"><span class="pre">partial</span></code></a></li>
<li><a class="reference internal" href="#partialdef"><code class="docutils literal notranslate"><span class="pre">partialdef</span></code></a></li>
<li><a class="reference internal" href="#querystring"><code class="docutils literal notranslate"><span class="pre">querystring</span></code></a><ul>
<li><a class="reference internal" href="#basic-usage">Utilisation de base</a></li>
<li><a class="reference internal" href="#setting-items">Définition d’éléments</a></li>
<li><a class="reference internal" href="#removing-items">Suppression d’éléments</a></li>
<li><a class="reference internal" href="#handling-lists">Comportement avec des listes</a></li>
<li><a class="reference internal" href="#customizing-the-base-querydict">Customizing the base QueryDict</a></li>
<li><a class="reference internal" href="#dynamic-usage">Utilisation dynamique</a></li>
</ul>
</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="#escapeseq"><code class="docutils literal notranslate"><span class="pre">escapeseq</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="#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>

  </div>
  <div>
    <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>
  </div>
  <div>
    <h4>Sujet suivant</h4>
    <p class="topless"><a href="api.html"
                          title="Chapitre suivant">Le langage de gabarit de Django : pour les programmeurs Python</a></p>
  </div>
  <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>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Recherche rapide</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">août 05, 2026</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 : pour les programmeurs Python">next</a> &raquo;</div>
    </div>
  </div>

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