没有 CSS 和 JS,当斜杠在末尾时

No CSS and JS, when slash at the end

Views.py

@app.errorhandler(404, strict_slashes=False)
def page_not_found(e):
    return render_template('404.html', e=e)

404.html

{% extends 'layouts/layout1.html' %}

{% block title %}404 Page Not Found
{% endblock title %}

{% block content %}
    <div class="container" >
        <h3>Page you are looking for does not exist: {{ e }}</h3>
    </div>
{% endblock %}

layout1.html

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>{% block title %}

    {% endblock %}</title>

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="css/small-business.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>

    <!-- Navigation -->
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <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="#">
                    <img src="http://placehold.it/150x50&text=Logo" alt="">
                </a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li>
                        <a href="/">About</a>
                    </li>
                    <li>
                        <a href="#">Services</a>
                    </li>
                    <li>
                        <a href="#">Contact</a>
                    </li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>


    <!-- this is the data with the instruments, I applied on the top of hte website. -->

    <div class="container" >

      <!-- Nav tabs -->
      <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
        <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
        <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
      </ul>

      <!-- Tab panes -->
      <div class="tab-content">
          <div role="tabpanel" class="tab-pane fade in active" id="home">
              <ul>
                  <li><a href="/register">register</a></li>
                  <li><a href="/admin">admin</a></li>
                  <li><a href="/categories">Categories</a></li>
                  <li>much more crap here</li>
              </ul>
          </div>
          <div role="tabpanel" class="tab-pane fade" id="profile">
              TAB2
          </div>
          <div role="tabpanel" class="tab-pane fade" id="messages">
              Tab 3
          </div>
      </div>

    </div>

{% block content %}
{% endblock %}


    <div class="container">
    <!-- Footer -->
        <footer>
            <div class="row">
                <div class="col-lg-8">
                    <p>Copyright</p>
                </div>
            </div>
        </footer>
    </div>

    <!-- jQuery -->
    <script src="js/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>

</body>

</html>

当我去

http://127.0.0.1:5000/any_url_not_routed ,如下所示,

它 returns 一个 404 error 正如预期的那样,但是当我在末尾放置一个斜杠时,

http://127.0.0.1:5000/any_url_not_routed/ ,如下所示,

它呈现相同的页面但没有 css 和 js。 类似的事情发生在其他页面,如 /register、/login 等。此外,浏览器会在末尾放置自动斜杠。

请告诉我发生了什么事?我可以在这里 link 更多代码。

您的 "css/bootstrap.min.css" 在缺少尾部斜线时导致 "/css/bootstrap.min.css",在尾部斜线存在时导致 "/any_url_not_routed/css/bootstrap.min.css"

您应该使用绝对路径 "/css/bootstrap.min.css" 来引用资源,就像您已经对 "/register".

这样的操作所做的一样

您可以通过查看您服务器的请求日志来核实这件事的真实性。