Django template filter, use 2 or more filters like pipe

I want to use more than one filter for a template, as shown below:

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

where the .status record will be written: n, 0 <n <100 but I want to add this value to the base value of 5.

I tried the code, it only affects the first filter, so I did not get the value plus 5.

Does django support only one filter? Thanks

+3
source share
2 answers

-, "Does django ?" , Django ( , =). , ( , ), '{{ 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'

-, , , cut add; cut, , / .
, Django 0.95 :

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

. , , :

  • ipdb
  • django/templates/defaultfilters.py, "def add" "import ipdb; ipdb.set_trace()"
  • , , , , .

- .

from django.template import Library

register = Library()

@register.filter
def cut_and_add(value, cut, add):
    value = value.replace(cut, '')
    value = int(value) + add
    return value

, yourapp/templatetags/your_templatetags.py ( yourapp/templatetags/__init__.py - ). :

{% load your_templatetags %}

{{ record.status|cut_and_add:"build:",5 }}

, -. .

+1

All Articles