I have the following scenario:
base.html:
{% block content %}{% endblock %}
child.html:
{% extends 'base.html' %}
{% block content %}
<p>Overriding content</p>
{% endblock %}
{% block child_block %}{% endblock %}
child_of_child.html:
{% extends 'child.html' %}
{% block child_block %}
<p>Overriding child</p>
{% endblock %}
Creating a new block child_block in child.html and having child_of_child.html extending child.html and overriding this block does not work until I also include child_block in base.html as a hook.
Is it not possible to create new template blocks / hooks apart from within the root template? If so, is there a way around it without having to include every possible hook inside base.html?
The problem is that your
child_blockblock doesn’t live anywhere inbase.html, because it’s outside of existing blocks. Where would it appear when the template is rendered? There’s simply no place defined for it.It’s perfectly OK for child templates to define blocks inside other blocks, which are then populated by further children. So, for example:
works absolutely fine, and your result will be: