Flask 应用程序中的多个静态文件夹和模板文件夹

Multiple static folders & templates folders in flask app

flask app中是否可以有多个静态文件夹?我有一些 .css 和 .js 文件,我只将它们用于某些蓝图,并希望将这些文件与蓝图放在同一目录中。

我的应用程序有这样的结构:

app
|
| blueprints
| |
| | blueprint_1
| | |
| | | views.py
| | | __init__.py
| | 
| | blueprint_2
| | |
| | | views.py
| | | __init__.py
| 
| static
| |
| | css, js, etc.

我想要这样的结构:

app
|
| blueprints
| |
| | blueprint_1
| | |
| | | views.py
| | | __init__.py
| | | static
| | | |
| | | | certain css, js, etc
| | 
| | blueprint_2
| | |
| | | views.py
| | | __init__.py
| | | static
| | | |
| | | | certain css, js, etc
| 
| static
| |
| | css, js, etc.

与模板类似。 当我在模板中使用 .js 文件时,我通过以下方式访问它:

{{url_for('static'), filename='jsfile'}}

好的,我明白了。

根据docs,如果蓝图没有url_prefix,则无法访问蓝图的静态文件夹。

那么解决方案是:

将 url_prefix 添加到蓝图:

some_blueprint = Blueprint('something', __name__, static_folder='static', url_prefix='/something')

在模板中我们引用我们的静态文件夹如下:

{{url_for('something.static', filename=...)}}

我觉得有点奇怪,url_prefix 是它工作所必需的,但无论如何。