Django Mathfilters

1.0.0 · maintenance · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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>

view raw JSON →