您的方法很好,但是您执行的顺序错误。首先,不应将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 isgood(在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。希望这可以澄清您的概念并为您带来好处。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)