有关flask的static文件夹,如何设置在blueprint的根目录下呢?

flask把template和static文件分别放在各自文件夹下是很不错的管理方法,但是有很多css和js包里会引用一下图啊什么的,会要求是跟着访问页文件的,比如这样:

  var line1=$('<div class="mobanthum empty" style="background:rgba(68,195,181,0.9)"> <img src="./res/line_demo1.jpg" style="width:100%;height:100%"/> </div>');

如果你的blueprint和原来的写法一样,比如这样:

bp = Blueprint("wx_template1", __name__, template_folder="templates", static_folder='static')

而你肯定在template文件里是这样写的:

<link rel="stylesheet" href="{{ url_for('wx_template2.static',filename='css/pageswiper.css') }}">

那就郁闷了,这个img肯定是访问不到的。

例如你的view里这样写:

@bp.route('/editor/')
def editor():
    return render_template('wx_template2/editor.html')

因为这个href的路径是 static/css/pagewiper.css,而里面引用的img文件的路径就是 editor/res/line_demo1.jpg,肯定就访问不到了。


所以,现在我们就要把css文件夹设置到blueprint下。

查了很多资料,秘密在Blueprint的定义时。这样写就OK了:

bp = Blueprint("wx_template2", __name__, template_folder="templates", static_url_path='', static_folder='')

后面那2个都要定义,然后在template里就可以按原来的写法:

<link rel="stylesheet" href="{{ url_for('wx_template2.static',filename='css/pageswiper.css') }}">

当然这个css文件夹是放在blueprint的根目录下的,就是那个blueprint的template文件夹同级的。

转载于:https://my.oschina.net/klausgao/blog/368212

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值