python – Problem with include tag and inheritance in Django template

Question:

There is a base.html template:

<head>
{% include 'head.html' %}
</head>

The included head.html file has a block:

{% block title %}{% endblock %}

In the application, I create a template that will inherit from base.html, in which I want to display a variable in the title block:

{% extends 'base.html' %}
{% block title %}
  {{title}}
{% endblock %}

But this scheme does not work – it gives an error. What's wrong? If I understand correctly, the include tag should include the template in the template as part of it, without restrictions.

Answer:

Bad news for you. Here's what the documentation says about this:

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.

That is, the redefinition of the content of the blocks when inheriting the template does not occur. Therefore, you get the result that you wrote about in the comments.

Scroll to Top