如何解决如何使用标签和过滤器从index.html到所有html页面显示页脚?
如果我有 index.html 并且它包含许多插件,例如搜索框,帖子,评论部分等,而我只想使用页脚部分 >并使用标签和过滤器将其添加到我的所有html页面中,以便以后在所有html页面中轻松对其进行编辑,我可以这样做吗?
这是index.html中的页脚代码:
<footer style="margin:auto" class="py-5 bg-dark h-50 d-inline-block w-100 p-3">
<div class="container">
<div class="text-center mt-4">
<a href="/Privacy_Policy" style="margin:5px;width:140px" role="presentation" type="button" class="btn btn-secondary btn-lg">Privcy Policy</a>
<a href="/request_apps/" style="margin:5px;width:140px" role="presentation" type="button" class="btn btn-secondary btn-lg">Request apps</a>
<a href="/our_Mission" style="margin:5px;width:140px" role="presentation" type="button" class="btn btn-secondary btn-lg">Our Mission</a>
<a href="/Contact_US" style="margin:5px;width:140px" role="presentation" type="button" class="btn btn-secondary btn-lg">Contact us</a>
<a href="/About_Me" style="margin:5px;width:140px" role="presentation" type="button" class="btn btn-secondary btn-lg">About us</a>
</div>
<!-- copyright -->
<p style="font-family:monospace;font-size:18px" class="m-0 text-center text-white" >Copyright 2019-2020 SoftDLR.com All Rights Reserved. </p>
</div>
<!-- /.container -->
解决方法
上面推荐的@JoãoHaas的工作方式非常好,我个人使用了另一种方法,尽管我认为在技术上都不比另一种更好,那就是将页脚设置为自己的独立代码段。我个人觉得这看起来更干净,可以阅读和编辑。
<!-- base.html -->
<html>
<head>
<!-- stuff you want every page to have on the head -->
<!-- head block for extending -->
{% block head %}
{% endblock %}
</head>
<body>
<!-- body block for extending -->
{% include 'snippets/base_css.html' %}
{% include 'snippets/header.html' %}
{% block body %}
{% endblock %}
<!-- your footer html -->
{% include 'snippets/footer.html' %}
</html>
<!-- snippets/footer.html -->
<footer class="container">
<p> footer info </p>
</footer>
<!-- snippets/header.html -->
<div class="container">
<p>Header info</p>
</div>
要执行此操作,请在主目录中设置一个模板目录,然后在一个子目录中仅用于摘要。结构看起来像这样:
manage.py
templates
----base.html
----snippets
----base_css.html
----header.html
----footer.html
----other_snippet.html
----another_snippet.html
----and_another_snippet.html
诚实地说,越多越好,我就疯狂地使用这些东西。当代码变得越来越复杂时,它们使工作变得轻松一百万倍。
,
您可能想要一个具有多个块的基本HTML文件,并创建从其扩展的其他页面:
<!-- base.html -->
<html>
<head>
<!-- stuff you want every page to have on the head -->
<style>
* {
font-size: 14;
}
</style>
<!-- head block for extending -->
{% block head %}
{% endblock %}
</head>
<body>
<!-- body block for extending -->
{% block body %}
{% endblock %}
<!-- your footer html -->
<footer>
...
</footer>
</html>
然后您将创建如下所有文件:
<!-- page.html -->
{% extends "base.html" %}
<!-- page specific head -->
{% block head %}
<style>
div {
margin: 0;
}
</style>
{% endblock %}
<!-- page specific body -->
{% block body %}
<p>The footer should be added after this paragraph!</p>
{% endblock %}
使用块扩展的想法是,每当您要求page.html
时,它将在您在base.html
上创建的块定义中使用page.html
。如果未定义块,则不会在最终的html上呈现该块。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。