【发布时间】:2022-07-27 23:49:04
【问题描述】:
我正在尝试从子模板中的父模板扩展两个嵌套块。
事情是这样的:
父.html
{% block parentBlock %}
<span> Foo </span>
{% block rowBlock %}
<button ...> Button here </button>
<button ...> Another button here </button>
{% endblock rowBlock %}
<div> Some other content </div>
{% endblock parentBlock %}
child.html
{% extends 'parent.html' %}
{% block parentBlock %}
{{ block.super }} # --> See note n°1 below
{% block rowBlock %}
{{ block.super }}
<button ...> A third button that extends the 2 others </button>
{% endblock rowBlock %}
<div> Content that extends parentBlock from parent.html </div>
{% endblock parentBlock %}
Note n° 1:问题是标记为 Note 1 的 parentBlock 上的 child 的 block.super 将执行包含新 rowBlock 并附加新 rowBlock 的 super。
结果是这样的:
<span> Foo </span>
<button ...> Button here </button>
<button ...> Another button here </button>
<button ...> A third button that extends the 2 others </button>
<button ...> Button here </button>
<button ...> Another button here </button>
<button ...> A third button that extends the 2 others </button>
<div> Some other content </div>
<div> Content that extends parentBlock from parent.html </div>
【问题讨论】: