Flask 蓝图静态目录不起作用? [英] Flask blueprint static directory does not work?

查看:26
本文介绍了Flask 蓝图静态目录不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Flask 自述文件,蓝图静态文件可在 <代码>蓝图名称/静态.但由于某种原因,它不起作用.

我的蓝图是这样的:

  • app/frontend/views.py :

    frontend = Blueprint('frontend', __name__,模板文件夹='模板',static_folder='static')@frontend.route('/') 等...

  • 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>

输出为

这也行不通.我的根 app/static/ 文件夹中没有任何内容.

我无法访问蓝图中的任何静态文件!Flask read me 说它应该可以工作!

<块引用>

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

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

解决方案

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

例如:

admin = Blueprint('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 蓝图静态目录不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆