Skip to main content

A set of simple math filters for Django

Project description

Build status PyPI download stats

django-mathfilters is a Python 3 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
  • intdiv – integer (floor) division
  • abs – absolute value
  • mod – modulo
  • addition – replacement for the add filter with support for float / decimal types

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>

Version Support

This module officially supports Python 3.5+ as well as PyPy3. Support for Python 3.3 and 3.4 is provided on best-effort basis, but there are no CI tests for it.

Supported Django versions are 1.11+, 2.x and 3.x.

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.

Files for django-mathfilters, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size django_mathfilters-1.0.0-py3-none-any.whl (5.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-mathfilters-1.0.0.tar.gz (5.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page