python - How to subtract two fields in django templates? -
before marking duplicate numerous other questions on so, read. have 3 fields, credit available
, credit limit
, credit balance
. i'm getting values credit limit
, credit balance
database. display credit available
, want credit limit
-credit balance
. tried doing other answers on stackoverflow, did not work
<tr> <td style="text-align:center">{{ credit.credit_limit|add:"-{{credit.credit_balance}}" }}</td> <td align="center">{{ credit.credit_limit}}</td> <td align="center">{{ credit.credit_balance }}</td> </tr>
can without writing new template tags or using mathfilter module?
the correct syntax adding limit balance is
{{ credit.credit_limit|add:credit.credit_balance }}
as per https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#add. unfortunately, putting minus sign in front of variable won't work make negative.
you use custom template filter, straightforward. might this:
@register.filter(name='subtract') def subtract(value, arg): return value - arg
then in template (after loading it) be:
{{ credit.credit_limit|subtract:credit.credit_balance }}
Comments
Post a Comment