【问题标题】:Django templates inheritanceDjango 模板继承
【发布时间】:2017-10-31 11:40:50
【问题描述】:

例如:

base.html

<body>
    {% block content}
    {% endblock %}
</body>

base_index.html

{% extends 'base.html' %}
{% block content %}
    something
{% endblock %}

# add new block "for_child" to fill in with next inheritance
<h1>Name:
{% block for_child %}
{% endblock %}</h1>

base_index_child.html

{% extends 'base_index.html' %}

{% block for_child %}
    Peter
{% endblock %}

结果base_index_child.html:

<body>
    something
</body>

但我想要(base.html -> base_index.html -> base_index_child.html)

<body>
    something
    <h1>Name: Peter</h1>
</body>

如何获得?

更新(答案)

添加块必须在块内

base_index.html

{% extends 'base.html' %}
{% block content %}

    something

    <h1>Name:
    {% block for_child %} # block must be inside the block
    {% endblock %}</h1>

{% endblock %}

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    This post 几乎就是你要问的。

    所以这会解决它:

    base_index.html

    {% extends 'base.html' %}
    {% block content %}
        something
    
        <h1>Name:
        {% block for_child %}
        {% endblock %}
        </h1>
    
    {% endblock %}
    

    【讨论】:

    • 谢谢!这就是我需要的。
    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 2011-02-04
    • 2015-11-13
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    相关资源
    最近更新 更多