{"title":"Overriding templates","version":"1.11","locale":"es","docname":"howto/overriding-templates","url":"/es/1.11/howto/overriding-templates/","canonical":"https://djangodocs.dev/es/1.11/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>Overriding templates<a class=\"heading-anchor\" href=\"#overriding-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=\"/es/1.11/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=\"/es/1.11/ref/settings/#std-setting-TEMPLATES-APP_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">APP_DIRS</code></a>.</p>\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\">import</span> <span class=\"nn\">os</span>\n\nBASE_DIR <span class=\"o\">=</span> os<span class=\"o\">.</span>path<span class=\"o\">.</span>dirname<span class=\"p\">(</span>os<span class=\"o\">.</span>path<span class=\"o\">.</span>dirname<span class=\"p\">(</span>os<span class=\"o\">.</span>path<span class=\"o\">.</span>abspath<span class=\"p\">(</span><span class=\"vm\">__file__</span><span class=\"p\">)))</span>\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>os<span class=\"o\">.</span>path<span class=\"o\">.</span>join<span class=\"p\">(</span>BASE_DIR<span class=\"p\">,</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=\"/es/1.11/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=\"/es/1.11/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=\"/es/1.11/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>","rootId":"overriding-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":[]}],"breadcrumbs":[{"docname":"howto/index","title":"«How-to» guides","url":"/es/1.11/howto/"}],"prev":{"docname":"howto/outputting-pdf","title":"Outputting PDFs with Django","url":"/es/1.11/howto/outputting-pdf/"},"next":{"docname":"howto/static-files/index","title":"Managing static files (e.g. images, JavaScript, CSS)","url":"/es/1.11/howto/static-files/"},"formats":{"html":"/es/1.11/howto/overriding-templates/","markdown":"/es/1.11/howto/overriding-templates.md","json":"/es/1.11/howto/overriding-templates.json"},"source":"https://github.com/django/django/blob/stable/1.11.x/docs/howto/overriding-templates.txt","official":"https://docs.djangoproject.com/es/1.11/howto/overriding-templates/","inVersions":["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","fr","ja","id","pt-br","ko","es","el","pl"]}