[Answered ]-Django – Add block inside an included html

1👍

As stated in the documentation:

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.

In simpler words as you notice, what you try is not possible.

How to overcome this? Simply bring the blocks to the parent template:

base.html

{% block title %}
    <p>This is title base.html</p>
{% endblock title %}
{% block subtitle %}
    {% include "head.html" %}
{% endblock subtitle %}

head.html

<p>This is subtitle head.html</p>

index.html

{% extends 'base.html' %}

{% block title %}
    <p>This is title index.html</p>
{% endblock title %}


{% block subtitle %}
    <p>This is subtitle index.html</p>
{% endblock subtitle %}

Leave a comment