Python 向所有模板添加导航栏

Python 向所有模板添加导航栏,python,flask,jinja2,Python,Flask,Jinja2,我想在每一页上显示导航栏。在PHP中,我会编写导航栏,然后将其包含在其他页面上。我试图将导航栏模板包括或扩展到其他模板中,但没有成功。它只输出“这是主页”。如何在每个模板中正确包含导航栏 layout.html <style> body { margin: 0; padding: 0; } div{ background: #333; color: #f9f9f9; width:

我想在每一页上显示导航栏。在PHP中,我会编写导航栏,然后将其包含在其他页面上。我试图将导航栏模板包括或扩展到其他模板中,但没有成功。它只输出“这是主页”。如何在每个模板中正确包含导航栏

layout.html

<style>
    body {
        margin: 0;
        padding: 0;
    }

    div{
        background: #333;
        color: #f9f9f9;
        width: 100%;
        height: 50px;
        line-height: 50px;
        text-align: center;
    }
</style>

<div>NAVBAR</div> 
{% extends "layout.html" %}

{% block content %}
    <h1>This is the homepage!</h1>
{% endblock %}

{%block navbar%}
身体{
保证金:0;
填充:0;
}
div{
背景:#333;
颜色:#f9f9f9;
宽度:100%;
高度:50px;
线高:50px;
文本对齐:居中;
}
导航栏
{%endblock%}
{%block content%}
{%endblock%}
index.html

<style>
    body {
        margin: 0;
        padding: 0;
    }

    div{
        background: #333;
        color: #f9f9f9;
        width: 100%;
        height: 50px;
        line-height: 50px;
        text-align: center;
    }
</style>

<div>NAVBAR</div> 
{% extends "layout.html" %}

{% block content %}
    <h1>This is the homepage!</h1>
{% endblock %}
这是主页。
{%extends“layout.html”%}
{%block导航栏%}{%endblock%}
{%block content%}
这是主页!
{%endblock%}

如果希望在每个页面中使用相同的导航栏,则不需要layout.html中的
{%block navbar%}…{%endblock%}
。或者,您可能必须使用所述的
{{super()}

创建一个基本模板,其布局和导航对所有页面都是通用的。然后扩展此模板以创建实际页面。将块添加到可以在其他模板中替代的基础模板

base.html

<style>
    body {
        margin: 0;
        padding: 0;
    }

    div{
        background: #333;
        color: #f9f9f9;
        width: 100%;
        height: 50px;
        line-height: 50px;
        text-align: center;
    }
</style>

<div>NAVBAR</div> 
{% extends "layout.html" %}

{% block content %}
    <h1>This is the homepage!</h1>
{% endblock %}

{%block title%}-我的网站{%endblock%}
导航栏
{%block content%}{%endblock%}
index.html

<style>
    body {
        margin: 0;
        padding: 0;
    }

    div{
        background: #333;
        color: #f9f9f9;
        width: 100%;
        height: 50px;
        line-height: 50px;
        text-align: center;
    }
</style>

<div>NAVBAR</div> 
{% extends "layout.html" %}

{% block content %}
    <h1>This is the homepage!</h1>
{% endblock %}
{%extends'base.html%}
{%block content%}
{%block title%}主页{%endblock%}
你好,世界

{%endblock%}
请注意,导航栏只是在基本模板中定义的。它不需要一个块,子模板中的内容将在它之后替换


您可以使用来控制导航栏中突出显示的项目。

您可以在每页中包括导航栏

nav.html

<style>
    body {
        margin: 0;
        padding: 0;
    }

    div{
        background: #333;
        color: #f9f9f9;
        width: 100%;
        height: 50px;
        line-height: 50px;
        text-align: center;
    }
</style>

<div>NAVBAR</div> 
{% extends "layout.html" %}

{% block content %}
    <h1>This is the homepage!</h1>
{% endblock %}
index.html

<style>
    body {
        margin: 0;
        padding: 0;
    }

    div{
        background: #333;
        color: #f9f9f9;
        width: 100%;
        height: 50px;
        line-height: 50px;
        text-align: center;
    }
</style>

<div>NAVBAR</div> 
{% extends "layout.html" %}

{% block content %}
    <h1>This is the homepage!</h1>
{% endblock %}
{%extends“layout.html”%}
{%block content%}
这是主页!
{%endblock%}

有时候,这是设计网页的好方法。您将页面拆分为,例如:head.html、nav.html、footer.html。。。您可以将它们包含在layout.html中使用。

谢谢<代码>{%include'nav.html%}正是我想要的。我想明确地将我的基础和导航栏(等)保存在不同的文件中,以便交换导航栏,方便编辑。但我找不到如何将navbar.html“导入”到base.html中,但如何能够突出显示nav中处于活动状态的页面?