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。希望這可以澄清您的概念并為您帶來好處。
添加回答
舉報