我使用Python Flask构建了后端,并使用了没有CSS的基本HTML模板对其进行了测试,并且没有任何问题。但是,当我尝试实现Bootstrap时,它不会加载CSS。我已经将css和js文件放入一个静态文件夹中。

这是Bootstrap 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">
    <!-- The above 3 meta tags *must* come first in the head; any other head     content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Simple Safety</title>

    <!-- Bootstrap core CSS -->
    <link href="{{ url_for('static', filename ='bootstrap.min.css') }}" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="{{ url_for('static', filename =ie10-viewport-bug-workaround.css') }}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="{{ url_for('static', filename ='style.css') }}" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]
    <script type="text/javascript" src="{{ url_for('static', filename = 'ie-emulation-modes-warning.js') }}"></script>-->

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js">    </script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js">    </script>
    <![endif]-->
    </head>

<!--Main Content-->

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="{{url_for('https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/', filename='jquery.min.js') }}"></script>
    <script>window.jQuery || document.write("<script type='text/javascript' src='{{ url_for('static', filename='jquery.min.js') }}''><\/script>")</script>
    <script type="text/javascript" src="{{ url_for('static', filename ='bootstrap.min.js') }}"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script type="text/javascript" src="{{ url_for('static', filename='ie10-viewport-bug-workaround.js') }}"></script>
  </body>
</html>

最佳答案

我意识到我已经将“静态”文件夹放置在“模板”文件夹中,这是我仅使用前端应用程序的一种旧习惯。您需要将“ static”文件夹放置在模板文件夹所在的目录中。

关于python - 无法使用Flask加载CSS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45939151/

10-09 17:59
查看更多