您的位置:首页 > 其它

jinja2 字符串替换

2015-12-29 22:59 477 查看
事情是这样的,我从模板通过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(s, old, new, count=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 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jinja2 substr