我不知道我的浏览器或编译器是否有问题,我在 replit 上尝试了我的代码,并且 CSS 静态文件正在反映。它也在我朋友的设备上进行了测试,并且可以正常工作。然而就我而言,它仍然不起作用。我硬刷新了好多次。我使用了 pycharm 并在边缘运行它。
主要.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def hello_world():
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True)
静态/styles.css
body {
background-color: purple;
}
h1 {
color: blue;
}
模板/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="static/styles.css">
<!-- <link rel='stylesheet' type='text/css' href="{{ url_for('static', filename='styles.css') }}" **I tried this too />-->
</head>
<body>
<h1>I'm Ben</h1>
</body>
</html>
我也尝试过,似乎也有效。也许尝试清除浏览器缓存,尝试不同的浏览器,甚至删除 Flask 包并重新安装。 Python版本:3.11.4 网络浏览器:火狐