Html 每个页面上的 Django 页脚和页眉,带有 {% extends }
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31456576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django Footer and header on each page with {% extends }
提问by encrypt
So I'm trying to add the footer and header on every page of my website. I made a base.html file which contains the general layout of the site.
所以我试图在我网站的每个页面上添加页脚和页眉。我制作了一个包含站点总体布局的 base.html 文件。
In my about.html page, i did:
在我的 about.html 页面中,我做了:
{% extends "public/base.html" %}
<h1>Content goes here</h1>
I can see my header and footer, but how do I display the content. I want to type stuff in that about.html page. The content goes here isn't being displayed in the middle.
我可以看到我的页眉和页脚,但如何显示内容。我想在那个 about.html 页面中输入内容。这里的内容没有显示在中间。
回答by Daniel Roseman
You need to define a block in base.html and populate it in about.html.
您需要在 base.html 中定义一个块并在 about.html 中填充它。
base.html:
基地.html:
<header>...</header>
{% block content %}{% endblock %}
<footer>...</footer>
about.html
关于.html
{% extends "public/base.html" %}
{% block content %}
<h1>Content goes here</h1>
{% endblock %}
This is all fully explained in the tutorial.
这一切都在教程中得到了充分的解释。
回答by chandu
{% extends "public/base.html" %}
{% block content %}
<h1>Content goes here</h1>
{% endblock %}
Or simply create about.html
and include it where you want in your main html.
或者简单地创建about.html
并将其包含在主 html 中您想要的位置。
Example:
例子:
{% extends "public/base.html" %}
{% block content %}
"Your code"
{% include "core/about.html" %}
{% endblock %}
回答by Matthew R.
Let's say your base.html looks like this:
假设您的 base.html 如下所示:
<html>
<body>
<!-- header code -->
{% block content %}
{% endblock %}
<!-- footer code -->
</body>
<html>
Then in your other file you would do this:
然后在您的其他文件中,您将执行以下操作:
{% extends "base.html" %}
{% block content %}
<!-- Content here -->
{% endblock %}
Anything placed inside the template's (extended file's) body tag would be overwritten by the stuff in the child file's content, but anything outside of that tag would be extended, or copied, into it.
放置在模板(扩展文件)正文标签内的任何内容都将被子文件内容中的内容覆盖,但该标签之外的任何内容都将扩展或复制到其中。
Here are the docs on the block tag
这是块标签上的文档