栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将模板分为几个部分并包括每个部分是否不好?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将模板分为几个部分并包括每个部分是否不好?

您的方法很好,但是您执行的顺序错误。首先,不应将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
。希望这可以澄清您的概念并为您带来好处。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659939.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号