From 2c2f4b37997daf84834547c8abd146cd6e9eac13 Mon Sep 17 00:00:00 2001 From: David Smith Date: Tue, 23 Jun 2020 06:57:19 +0100 Subject: Fixed #29336 -- Doc'd circular template inheritance --- docs/howto/overriding-templates.txt | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'docs/howto') diff --git a/docs/howto/overriding-templates.txt b/docs/howto/overriding-templates.txt index 7faf972a3b..71eb14b0c4 100644 --- a/docs/howto/overriding-templates.txt +++ b/docs/howto/overriding-templates.txt @@ -97,3 +97,43 @@ then your directory structure will look like: With :setting:`APP_DIRS` set to ``True``, the template loader will look in the app's templates directory and find the templates. + +.. _extending_an_overridden_template: + +Extending an overridden template +================================ + +With your template loaders configured, you can extend a template using the +:ttag:`{% extends %}` template tag whilst at the same time overriding +it. This can allow you to make small customizations without needing to +reimplement the entire template. + +For example, you can use this technique to add a custom logo to the +``admin/base_site.html`` template: + + .. code-block:: html+django + :caption: templates/admin/base_site.html + + {% extends "admin/base_site.html" %} + + {% block branding %} + logo + {{ block.super }} + {% endblock %} + +Key points to note: + +* The example creates a file at ``templates/admin/base_site.html`` that uses + the configured project-level ``templates`` directory to override + ``admin/base_site.html``. +* The new template extends ``admin/base_site.html``, which is the same template + as is being overridden. +* The template replaces just the ``branding`` block, adding a custom logo, and + using ``block.super`` to retain the prior content. +* The rest of the template is inherited unchanged from + ``admin/base_site.html``. + +This technique works because the template loader does not consider the already +loaded override template (at ``templates/admin/base_site.html``) when +resolving the ``extends`` tag. Combined with ``block.super`` it is a powerful +technique to make small customizations. -- cgit v1.3