By using a module called django-mathfilters, you can perform arithmetic processing on the template. In this article, I'll show you how to implement it.
Python(3.6.2) Django(2.1.7)
pip install django-mathfilters
settings.py
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'sass_processor',
'storages',
'mathfilters', #← Add
]
- load mathfilters
Now you can perform arithmetic processing on the template.
--sub – Subtraction --addition – addition --mul – Multiplication --div – division --intdiv – Integer division --abs – Absolute value --mod – Surplus
python:template_file.html.haml
- load mathfilters
%ul
%li
10 - 2 = {{ 10 | sub:2 }} # 8
%li
10 + 5 = {{ 10 | addition:5 }} # 15
%li
10 × 3 = {{ 10 | mul:3 }} # 30
%li
10 ÷ 2 = {{ 10 | div:2 }} # 5
%li
| -10 | = {{ -10 | abs }} # 10
%li
20 ÷ 3 = {{ 20 | mod:3 }} # 2
Recommended Posts