Flask - base.html 中的 Bootstrap

我想将 Bootstrap 添加到我的网站。我有 base.html 和其他文件,它们继承自 base.html,但在我的 base.html 中,我想要导航栏,它将显示在每个站点上。不幸的是,添加了一些引导代码后,它没有显示,但是如果我将此代码添加到 index.html 中,它将显示出来。


我的 base.html:


{% extends "bootstrap/base.html" %}

{% block head %}

    {{ super() }}

    {% if title %}

    <title> {{ title }} - Book Blog</title>

    {% else %}

    <title> Welcome to Book Blog!</title>

    {% endif %}

{% endblock %}


{% block navbar %}

Microblog: 

    <ul class="nav nav-pills">

    <li class="nav-item">

        <a class="nav-link active" href="{{ url_for('index') }}">Home</a>

    </li>

    <li class="nav-item">

        <a class="nav-link" href="{{ url_for('show_books') }}">Books</a>

    </li>

    <li class="nav-item">

        <a class="nav-link" href="{{ url_for('login') }}">Login</a>

    </li>

    <li class="nav-item">

        <a class="nav-link" href="{{ url_for('logout') }}">Logout</a>

    </li>

</ul>

{% endblock %}


<body>


<hr>

{% with messages = get_flashed_messages() %}

{% if messages %}

<ul>

    {% for message in messages %}

    <li>{{ message }}</li>

    {% endfor %}

</ul>

{% endif %}

{% endwith %}

{% block content %}{% endblock %}

</body>

所以在这种情况下,导航栏不显示。正如我上面写的,如果我将导航类添加到 index.html,导航栏仅显示在此站点上。我该如何解决?


拉莫斯之舞
浏览 315回答 2
2回答

达令说

问题可能来自您的index.html页面。原则上,您的索引页必须从base.html模板继承。什么应该给这个:{% extends "base.html" %}{% block title %}Welcome to Book Blog!{% endblock %}{% block content %}&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <h1>Your content</h1>&nbsp; &nbsp; </div>{% endblock %}你的模板base.html也可以稍微改变:{% extends "bootstrap/base.html" %}{% block head %}&nbsp; &nbsp; {{ super() }}&nbsp; &nbsp; {% if title %}&nbsp; &nbsp; &nbsp; &nbsp; <title> {{ title }} - Book Blog</title>&nbsp; &nbsp; {% else %}&nbsp; &nbsp; &nbsp; &nbsp; <title> Welcome to Book Blog!</title>&nbsp; &nbsp; {% endif %}{% endblock %}{% block body %}<body>{% block navbar %}Microblog:&nbsp;<ul class="nav nav-pills">&nbsp; &nbsp; <li class="nav-item">&nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link active" href="{{ url_for('index') }}">Home</a>&nbsp; &nbsp; </li>&nbsp; &nbsp; <li class="nav-item">&nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link" href="{{ url_for('show_books') }}">Books</a>&nbsp; &nbsp; </li>&nbsp; &nbsp; <li class="nav-item">&nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link" href="{{ url_for('login') }}">Login</a>&nbsp; &nbsp; </li>&nbsp; &nbsp; <li class="nav-item">&nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link" href="{{ url_for('logout') }}">Logout</a>&nbsp; &nbsp; </li></ul>{% endblock %}&nbsp; &nbsp; <hr>&nbsp; &nbsp; {% with messages = get_flashed_messages() %}&nbsp; &nbsp; &nbsp; &nbsp; {% if messages %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% for message in messages %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>{{ message }}</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</ul>&nbsp; &nbsp; &nbsp; &nbsp;{% endif %}&nbsp; &nbsp; {% endwith %}&nbsp; &nbsp; {% block content %}{% endblock %}</body>{% endblock %}我在模板的正文标签中添加了一个块标签。现在,您应该在希望具有相同结构(导航栏等)的所有页面中继承此模板

江户川乱折腾

您可能需要在每个页面上导入 Bootstrap Libs(Js、Css)。我建议编写一个<title></title>可以在每个页面中导入的文件。属性也是如此:为导航栏编写一个文件,并在需要导航栏时将其导入“Base Html”。希望我能帮上忙
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python