[Django]-How to do math in a Django template?

181👍

You can use the add filter:

{{ object.article.rating_score|add:"-100" }}

42👍

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 }}.

👤Erik

21👍

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

👤bdd

Leave a comment