How to do math in a Django template?

后端 未结 3 818
傲寒
傲寒 2020-12-13 02:08

I want to do this:

100 - {{ object.article.rating_score }} 

So for example, the output would be 20 if {{ object.article.

相关标签:
3条回答
  • 2020-12-13 02:27

    Use django-mathfilters. In addition to the built-in add filter, it provides filters to subtract, multiply, divide, and take the absolute value.

    For the specific example above, you would use {{ 100|sub:object.article.rating_score }}.

    0 讨论(0)
  • 2020-12-13 02:28

    You can use the add filter:

    {{ object.article.rating_score|add:"-100" }}
    
    0 讨论(0)
  • 2020-12-13 02:29

    Generally it is recommended you do this calculation in your view. Otherwise, you could use the add filter.

    0 讨论(0)
提交回复
热议问题