[PYTHON] How to do arithmetic with Django template

Introduction

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.

reference

django-mathfilters · PyPI

environment

Python(3.6.2) Django(2.1.7)

Installation method

Install django-mathfilters

pip install django-mathfilters

Added math filters to INSTALLED_APPS

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 from template

- load mathfilters

Now you can perform arithmetic processing on the template.

Calculation method

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

How to do arithmetic with Django template
[Tips] How to do template extends when creating HTML with django
How to get started with Django
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to do portmanteau test with python
How to do Server-Sent Events in Django
How to do multi-core parallel processing with python
How to develop a cart app with Django
How to implement "named_scope" of RubyOnRails with Django
Do Django with CodeStar (Python3.6.8, Django2.2.9)
How to do hash calculation with salt in Python
Do Django with CodeStar (Python3.8, Django2.1.15)
How to deal with Django's Template Does Not Exist
How to do zero-padding in one line with OpenCV
How to update with SQLAlchemy?
How to cast with Theano
Do AES encryption with DJango
How to Alter with SQLAlchemy?
How to separate strings with','
Things to do when you start developing with Django
How to RDP with Fedora31
How to Delete with SQLAlchemy?
How to handle static files when deploying to production with Django
How to check ORM behavior in one file with django
[Django] How to give input values in advance with ModelForm
How to resolve CSRF Protection when using AngularJS with Django
How to do the initial setup from Django project creation
How to do Bulk Update with PyMySQL and notes [Python]
How to cancel RT with tweepy
Steps to develop Django with VSCode
[Python] How to do PCA in Python
Python: How to use async with
[Django] How to test Form [TDD]
What to do with Magics install
How to use virtualenv with PowerShell
How to deal with imbalanced data
How to install python-pip with ubuntu20.04LTS
Common html to rent with Django
How to deal with imbalanced data
To do tail recursion with Python2
How to get started with Scrapy
How to get started with Python
How to reflect CSS in Django
How to deal with DistributionNotFound errors
What to do with PYTHON release?
How to Data Augmentation with PyTorch
How to use FTP with Python
Make a filter with a django template
How to calculate date with python
I want to do ○○ with Pandas
How to write Django1.9 environment-independent wsgi.py
How to install mysql-connector with pip3
How to INNER JOIN with SQLAlchemy
How to install Anaconda with pyenv
How to deal with "You have multiple authentication backends configured ..." (Django)
Django # 2 (template)
Here's a brief summary of how to get started with Django
Step notes to get started with django
[Blender] How to set shape_key with script
How to title multiple figures with matplotlib