A set of simple math filters for Django
Project description
django-mathfilters is a pip-installable module that provides different simple math filters for Django.
Django provides an add template filter, but no corresponding subtracting, multiplying or dividing filters.
Django ticket #361 has been closed as wontfix, so I had to create an alternative that is easy to install in a new Django project.
It currently supports int, float and Decimal types, or any other type that can be converted to int or float.
Installation
$ pip install django-mathfilters
Then add mathfilters to your INSTALLED_APPS.
Usage
You need to load mathfilters at the top of your template. The script provides the following filters:
sub – subtraction
mul – multiplication
div – division
abs – absolute value
Example:
{% load mathfilters %} ... <h1>Basic math filters</h1> <ul> <li>8 + 3 = {{ 8|add:3 }}</li> <li>13 - 17 = {{ 13|sub:17 }}</li> {% with answer=42 %} <li>42 * 0.5 = {{ answer|mul:0.5 }}</li> {% endwith %} {% with numerator=12 denominator=3 %} <li>12 / 3 = {{ numerator|div:denominator }}</li> {% endwith %} <li>|-13| = {{ -13|abs }}</li> </ul>
License
MIT License, see LICENSE file.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-mathfilters-0.1.3.tar.gz
.
File metadata
- Download URL: django-mathfilters-0.1.3.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42c9aff55cafdf9a68ebe6791553f9f3856a52b04324b3a12168a88256a2dddc |
|
MD5 | e51f52a9330b947ea2b24329369d63cd |
|
BLAKE2b-256 | 9a54293cceb73e544275a74f6e0c0ebb66e1611924303a2d2ea13977f987236f |