我试着遵循this教程,该教程旨在演示如何更改静态和模板文件夹在根目录中的位置 . 但是我无法让这个例子起作用 . 应用程序运行正常但在查找样式表"GET /static/style.css HTTP/1.1" 404时返回404.所以看起来它可以找到模板而不是样式表 .
我的hello world示例是here及以下 . 我应该使用root_path,还是instance_path或template_folder和static_folder?
api_files
static
style.css
templates
index.html
api.py
api.py来自flask导入Flask,render_template,url_for
# here we can set a different root path
app = Flask(__name__, root_path='api_files/')
@app.route('/')
def index():
"""Render home page."""
return render_template('index.html') # we can render templates as usual
if __name__ == '__main__':
app.run(debug=True)
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<link rel= "stylesheet" type= "text/css" href= {{ url_for('static', filename='style.css') }}>
</head>
<body>
hello world
</body>
</html>
style.css文件
body {
color: red;
}