Sophie

Sophie

distrib > Mageia > 7 > aarch64 > by-pkgid > 481c2de1450e70fa8fdc1e3abf72606b > files > 712

python-django-doc-1.11.20-1.mga7.noarch.rpm


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Overriding templates &#8212; Django 1.11.20 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Managing static files (e.g. images, JavaScript, CSS)" href="static-files/index.html" />
    <link rel="prev" title="Outputting PDFs with Django" href="outputting-pdf.html" />



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


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 1.11.20 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="outputting-pdf.html" title="Outputting PDFs with Django">previous</a>
     |
    <a href="index.html" title="“How-to” guides" accesskey="U">up</a>
   |
    <a href="static-files/index.html" title="Managing static files (e.g. images, JavaScript, CSS)">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="howto-overriding-templates">
            
  <div class="section" id="s-overriding-templates">
<span id="overriding-templates"></span><h1>Overriding templates<a class="headerlink" href="#overriding-templates" title="Permalink to this headline">¶</a></h1>
<p>In your project, you might want to override a template in another Django
application, whether it be a third-party application or a contrib application
such as <code class="docutils literal notranslate"><span class="pre">django.contrib.admin</span></code>. You can either put template overrides in your
project’s templates directory or in an application’s templates directory.</p>
<p>If you have app and project templates directories that both contain overrides,
the default Django template loader will try to load the template from the
project-level directory first. In other words, <a class="reference internal" href="../ref/settings.html#std:setting-TEMPLATES-DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code></a>
is searched before <a class="reference internal" href="../ref/settings.html#std:setting-TEMPLATES-APP_DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">APP_DIRS</span></code></a>.</p>
<div class="section" id="s-overriding-from-the-project-s-templates-directory">
<span id="overriding-from-the-project-s-templates-directory"></span><h2>Overriding from the project’s templates directory<a class="headerlink" href="#overriding-from-the-project-s-templates-directory" title="Permalink to this headline">¶</a></h2>
<p>First, we’ll explore overriding templates by creating replacement templates in
your project’s templates directory.</p>
<p>Let’s say you’re trying to override the templates for a third-party application
called <code class="docutils literal notranslate"><span class="pre">blog</span></code>, which provides the templates <code class="docutils literal notranslate"><span class="pre">blog/post.html</span></code> and
<code class="docutils literal notranslate"><span class="pre">blog/list.html</span></code>. The relevant settings for your project would look like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>

<span class="n">BASE_DIR</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)))</span>

<span class="n">INSTALLED_APPS</span> <span class="o">=</span> <span class="p">[</span>
    <span class="o">...</span><span class="p">,</span>
    <span class="s1">&#39;blog&#39;</span><span class="p">,</span>
    <span class="o">...</span><span class="p">,</span>
<span class="p">]</span>

<span class="n">TEMPLATES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span>
        <span class="s1">&#39;BACKEND&#39;</span><span class="p">:</span> <span class="s1">&#39;django.template.backends.django.DjangoTemplates&#39;</span><span class="p">,</span>
        <span class="s1">&#39;DIRS&#39;</span><span class="p">:</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">BASE_DIR</span><span class="p">,</span> <span class="s1">&#39;templates&#39;</span><span class="p">)],</span>
        <span class="s1">&#39;APP_DIRS&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
        <span class="o">...</span>
    <span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../ref/settings.html#std:setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> setting and <code class="docutils literal notranslate"><span class="pre">BASE_DIR</span></code> will already exist if you
created your project using the default project template. The setting that needs
to be modified is <a class="reference internal" href="../ref/settings.html#std:setting-TEMPLATES-DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code></a>.</p>
<p>These settings assume you have a <code class="docutils literal notranslate"><span class="pre">templates</span></code> directory in the root of your
project. To override the templates for the <code class="docutils literal notranslate"><span class="pre">blog</span></code> app, create a folder
in the <code class="docutils literal notranslate"><span class="pre">templates</span></code> directory, and add the template files to that folder:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>templates/
    blog/
        list.html
        post.html
</pre></div>
</div>
<p>The template loader first looks for templates in the <code class="docutils literal notranslate"><span class="pre">DIRS</span></code> directory. When
the views in the <code class="docutils literal notranslate"><span class="pre">blog</span></code> app ask for the <code class="docutils literal notranslate"><span class="pre">blog/post.html</span></code> and
<code class="docutils literal notranslate"><span class="pre">blog/list.html</span></code> templates, the loader will return the files you just created.</p>
</div>
<div class="section" id="s-overriding-from-an-app-s-template-directory">
<span id="overriding-from-an-app-s-template-directory"></span><h2>Overriding from an app’s template directory<a class="headerlink" href="#overriding-from-an-app-s-template-directory" title="Permalink to this headline">¶</a></h2>
<p>Since you’re overriding templates located outside of one of your project’s
apps, it’s more common to use the first method and put template overrides in a
project’s templates folder. If you prefer, however, it’s also possible to put
the overrides in an app’s template directory.</p>
<p>First, make sure your template settings are checking inside app directories:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">TEMPLATES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span>
        <span class="o">...</span><span class="p">,</span>
        <span class="s1">&#39;APP_DIRS&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
        <span class="o">...</span>
    <span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>If you want to put the template overrides in an app called <code class="docutils literal notranslate"><span class="pre">myapp</span></code> and the
templates to override are named <code class="docutils literal notranslate"><span class="pre">blog/list.html</span></code> and <code class="docutils literal notranslate"><span class="pre">blog/post.html</span></code>,
then your directory structure will look like:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>myapp/
    templates/
        blog/
            list.html
            post.html
</pre></div>
</div>
<p>With <a class="reference internal" href="../ref/settings.html#std:setting-TEMPLATES-APP_DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">APP_DIRS</span></code></a> set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, the template
loader will look in the app’s templates directory and find the templates.</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Overriding templates</a><ul>
<li><a class="reference internal" href="#overriding-from-the-project-s-templates-directory">Overriding from the project’s templates directory</a></li>
<li><a class="reference internal" href="#overriding-from-an-app-s-template-directory">Overriding from an app’s template directory</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="outputting-pdf.html"
                        title="previous chapter">Outputting PDFs with Django</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="static-files/index.html"
                        title="next chapter">Managing static files (e.g. images, JavaScript, CSS)</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/howto/overriding-templates.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Feb 11, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="outputting-pdf.html" title="Outputting PDFs with Django">previous</a>
     |
    <a href="index.html" title="“How-to” guides" accesskey="U">up</a>
   |
    <a href="static-files/index.html" title="Managing static files (e.g. images, JavaScript, CSS)">next</a> &raquo;</div>
    </div>
  </div>

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