[Django]-<title> {% block title %} Home {% endblock %} </title> is not override by other page?

5đź‘Ť

âś…

As the documentation says, you can only “override” blocks defined in templates from which you extend directly or indirectly (i.e. you extend from a template that extends itself):

The include tag should be considered as an implementation of “render this subtemplate and include the HTML”, not as “parse this subtemplate and include its contents as if it were part of the parent”. This means that there is no shared state between included templates – each include is a completely independent rendering process.

Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered – not blocks that can be overridden by, for example, an extending template.

You thus need to inline your block into the parent template. For example:

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
     <title>
        {% block title %} Home {% endblock %}
     </title>
</head>
{% block content%}

{% endblock %}
{% include "feed/footer.html" %}

and then thus override it in the “subtemplate”.

3đź‘Ť

The docs for the include tag are explicit that this will not work:

Blocks are evaluated before they are included. This means that a template that includes blocks from another will contain blocks that have already been evaluated and rendered – not blocks that can be overridden by, for example, an extending template.

Leave a comment