如何在Django中覆盖包含模板的块?

时间:2013-05-07 11:52:53

标签: python django

假设我有这种结构:

{# base.html #}
{% block content %}{% endblock %}

{# page.html #}
{% extends "base.html" %}
{% block content %}
{% include "snippet.html" %}
{# and I also want somehow to redefine {% block snippet_content %} of snippet here #}
{% endblock %}

{# snippet.html #}
<bells_and_whistles>
    {% block snippet_content %}{% endblock %}
</bells_and_whistles>

我希望代码能够自我解释。

有没有一种优雅的方法来实现这一目标?

1 个答案:

答案 0 :(得分:1)

我担心你想要这样做是不可能的。

您的选择是:

  1. 创建一个modified_snippet.html继承自snippet.html并覆盖该块并将其包含在内
  2. snippet_content块更改为{{ snippet_content }}变量,并使用{% include "snippet.html" with snippet_content="My content" %}传递其内容。当然这种方法非常有限。