Question about template inheritance

Hi,

I am wondering if this template code is valid in django templating system. Actually it works, but I am not sure I would I want to rely on. I didn’t found in docs how the behavior should be in this case.

Suppose a file test.html:

{% block content %}
  Here is our main content
  {% block subcontent %}
    Here is our sub content
  {% endblock %}
{% endblock %}

And suppose a template extending it:

{% extends "./test.html" %}

{% block content %}
  Here is our unexpected additionnal info

  {{ block.super }}
{% endblock %}

{% block subcontent %}
  Here is our overloaded subcontent
{% endblock %}

So I override content the same time I define the contained block subcontent.

Is that a documented feature? How do you think about it ?

Hi fpoulain,

Yes, that’s valid template code. I couldn’t find that specific case in the django docs. I think it makes sense as each block is redefining a parent block. If that redefinition were to remove a nested block, the expectation would be that the nested block definition in the child template would not be used. Whether or not that will error is unknown to me. I’d guess it wouldn’t error as the template system is pretty forgiving.

1 Like