27

The master template in my Django app looks like this:

{% block parent %}
    Some text...
    {% block child %}
        Default content here...
    {% endblock child %}
    ...some more text
{% endblock parent %}

Now, this template should be overwritten in a way that the child block is changed:

{% extends "master.html" %}

{% block child %}
    New content here...
{% endblock child%}

However, the rendering stays the same (printing "default content here..."). Have I missed something obvious or are nested blocks not possible? (Or, violating the DRY principle, have I to re-define the parent block?)

Edit: I'm working with Django 1.1, if that matters.

niton
  • 8,771
  • 21
  • 32
  • 52
Boldewyn
  • 81,211
  • 44
  • 156
  • 212
  • 3
    Something else is wrong here, because (also using 1.1) I am unable to replicate this problem. For me, simply overriding the child block, without any reference to the parent block, has the desired effect. – Ben James Dec 14 '09 at 13:34
  • Yes, you're right. See my answer below. Thanks for looking at it. – Boldewyn Dec 14 '09 at 13:49

2 Answers2

13

OK, it's a bug in Django's template system. For most other cases, Ben James is right (see his comment to my question above).

In my case, the child block was inside a {% ifnotequal a b %} block, and that breaks the block inheritance. I consider that to be a bug, since there are dozens of natural use cases for such operations.

The corresponding ticket.

Boldewyn
  • 81,211
  • 44
  • 156
  • 212
  • 2
    It's a good idea to always _test_ the code you are posting, rather than assuming that what you took out won't affect the outcome. – Ben James Dec 14 '09 at 14:09
  • True, true... I had a custom template tag in it in the first place and suspected that, but after throwing it out I didn't think about the ifnotequal. – Boldewyn Dec 14 '09 at 15:17
-1

Do this:

{% extends "master.html" %}

{% block parent %}
    {% block child %}
        New content here...
    {% endblock child%}
{% endblock parent %}
lprsd
  • 84,407
  • 47
  • 135
  • 168
  • 4
    What if the base `parent` block contains anything other than the `child` block? – Dominic Rodger Dec 14 '09 at 13:26
  • 3
    Thanks for the answer, but unfortunately it's not useful. That is partly my mistake, I updated the example for the master template above. Problem: The reason for the master template is, that there is more data in it. If I do as you suggest, all this additional data is of course lost. – Boldewyn Dec 14 '09 at 13:28
  • doing that may break website functionality, it requests 2 times the child page, therefore it may duplicate script tags and more. – christk Feb 29 '20 at 20:14