Skip to main content

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.

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

django-mathfilters-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

File details

Details for the file django-mathfilters-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django-mathfilters-0.1.0.tar.gz
Algorithm Hash digest
SHA256 276bff95f2c36722911b551be2f5ead96efaa17bec1bdbf6fedab5bbe441cdf0
MD5 e8b6801c06527a1f97553ea9373eebf4
BLAKE2b-256 d82c2b5fc4010692e6889fd9e7ca4d73323ff64b50e92b6bc44527ea56a4c80f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page