{"title":"How to override templates","version":"4.1","locale":"en","docname":"howto/overriding-templates","url":"/en/4.1/howto/overriding-templates/","canonical":"https://djangodocs.dev/en/4.1/howto/overriding-templates/","summary":"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…","html":"<h1>How to override templates<a class=\"heading-anchor\" href=\"#how-to-override-templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>In your project, you might want to override a template in another Django\napplication, whether it be a third-party application or a contrib application\nsuch as <code class=\"docutils literal notranslate\">django.contrib.admin</code>. You can either put template overrides in your\nproject’s templates directory or in an application’s templates directory.</p>\n<p>If you have app and project templates directories that both contain overrides,\nthe default Django template loader will try to load the template from the\nproject-level directory first. In other words, <a class=\"reference internal\" href=\"/en/4.1/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">DIRS</code></a>\nis searched before <a class=\"reference internal\" href=\"/en/4.1/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">APP_DIRS</code></a>.</p>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">See also</p>\n<p>Read <a class=\"reference internal\" href=\"/en/4.1/ref/forms/renderers/#overriding-built-in-widget-templates\"><span class=\"std std-ref\">Overriding built-in widget templates</span></a> if you’re looking to\ndo that.</p>\n</aside>\n<section id=\"overriding-from-the-project-s-templates-directory\">\n<h2>Overriding from the project’s templates directory<a class=\"heading-anchor\" href=\"#overriding-from-the-project-s-templates-directory\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>First, we’ll explore overriding templates by creating replacement templates in\nyour project’s templates directory.</p>\n<p>Let’s say you’re trying to override the templates for a third-party application\ncalled <code class=\"docutils literal notranslate\">blog</code>, which provides the templates <code class=\"docutils literal notranslate\">blog/post.html</code> and\n<code class=\"docutils literal notranslate\">blog/list.html</code>. The relevant settings for your project would look like:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">pathlib</span> <span class=\"kn\">import</span> Path\n\nBASE_DIR <span class=\"o\">=</span> Path<span class=\"p\">(</span><span class=\"vm\">__file__</span><span class=\"p\">)</span><span class=\"o\">.</span>resolve<span class=\"p\">()</span><span class=\"o\">.</span>parent<span class=\"o\">.</span>parent\n\nINSTALLED_APPS <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"o\">...</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;blog&#39;</span><span class=\"p\">,</span>\n    <span class=\"o\">...</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n\nTEMPLATES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <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>\n        <span class=\"s1\">&#39;DIRS&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span>BASE_DIR <span class=\"o\">/</span> <span class=\"s1\">&#39;templates&#39;</span><span class=\"p\">],</span>\n        <span class=\"s1\">&#39;APP_DIRS&#39;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n        <span class=\"o\">...</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>The <a class=\"reference internal\" href=\"/en/4.1/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\">TEMPLATES</code></a> setting and <code class=\"docutils literal notranslate\">BASE_DIR</code> will already exist if you\ncreated your project using the default project template. The setting that needs\nto be modified is <a class=\"reference internal\" href=\"/en/4.1/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">DIRS</code></a>.</p>\n<p>These settings assume you have a <code class=\"docutils literal notranslate\">templates</code> directory in the root of your\nproject. To override the templates for the <code class=\"docutils literal notranslate\">blog</code> app, create a folder\nin the <code class=\"docutils literal notranslate\">templates</code> directory, and add the template files to that folder:</p>\n<div class=\"code-block\" data-language=\"none\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">None</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"None code\"><code>templates/\n    blog/\n        list.html\n        post.html\n</code></pre></div>\n<p>The template loader first looks for templates in the <code class=\"docutils literal notranslate\">DIRS</code> directory. When\nthe views in the <code class=\"docutils literal notranslate\">blog</code> app ask for the <code class=\"docutils literal notranslate\">blog/post.html</code> and\n<code class=\"docutils literal notranslate\">blog/list.html</code> templates, the loader will return the files you just created.</p>\n</section>\n<section id=\"overriding-from-an-app-s-template-directory\">\n<h2>Overriding from an app’s template directory<a class=\"heading-anchor\" href=\"#overriding-from-an-app-s-template-directory\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Since you’re overriding templates located outside of one of your project’s\napps, it’s more common to use the first method and put template overrides in a\nproject’s templates folder. If you prefer, however, it’s also possible to put\nthe overrides in an app’s template directory.</p>\n<p>First, make sure your template settings are checking inside app directories:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>TEMPLATES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"o\">...</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;APP_DIRS&#39;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n        <span class=\"o\">...</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>If you want to put the template overrides in an app called <code class=\"docutils literal notranslate\">myapp</code> and the\ntemplates to override are named <code class=\"docutils literal notranslate\">blog/list.html</code> and <code class=\"docutils literal notranslate\">blog/post.html</code>,\nthen your directory structure will look like:</p>\n<div class=\"code-block\" data-language=\"none\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">None</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"None code\"><code>myapp/\n    templates/\n        blog/\n            list.html\n            post.html\n</code></pre></div>\n<p>With <a class=\"reference internal\" href=\"/en/4.1/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">APP_DIRS</code></a> set to <code class=\"docutils literal notranslate\">True</code>, the template\nloader will look in the app’s templates directory and find the templates.</p>\n</section>\n<section id=\"extending-an-overridden-template\">\n<span id=\"id1\"></span><h2>Extending an overridden template<a class=\"heading-anchor\" href=\"#extending-an-overridden-template\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>With your template loaders configured, you can extend a template using the\n<a class=\"reference internal\" href=\"/en/4.1/ref/templates/builtins/#std-templatetag-extends\"><code class=\"xref std std-ttag docutils literal notranslate\">{% extends %}</code></a> template tag whilst at the same time overriding\nit. This can allow you to make small customizations without needing to\nreimplement the entire template.</p>\n<p>For example, you can use this technique to add a custom logo to the\n<code class=\"docutils literal notranslate\">admin/base_site.html</code> template:</p>\n<blockquote>\n<div><figure class=\"code-block code-block-captioned\" data-language=\"html+django\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">templates/admin/base_site.html</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code> <span class=\"cp\">{%</span> <span class=\"k\">extends</span> <span class=\"s2\">&quot;admin/base_site.html&quot;</span> <span class=\"cp\">%}</span>\n\n <span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">branding</span> <span class=\"cp\">%}</span>\n     <span class=\"p\">&lt;</span><span class=\"nt\">img</span> <span class=\"na\">src</span><span class=\"o\">=</span><span class=\"s\">&quot;link/to/logo.png&quot;</span> <span class=\"na\">alt</span><span class=\"o\">=</span><span class=\"s\">&quot;logo&quot;</span><span class=\"p\">&gt;</span>\n     <span class=\"cp\">{{</span> <span class=\"nb\">block</span><span class=\"nv\">.super</span> <span class=\"cp\">}}</span>\n <span class=\"cp\">{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n</code></pre></figure>\n</div></blockquote>\n<p>Key points to note:</p>\n<ul class=\"simple\">\n<li><p>The example creates a file at <code class=\"docutils literal notranslate\">templates/admin/base_site.html</code> that uses\nthe configured project-level <code class=\"docutils literal notranslate\">templates</code> directory to override\n<code class=\"docutils literal notranslate\">admin/base_site.html</code>.</p></li>\n<li><p>The new template extends <code class=\"docutils literal notranslate\">admin/base_site.html</code>, which is the same template\nas is being overridden.</p></li>\n<li><p>The template replaces just the <code class=\"docutils literal notranslate\">branding</code> block, adding a custom logo, and\nusing <code class=\"docutils literal notranslate\">block.super</code> to retain the prior content.</p></li>\n<li><p>The rest of the template is inherited unchanged from\n<code class=\"docutils literal notranslate\">admin/base_site.html</code>.</p></li>\n</ul>\n<p>This technique works because the template loader does not consider the already\nloaded override template (at <code class=\"docutils literal notranslate\">templates/admin/base_site.html</code>) when\nresolving the <code class=\"docutils literal notranslate\">extends</code> tag. Combined with <code class=\"docutils literal notranslate\">block.super</code> it is a powerful\ntechnique to make small customizations.</p>\n</section>","rootId":"how-to-override-templates","toc":[{"title":"Overriding from the project’s templates directory","anchor":"overriding-from-the-project-s-templates-directory","children":[]},{"title":"Overriding from an app’s template directory","anchor":"overriding-from-an-app-s-template-directory","children":[]},{"title":"Extending an overridden template","anchor":"extending-an-overridden-template","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"“How-to” guides","url":"/en/4.1/howto/"}],"prev":{"docname":"howto/outputting-pdf","title":"How to create PDF files","url":"/en/4.1/howto/outputting-pdf/"},"next":{"docname":"howto/static-files/index","title":"How to manage static files (e.g. images, JavaScript, CSS)","url":"/en/4.1/howto/static-files/"},"formats":{"html":"/en/4.1/howto/overriding-templates/","markdown":"/en/4.1/howto/overriding-templates.md","json":"/en/4.1/howto/overriding-templates.json"},"source":"https://github.com/django/django/blob/stable/4.1.x/docs/howto/overriding-templates.txt","official":"https://docs.djangoproject.com/en/4.1/howto/overriding-templates/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}