Flask无法提供静态文件(CSS)

时间:2020-10-29 11:34:40

标签: python css python-3.x flask

我有一个简单的烧瓶应用程序:

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
    return render_template('basic.html')

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

目录结构(建议):---

\templates
    \basic.html
\static
    \basic.css
basic.py

我已经使用以下方式将HTML中的CSS文件连接了:---

<link rel="stylesheet" href="{{ url_for('static', filename='basic.css') }}">

问题:即使重新加载浏览器后,我对CSS文件所做的更改也没有在我的应用程序/网站中进行,当我在开发人员工具中检查了源代码后,发现样式未应用! 这是我的CSS文件:-

h2{
  font-size: 50px;
  color: red;
  background-color: green;
}
p{
  color: blue;
}
h3{
  font-size: 100px;
  color: green;
  background-color: red;
}
h4{
  color: purple;
  background-color: blue;
}

这就是开发人员工具中显示的内容:-

h1{
  font-size: 50px;
  color: red;
  background: green;
}
p{
  color: blue;
}

就这样!! Here it is:

我也查看了其他答案(Here) 但我无法理解我到底在做什么错!

编辑:在浏览器中也导航到文件后也看不到更改:-

Here:

编辑2:好! 硬刷新方式(Ctrl + Shift + R)是有效的,但每次进行任何更改时我都必须这样做! 没有其他方法

1 个答案:

答案 0 :(得分:0)

正如您所知,这是Flask框架中最大的问题之一。我没有找到解决该问题的技术堆栈。但是您可以清除所有浏览器Cookie ,也可以清除 localhost 历史记录,以在本地主机上运行。另外,如果您更改浏览器,它将可以正常工作。