django模板过滤器,使用2个或多个类似管道的过滤器 [英] django template filter ,use 2 or more filter like pipe

查看:413
本文介绍了django模板过滤器,使用2个或多个类似管道的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在模板上使用多个过滤器,如下所示:

I want to use more than one filter on template like below:

value: {{ record.status|cut:"build:"|add:"5" }}

其中record.status将被构建:n,0< n < 100 但我想将此值添加为基值5.

where record.status would be build:n, 0 < n< 100 but I want to add this value a base value 5.

我尝试了上面的代码,它仅在第一个过滤器上生效, 所以我没有得到加5的值.

I tried above code, it only take effect on the first filter, so I did not get the value plus 5.

django仅支持一个过滤器吗? 谢谢

Does django only support one filter? Thanks

推荐答案

首先,您的问题"django仅支持一个过滤器吗?"的答案.就是它 Django确实支持几乎无限数量的链接过滤器(取决于您的平台和编写该数量的链接过滤器的能力,当然是=).以一些代码为例(不是证明,但很有道理),它实际上是模板'{{ x|add:1|add:1|...10000 in all...|add:1 }}'

First, the answer for your question "Does django only support one filter?" is that Django does support almost unlimited number of chained filters (depends on your platform and ability to write that number of chained filters of course =) . Take some code for example (not proof but it makes sense), it is actually a template '{{ x|add:1|add:1|...10000 in all...|add:1 }}'

>>> from django.template import *
>>> t = Template('{{ x|'+'|'.join(['add:1']*10000)+' }}')
>>> t.render(Context({'x':0}))
u'10000'

第二,请检查模板以确保您使用的是内置版本的cutadd;还检查cut之后的输出值,以确保可以将其强制为int w/o引发异常.
我刚刚检查并发现,即使Django 0.95也支持这种用法:

Second, please check the template to ensure that you are using built-in version of cut and add; also check the output value after the cut to ensure it can be coerced to int w/o raising exception.
I've just checked and found that even the Django 0.95 supports this usage:

def add(value, arg):
    "Adds the arg to the value"
    return int(value) + int(arg) 

这篇关于django模板过滤器,使用2个或多个类似管道的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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