【问题标题】:Css Error "GET /static/%20style.css HTTP/1.1" 404 - Using Pycharm 3.8.1Css 错误“GET /static/%20style.css HTTP/1.1”404 - 使用 Pycharm 3.8.1
【发布时间】:2020-07-15 01:39:38
【问题描述】:

我花了将近一天的时间试图弄清楚为什么我的 css 文件不能与我的 html 文件一起使用。所有堆栈溢出的答案都对我不起作用。

在这一点上我能得到任何帮助,我将不胜感激。我正在运行安装了烧瓶并使用 pycharm 的 python 3.8.1。 Click here to see my file structure screenshot, 这是主要的python文件代码:

 from flask import Flask , render_template, 



app = Flask(__name__)


@app.route('/')  
def index( ):
    return render_template("index.html" )  

if __name__ == '__main__':  
    app.run( debug=True)    

这是 index.html 文件代码,我希望 CSS 显示在

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <h1> Title </h1>
<h2>Second headline</h2>
    <body>This is an orange test6</body>
</head>
<body>
 <link rel="stylesheet" type="text/css" href=" {{ url_for('static', filename=' style.css') }}">
</body>
</html>

这是我在 pycharm 中运行文件时看到的错误:

“GET / HTTP/1.1” 200 - “GET /static/%20style.css HTTP/1.1”404 -

这里是 CSS 代码:

h1{
    color: yellow;
}
body {
    color: orange;
}
h2 {
    color: pink;
}

【问题讨论】:

    标签: html css flask pycharm http-status-code-404


    【解决方案1】:

    去掉多余的空格

    href=" {{ url_for('static', filename=' style.css') }}">
          ^                               ^
    

    第二个是导致问题的原因。第一个只是糟糕的形式。

    【讨论】:

    • 我在运行程序后看到了一些不同的东西:“GET /static/style.css HTTP/1.1”404 -
    • 你进入了另一个问题。你的项目中有static/style.css吗?
    • 是的,先生,我有一个静态/CSS/style.css 的顺序。
    • CSS 大写?如果是这样,你需要filename='CSS/style.css'
    • 好的,感谢提醒,仍然无法正常工作,但我会继续努力。
    【解决方案2】:

    我想指出几件事。
    首先,您在导入的地方有一个尾随逗号,有时它会弄乱程序。
    其次,尝试在 url_for 中指定子目录 CSS 并删除文件名开头的空格,这解决了我的问题:

    <link rel="stylesheet" type="text/css" href="{{ url_for('static', 
    filename='css/style.css') }}">
    

    【讨论】:

    • 好的,谢谢你的提示,但还是不行。
    • @BrianC 听起来很奇怪,你能告诉我在使用我的链接标签后你得到了什么错误吗?因为我建立了和你一样的文件夹树,它工作正常
    • 127.0.0.1 - - [2020 年 4 月 2 日 21:12:56] “GET / HTTP/1.1”200 - 127.0.0.1 - - [2020 年 4 月 21:12:56 ] "GET /static/css/style.css HTTP/1.1" 404
    猜你喜欢
    • 1970-01-01
    • 2020-12-28
    • 2020-03-03
    • 1970-01-01
    • 2016-04-11
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多