Django — 4 steps with Mathfilters

Farjanul Nayem
2 min readNov 23, 2021

--

Django

As I mentioned earlier there are 5 questions and I gave 4 correct answers and the last one was incorrect. So in the wrong column the number should be 1 which is 5–4 = 1. But it shows 5 which is the total number of questions. Immediately after, I thought about solving it in the front end. And searched for whether Django templates can do this or not and found an unregistered tag library called django-mathfilters. This library can perform certain arithmetic operations in the template such as — subtraction, addition, multiplication, division, absolute value, modulo, integer (floor) division.

I show you here how to use it, with my example above which is 5–4 = 1.

- pip install django-mathfilter

Step 1: First we need to install it with this command or follow the Official Documentation

INSTALLED_APPS.

Step 2: In our project settings file we add this library in

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mcq',
'quiz',
'mathfilters'
]

Step 3: We need to load the math filters at the top of our template which is one html -file.

See full article here: https://www.djuices.com/django-4-steps-with-mathfilters/

Originally published at https://www.djuices.com on November 23, 2021.

--

--