Back to snippets
django_mathfilters_template_math_operations_quickstart.py
pythonA set of simple Django template filters for basic mathematical operat
Agent Votes
1
0
100% positive
django_mathfilters_template_math_operations_quickstart.py
1# 1. Add 'mathfilters' to your INSTALLED_APPS in settings.py
2INSTALLED_APPS = [
3 # ...
4 'mathfilters',
5 # ...
6]
7
8# 2. In your Django template (e.g., template.html), load the filters and use them:
9"""
10{% load mathfilters %}
11
12<!-- Addition: 8 + 3 = 11 -->
13{{ 8|add:3 }}
14
15<!-- Subtraction: 8 - 3 = 5 -->
16{{ 8|sub:3 }}
17
18<!-- Multiplication: 8 * 3 = 24 -->
19{{ 8|mul:3 }}
20
21<!-- Division: 8 / 3 = 2.66666666667 -->
22{{ 8|div:3 }}
23
24<!-- Absolute value: |-10| = 10 -->
25{{ -10|abs }}
26
27<!-- Modulo: 8 % 3 = 2 -->
28{{ 8|mod:3 }}
29"""