python – Django template: Why block in included template can’t be overwritten by child template? – Education Career Blog

To illustrate my question more clearly, let’s suppose I have a include.html template with content:

{% block test_block %}This is include{% endblock %}

I have another template called parent.html with content like this:

This is parent

{% include "include.html" %}

Now I create a templated called child.html that extends parent.html:

{% extends "parent.html" %}
{% block test_block %}This is child{% endblock %}

My idea is that when rendering child.html, the test_block in child.html can overwrite the one in include.html. As per my understanding, when a template is included, it is included as it is. So in my case, I think parent.html equals to:

This is parent

{% block test_block %}This is include{% endblock %}

So child.html should be able to overwrite test_block. But looks like it can’t. Why? Is there a workaround?


When you include a template, it renders the template, then includes the rendered content.

From the django docs:

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.

A workaround would be to have the child template extend the included template instead of the including template. Then, include the child template.

Leave a Comment