Flask蓝图静态目录不起作用?

问题描述 投票:20回答:3

According to the Flask readme,蓝图静态文件可在blueprintname/static访问。但由于某种原因,它不起作用。

我的蓝图是这样的:

  • app/frontend/views.pyfrontend = Blueprint('frontend', __name__, template_folder='templates', static_folder='static') @frontend.route('/') etc...
  • app/frontend/js/app.js:我的javascript
  • 在Flask应用程序中注册的蓝图(路线工作和一切)

当我去abc.com/frontend/static/js/app.js时,它只给出了404。

当我按照Flask自述文件获取我的静态文件时:

<script src="{{url_for('frontend.static', filename='js/app.js')}}"></script>

输出是

<script src="/static/js/app.js"></script>

哪个也行不通。在我的根app/static/文件夹中没有任何内容。

我无法访问我的蓝图中的任何静态文件!烧瓶读我说它应该工作!

admin = Blueprint('admin', __name__, static_folder='static')

默认情况下,路径的最右边部分是它在Web上公开的位置。由于该文件夹在此处称为静态,因此它将在蓝图+ / static的位置可用。假设蓝图已注册为/ admin,静态文件夹将位于/ admin / static。

python flask
3个回答
15
投票

我在static_url_path参数中包含一个参数,以确保Blueprint的静态路径不会与主应用程序的静态路径冲突。

e.g:

admin = Blueprint('admin', __name__, static_folder='static', static_url_path='/static/admin')

17
投票

您可能已将您的蓝图注册为您网站的根目录:

app.register_blueprint(core, url_prefix='')

但蓝图中的static视图与您的所有其他蓝图视图没有什么不同;它使用url_prefix值使URL唯一。

核心static视图也是活动的,因此您现在有两条路径想要处理/static/ URL。因此,如果您在没有URL前缀的情况下注册Blueprint,则必须为这两个中的一个提供唯一的路径。

要么给蓝图一个自定义的static_url_path值,要么给核心Flask app


5
投票

它通过初始化像thisconfiguration = Blueprint('configuration', __name__, template_folder='templates',static_folder='static')这样的蓝图,然后引用像thishref="{{ url_for('.static', filename='css/base.css') }}"这样的静态文件来为我工作。在href中静态之前有一个点。

© www.soinside.com 2019 - 2024. All rights reserved.