Registry / web-framework / django-mathfilters

django-mathfilters

JSON →
library1.0.0pypypi✓ verified 24d ago

django-mathfilters is a Python 3 module that provides a set of simple mathematical template filters for Django. It extends Django's built-in 'add' filter by offering operations like subtraction, multiplication, division, integer division, absolute value, and modulo. It supports 'int', 'float', and 'Decimal' types, allowing for basic arithmetic directly within templates. The current version is 1.0.0, and it serves as a stable utility for common math operations not natively supported by Django's template language.

pip install django-mathfilters
INSTALL
IMPORT
SIG · DJANGO-MATHFILTERS
D
django-mathfilters
web-frameworkpythonv1.0.0
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mathfilters
{% load mathfilters %}
Used as a template tag library, not a Python import.

After installing the package, add 'mathfilters' to your `INSTALLED_APPS` in your Django project's `settings.py`. Then, in any Django template where you want to use the math filters, add `{% load mathfilters %}` at the top. You can then apply the filters to variables or literal values. Ensure values are numeric (int, float, Decimal) for correct operation.

{% load mathfilters %} <p>Subtraction: {{ 10|sub:3 }}</p> <p>Multiplication: {{ 5|mul:2.5 }}</p> <p>Division: {{ 10|div:4 }}</p> <p>Integer Division: {{ 10|intdiv:4 }}</p> <p>Absolute Value: {{ -7|abs }}</p> <p>Modulo: {{ 10|mod:3 }}</p> <p>Addition (float/decimal support): {{ 5.5|addition:2.3 }}</p>
Debug
Known issues
breakingIn versions prior to 0.4.0, the 'addition' filter might not have been available or behaved differently, causing 'TemplateSyntaxError'.
fix
Upgrade django-mathfilters to version 0.4.0 or higher. The current version 1.0.0 includes the 'addition' filter with float/decimal support.
affects: <0.4.0
gotchaValues passed to math filters must be compatible numeric types (int, float, or Decimal). Passing non-numeric values (e.g., strings that cannot be converted) will likely result in a runtime error or an empty string output in the template.
fix
Ensure that the variables or literals used with mathfilters are always of a numeric type. Perform type conversions in your Django views or models if necessary before passing data to the template.
affects: All
gotchaForgetting to add 'mathfilters' to your `INSTALLED_APPS` in `settings.py` will prevent the template tags from being recognized, leading to a `TemplateSyntaxError` for 'mathfilters' not being a registered tag library.
fix
Ensure `'mathfilters'` is present in your `INSTALLED_APPS` list in `settings.py`: `INSTALLED_APPS = ['...', 'mathfilters', ...]`.
affects: All
Upgrade
Version history
1.0.0latest on PyPI · released Feb 10, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
django-mathfilters — pip install django-mathfilters · libregistry