Nested blocks in Django templates

时光怂恿深爱的人放手 提交于 2019-12-23 06:48:10

问题


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.


回答1:


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.




回答2:


Do this:

{% extends "master.html" %}

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


来源:https://stackoverflow.com/questions/1900892/nested-blocks-in-django-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!