According to the Flask readme,蓝图静态文件可在blueprintname/static
访问。但由于某种原因,它不起作用。
我的蓝图是这样的:
app/frontend/views.py
:
frontend = Blueprint('frontend', __name__, template_folder='templates', static_folder='static')
@frontend.route('/') etc...
app/frontend/js/app.js
:我的javascript当我去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。
我在static_url_path参数中包含一个参数,以确保Blueprint的静态路径不会与主应用程序的静态路径冲突。
e.g:
admin = Blueprint('admin', __name__, static_folder='static', static_url_path='/static/admin')
您可能已将您的蓝图注册为您网站的根目录:
app.register_blueprint(core, url_prefix='')
但蓝图中的static
视图与您的所有其他蓝图视图没有什么不同;它使用url_prefix
值使URL唯一。
核心static
视图也是活动的,因此您现在有两条路径想要处理/static/
URL。因此,如果您在没有URL前缀的情况下注册Blueprint,则必须为这两个中的一个提供唯一的路径。
要么给蓝图一个自定义的static_url_path
值,要么给核心Flask app
。
它通过初始化像thisconfiguration = Blueprint('configuration', __name__, template_folder='templates',static_folder='static')
这样的蓝图,然后引用像thishref="{{ url_for('.static', filename='css/base.css') }}"
这样的静态文件来为我工作。在href中静态之前有一个点。