1 回答
TA贡献1911条经验 获得超7个赞
您的方法很好,但是您执行的顺序错误。首先,不应将html的开始<html>和结束标记</html>拆分为不同的文件,最好将其放入base.html。
以下是如何遵循分手结构的示例:
base.html
<html>
<head>
<!-- Some stuff here which should be included in all templates for example js or css -->
{% block extra_css %}
<!-- to included app-template dependent css -->
{% endblock extra_css %}
{% block extra_js %}
<!-- to included app-template dependent js -->
{% endblock extra_js %}
{% block extra_head %}
<!-- for anything else inside head -->
{% endblock extra_head %}
</head>
<body>
{% block menu %}
<!-- Default menu here -->
{% block extra_menu %}
<!-- extend menu based on template -->
{% endblock extra_menu %}
{% endblock menu %}
{% block content %}
<div>This is good</div>
{% endblock content %}
{% include "footer.html" %}
{% block bottom_js %}
<!-- if you want to have manual js scripts at bottom -->
{% endblock bottom_js %}
</body>
</html>
现在base.html是所有设置,现在让我们假设您想覆盖另一个要覆盖的base.html块的子模板content:
child.html
{% extends "base.html" %}
{% block content %}
<div>This is really good</div>
{% endblock content %}
因此,在加载页面时,您会看到this is really good而不是this is good(它在content块内的base.html中定义),因为您只是覆盖了它。
如果您希望base.html保留内容块中的所有内容,则需要扩展该块,而不是通过使用方法{{block.super}}来完全覆盖它
child.html
{% extends "base.html" %}
{% block content %}
{{ block.super }}
<div>This is really good</div>
{% endblock content %}
现在,您将同时看到this is good和this is really good。希望这可以澄清您的概念并为您带来好处。
添加回答
举报