Skip to main content

A set of simple math filters for Django

Project description

Build status

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

Uploaded Source

File details

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

File metadata

File hashes

Hashes for django-mathfilters-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2f51565736e9c17919e43ceaa0792589139ffc1008d19b01feebc8825c60cddd
MD5 e7b7819d1fb35cec9becdc70c835782a
BLAKE2b-256 6b1a6dcc40115faf929c475dcb8150c47f0e9103105ec9a2cdec90bedae6a9b8

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