jinja2 字符串替换

事情是这样的,我从模板通过url传给后端一个参数,<a href="/delete/{{ result['post']['value'] }}">

参数result['post']['value']的值是形如这样的http://210.30.96.213:8080/post/688523 

开始一直找不到页面,我以为代码写错后来试了半天发现url里不能有/啊雾草 


然后我就试图重建RDF,重新上传数据直接把post的值存储为数字不带斜杠例如688532 

未果。

因为post-hastag-tag这个三元组中post必须要有地址 ,不然你上传上去也是这样的<update-base/688532> 

很奔溃啊有没有 


所以我只能在处理这个result['post']['value']字符串变量上想办法了,比如怎么生成只取最后六位的子串 

查了一顿未果之后我又想起了上午那个靠谱的jinja2文档 

http://docs.jinkan.org/docs/jinja2/templates.html

于是检索了一下substr终于被我找到了,呜呜呜

replace ( soldnewcount=None )

Return a copy of the value with all occurrences of a substring replaced with a new one. The first argument is the substring that should be replaced, the second is the replacement string. If the optional third argument count is given, only the firstcount occurrences are replaced:

{{ "Hello World"|replace("Hello", "Goodbye") }}
    -> Goodbye World

{{ "aaaaargh"|replace("a", "d'oh, ", 2) }}
    -> d'oh, d'oh, aaargh

于是我就可以这样替换 
<a href="/delete/{{ result['post']['value']|replace("http://210.30.96.213:8080/post/", "") }}">
这样传过去的参数就是688532 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值