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.1.tar.gz (3.2 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for django-mathfilters-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2ebdfea1b45d5ab8853f7165477d9ee2c0731d87f67a1a210d509535fc1fb19c
MD5 c92274c024fa5613727adc0ddee754de
BLAKE2b-256 48919d7c9572fba28fcb8ab797d62e4149a9501482a40b056815188e5f84870e

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