Flask blueprint静态目录不起作用? [英] Flask blueprint static directory does not work?
问题描述
blueprintname /静态
。但是出于某种原因,这是行不通的。 我的蓝图是这样的:
$ ul
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。
< script src ={{url_for(' frontend.static',文件名='js / app.js')}}>< / script>
输出是
< script src =/ static / js / app.js>< / script>
哪一个也不行。我的根目录中没有任何内容 app / static /
文件夹。
我无法访问蓝图中的任何静态文件!
$ b
admin =蓝图('admin',__name__, static_folder ='static')
默认情况下,路径的最右边部分是网页。由于该文件夹在此处称为静态,因此它将在蓝图+ /静态位置可用。假设蓝图注册为/ admin,那么静态文件夹将位于/ admin / static。 p>我在static_url_path参数中包含一个参数,以确保Blueprint的静态路径不会与主应用程序的静态路径发生冲突。
例如:
admin =蓝图('admin',__name__,static_folder ='static',static_url_path ='/ static / admin')
According to the Flask readme, blueprint static files are accessible at
blueprintname/static
. But for some reason, it doesn't work.My blueprint is like this:
app/frontend/views.py
:frontend = Blueprint('frontend', __name__, template_folder='templates', static_folder='static') @frontend.route('/') etc...
app/frontend/js/app.js
: my javascript- Blueprint registered in Flask app (routes work and everything)
When I go to
abc.com/frontend/static/js/app.js
, it just gives a 404.When I follow the Flask readme to get my static files:
<script src="{{url_for('frontend.static', filename='js/app.js')}}"></script>
The output is
<script src="/static/js/app.js"></script>
Which doesn't work either. There's nothing in my root
app/static/
folder.I can't access any static files in my blueprint! Flask read me says that it should work!
admin = Blueprint('admin', __name__, static_folder='static')
By default the rightmost part of the path is where it is exposed on the web. Because the folder is called static here it will be available at the location of the blueprint + /static. Say the blueprint is registered for /admin the static folder will be at /admin/static.
解决方案I include an argument to the static_url_path parameter to ensure that the Blueprint's static path doesn't conflict with the static path of the main app.
e.g:
admin = Blueprint('admin', __name__, static_folder='static', static_url_path='/static/admin')
这篇关于Flask blueprint静态目录不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!