Flask 中 CSS 文件的 404 错误

raf*_*010 1 html css python flask

我正在尝试为基于 Flask 的网站上的某些元素设置样式,但由于某种原因,我的 css 文件从未加载过。

不过情况很特殊。layout.html使用 path调用时,文件将正常加载/static/css/,但由于某种原因,页面/css/第二次截断路径并生成404错误。

这是我的 Flask 项目的结构:

Directory Structure:

+app
+static
    +css
        style-large.css
        style.css
        style-xlarge.css
+templates
    index.html
    layout.html
Run Code Online (Sandbox Code Playgroud)

片段layout.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block doc_title %} {% endblock %}</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <!--[if lte IE 8]><script src="css/ie/html5shiv.js"></script><![endif]-->
    <script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/skel.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/skel-layers.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/init.js') }}"></script>


    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/skel.css') }}" />
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}" />
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style-xlarge.css') }}" />
    <!-- <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}" /> -->
    <!--[if lte IE 8]><link rel="stylesheet" href="css/ie/v8.css" /><![endif]-->
</head>
Run Code Online (Sandbox Code Playgroud)

特点是这样; 无论我如何改变URL的文件steyl-large.cssstyle.css以及style-xlarge.css,我总是得到一个404错误,看起来像这样:

127.0.0.1 - - [05/Dec/2015 17:34:20] "GET /static/css/style-xlarge.css HTTP/1.1" 304 -
127.0.0.1 - - [05/Dec/2015 17:34:20] "GET /static/css/style.css HTTP/1.1" 304 -
127.0.0.1 - - [05/Dec/2015 17:34:20] "GET /css/style-large.css HTTP/1.1" 404 -
127.0.0.1 - - [05/Dec/2015 17:34:20] "GET /css/style.css HTTP/1.1" 404 -
127.0.0.1 - - [05/Dec/2015 17:34:20] "GET /css/style-xlarge.css HTTP/1.1" 404 -
Run Code Online (Sandbox Code Playgroud)

前两个请求,生成304代码的请求工作正常,几乎可以呈现我页面上的所有内容,但紧随其后的请求却没有。

我不确定为什么会这样。我认为这可能是为什么我的页面上的元素没有被样式化的原因,但我不知道为什么会发生这种情况。

dor*_*oru 5

因此,从您的服务器日志中,前两个链接到 css 文件

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/skel.css') }}" />
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}" />
Run Code Online (Sandbox Code Playgroud)

正常工作,因为他们应该。在第三个链接

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style-xlarge.css') }}" />
Run Code Online (Sandbox Code Playgroud)

你有 css 文件名,style-xlarge.css但在服务器日志中是style-large.css. 您没有指向 的链接style-large.css。要加载您应该使用的所有四个 css 文件

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/skel.css') }}" />
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}" />
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style-large.css') }}" />
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style-xlarge.css') }}" />
Run Code Online (Sandbox Code Playgroud)