很久之前用过flask,那时候是跟着教程,教程怎么做我就怎么做,没有仔细考虑过。
现在是全靠文档和搜索一步一步搭建,忘了很多东西,就碰了很多壁,浅浅记录一下子。
比如,我一个网页设置了导航栏和背景,其他页面都是以此为基础显示不同内容,但是我又不想都写在同一个网页上显得杂乱,故此分离开来,应该是这样使用:
base.html
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>myWebtitle>
- <link rel="stylesheet" type="text/css" href="../static/css/base.css">
- <link rel="stylesheet" type="text/css" href="../static/css/bootstrap.css">
- <script src="../static/js/jquery.min.js">script>
- <script type="text/javascript" src="../static/js/daterangepicker.js">script>
- <script type="text/javascript" src="../static/js/bootstrap.bundle.js">script>
- {% block head %}{% endblock %}
- head>
- <body>
- <nav class="navbar navbar-expand-lg bg-body-tertiary">
- <div class="container-fluid">
- <a class="navbar-brand" href="#">Navbara>
- <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
- <span class="navbar-toggler-icon">span>
- button>
- <div class="collapse navbar-collapse" id="navbarNav">
- <ul class="navbar-nav" id="myTab">
- <li class="nav-item">
- <a class="nav-link active" aria-current="page" href="/">Homea>
- li>
- <li class="nav-item">
- <a class="nav-link" href="/page1">page1a>
- li>
- <li class="nav-item">
- <a class="nav-link" href="/page2">page2a>
- li>
- <li class="nav-item">
- <a class="nav-link" href="/page3">page3a>
- li>
- ul>
- div>
- div>
- nav>
- {% block content %}{% endblock %}
- body>
- <script>
- $(document).ready(function () {
- $("#myTab").find("li").each(function () {
- var a = $(this).find("a:first")[0];
- console.log(location.pathname)
- if ($(a).attr("href") === location.pathname) {
- $(a).addClass("active");
- } else {
- $(a).removeClass("active");
- }
- });
- });