我通过youtube tutorial学习Flask,但我无法让bootstrap导航栏正常工作。我从bootstrap示例部分中获取了代码。我已经尝试了类似问题的其他答案,但似乎没有解决问题。代码段和截图发布在下面。我使用的CSS链接也很好用。我是初学者,所以任何帮助都会非常感激。谢谢。
的layout.html:
<!DOCTYPE html>
<html>
<head>
<title>MyFlaskApp</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
{% include 'includes/_navbar.html' %}
{% block body %}{% endblock %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>
_navbar.html:
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">MyFlaskApp</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/articles">Articles</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
答案 0 :(得分:2)
您正在导入 Bootstrap 4 ,但尝试使用 Bootstrap 3 语法。
解决此问题的最简单方法是简单地导入Boostrap 3,这可以通过URL https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css
完成。请注意,虽然样式不是必需的,但您可能还希望通过https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js
将JavaScript切换到相同的版本。
这个简单的更改会更正您的导航栏,如下所示:
<!DOCTYPE html>
<html>
<head>
<title>MyFlaskApp</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<!-- _navbar.html -->
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">MyFlaskApp</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/articles">Articles</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
希望这有帮助! :)