{"title":"How to override templates","version":"4.2","locale":"en","docname":"howto/overriding-templates","url":"/en/4.2/howto/overriding-templates/","canonical":"https://djangodocs.dev/en/4.2/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\"><span class=\"pre\">django.contrib.admin</span></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.2/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DIRS</span></code></a>\nis searched before <a class=\"reference internal\" href=\"/en/4.2/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APP_DIRS</span></code></a>.</p>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">See also</p>\n<p>Read <a class=\"reference internal\" href=\"/en/4.2/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\"><span class=\"pre\">blog</span></code>, which provides the templates <code class=\"docutils literal notranslate\"><span class=\"pre\">blog/post.html</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">blog/list.html</span></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=\"w\"> </span><span class=\"nn\">pathlib</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Path</span>\n\n<span class=\"n\">BASE_DIR</span> <span class=\"o\">=</span> <span class=\"n\">Path</span><span class=\"p\">(</span><span class=\"vm\">__file__</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">resolve</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">parent</span><span class=\"o\">.</span><span class=\"n\">parent</span>\n\n<span class=\"n\">INSTALLED_APPS</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"o\">...</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;blog&quot;</span><span class=\"p\">,</span>\n    <span class=\"o\">...</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n\n<span class=\"n\">TEMPLATES</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.template.backends.django.DjangoTemplates&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;DIRS&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"n\">BASE_DIR</span> <span class=\"o\">/</span> <span class=\"s2\">&quot;templates&quot;</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;APP_DIRS&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n        <span class=\"c1\"># ...</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.2/ref/settings/#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\ncreated your project using the default project template. The setting that needs\nto be modified is <a class=\"reference internal\" href=\"/en/4.2/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DIRS</span></code></a>.</p>\n<p>These settings assume you have a <code class=\"docutils literal notranslate\"><span class=\"pre\">templates</span></code> directory in the root of your\nproject. To override the templates for the <code class=\"docutils literal notranslate\"><span class=\"pre\">blog</span></code> app, create a folder\nin the <code class=\"docutils literal notranslate\"><span class=\"pre\">templates</span></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\"><span class=\"pre\">DIRS</span></code> directory. When\nthe 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\n<code class=\"docutils literal notranslate\"><span class=\"pre\">blog/list.html</span></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><span class=\"n\">TEMPLATES</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"c1\"># ...</span>\n        <span class=\"s2\">&quot;APP_DIRS&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n        <span class=\"c1\"># ...</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\"><span class=\"pre\">myapp</span></code> and the\ntemplates 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>,\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.2/ref/settings/#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\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.2/ref/templates/builtins/#std-templatetag-extends\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">extends</span> <span class=\"pre\">%}</span></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\"><span class=\"pre\">admin/base_site.html</span></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\"><span class=\"pre\">templates/admin/base_site.html</span></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\"><span class=\"pre\">templates/admin/base_site.html</span></code> that uses\nthe configured project-level <code class=\"docutils literal notranslate\"><span class=\"pre\">templates</span></code> directory to override\n<code class=\"docutils literal notranslate\"><span class=\"pre\">admin/base_site.html</span></code>.</p></li>\n<li><p>The new template extends <code class=\"docutils literal notranslate\"><span class=\"pre\">admin/base_site.html</span></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\"><span class=\"pre\">branding</span></code> block, adding a custom logo, and\nusing <code class=\"docutils literal notranslate\"><span class=\"pre\">block.super</span></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\"><span class=\"pre\">admin/base_site.html</span></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\"><span class=\"pre\">templates/admin/base_site.html</span></code>) when\nresolving the <code class=\"docutils literal notranslate\"><span class=\"pre\">extends</span></code> tag. Combined with <code class=\"docutils literal notranslate\"><span class=\"pre\">block.super</span></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.2/howto/"}],"prev":{"docname":"howto/outputting-pdf","title":"How to create PDF files","url":"/en/4.2/howto/outputting-pdf/"},"next":{"docname":"howto/static-files/index","title":"How to manage static files (e.g. images, JavaScript, CSS)","url":"/en/4.2/howto/static-files/"},"formats":{"html":"/en/4.2/howto/overriding-templates/","markdown":"/en/4.2/howto/overriding-templates.md","json":"/en/4.2/howto/overriding-templates.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/howto/overriding-templates.txt","official":"https://docs.djangoproject.com/en/4.2/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"]}