According to the Flask readme,蓝图静态文件可以在blueprintname/static
上访问。但由于某些原因,它不起作用。
我的蓝图是这样的:
app/frontend/views.py
:etc...
app/frontend/js/app.js
= Blueprint(' frontend ',__name__,template_folder='templates',static_folder='static') @frontend.route('/') 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/
文件夹中什么也没有。
我不能访问我的蓝图中的任何静态文件!Flask me说它应该行得通!
静态管理=蓝图(‘admin’,__name__,
_folder=‘static’)
默认情况下,路径的最右侧部分是它在web上显示的位置。因为文件夹在这里被称为static,所以它将在blueprint + /static的位置可用。假设蓝图已为静态注册,则静态文件夹将位于/ /admin / static。
发布于 2014-03-03 17:14:44
我在static_url_path参数中包含了一个参数,以确保Blueprint的静态路径不会与主应用程序的静态路径冲突。
例如:
admin = Blueprint('admin', __name__, static_folder='static', static_url_path='/static/admin')
发布于 2014-03-03 17:15:54
您可能将您的蓝图注册为位于站点的根目录:
app.register_blueprint(core, url_prefix='')
但是Blueprint中的static
视图与所有其他Blueprint视图没有什么不同;它使用该URL值来使url_prefix
惟一。
核心static
视图也处于活动状态,因此您现在有两个想要处理/static/
URL的路由。因此,如果您在没有URL前缀的情况下注册Blueprint,则必须为这两个前缀中的一个提供唯一路径。
要么给蓝图一个自定义的Flask值,要么给核心static_url_path
app
。
发布于 2018-03-02 10:45:42
对我来说,它是这样初始化blueprint的:
configuration = Blueprint('configuration', __name__, template_folder='templates',static_folder='static')
然后引用像这样的静态文件
href="{{ url_for('.static', filename='css/base.css') }}"
在href中的static之前有一个点。
https://stackoverflow.com/questions/22152840
复制相似问题