简体   繁体   English

Flask:从不同的文件夹中提供像css这样的静态文件

[英]Flask: Serve static files like css from different folder

given a structure like inside a directory foo: 给定一个像foo目录里面的结构:

/web
    /static
        /css
        /img
/model
runapp.py

How to server static files from web/static/css or /img 如何从web / static / css或/ img服务器静态文件

like 喜欢

<link href="{{ url_for('web/static', filename='css/bootstrap.min.css') }}" rel="stylesheet">

It gives 它给

werkzeug.routing.BuildError
BuildError: ('web/static', {'filename': 'css/bootstrap.min.css'}, None)

I have done 我已经做好了

app = Flask(__name__, static_folder=os.path.join(os.getcwd(),'web','static'), static_url_path='')

but it doesn't work. 但它不起作用。 and btw whats the difference between static_folder and static_url_path ? 和btw有什么区别static_folder和static_url_path?

url_for('web/static') won't work because 'static' is a special blueprint for serving static files. url_for('web/static')不起作用,因为'static'是提供静态文件的特殊蓝图。 So do this: 这样做:

url_for('static', filename='css/bootstrap.min.css')

And set the static folder in your app: 并在您的应用中设置静态文件夹:

app = Flask(__name__, static_folder=os.path.join(os.getcwd(),'web','static'))

static_url_path defaults to the name of static_folder . static_url_path默认为static_folder的名称。 Not sure how setting it to '' would help. 不确定如何设置''会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM