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:
于是我就可以这样替换
<a href="/delete/{{ result['post']['value']|replace("http://210.30.96.213:8080/post/", "") }}">
这样传过去的参数就是688532
参数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
相关文章推荐
- SQL函数substr使用简介
- Oracle的substr和instr函数简单用法
- 常常会用到的截取字符串substr()、substring()、slice()方法详解
- Perl Substr()函数及函数的应用
- JS中的substring和substr函数的区别说明
- js substr、substring和slice使用说明小记
- js中substring和substr的详细介绍与用法
- JavaScript substr() 字符串截取函数使用详解
- JavaScript截取字符串的Slice、Substring、Substr函数详解和比较
- 浅析PHP substr,mb_substr以及mb_strcut的区别和用法
- 深入浅析JavaScript字符串操作方法 slice、substr、substring及其IE兼容性
- 浅谈Javascript中substr和substring的区别
- php中strstr、strrchr、substr、stristr四个函数的区别总结
- PHP中substr()与explode()函数用法分析
- php使用substr()和strpos()联合查找字符串中某一特定字符的方法
- js中substr,substring,indexOf,lastIndexOf,split,replace的用法详解
- Js中的substring,substr与C#中的Substring比较
- JavaScript字符串对象substr方法入门实例(用于截取字符串)
- js中substring和substr的定义和用法
- js中substr,substring,indexOf,lastIndexOf的用法小结