{"id":3461,"library":"django-mathfilters","title":"Django Mathfilters","description":"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.","status":"maintenance","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/dbrgn/django-mathfilters","tags":["django","template-filters","mathematics","frontend","utility"],"install":[{"cmd":"pip install django-mathfilters","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"Used as a template tag library, not a Python import.","symbol":"mathfilters","correct":"{% load mathfilters %}"}],"quickstart":{"code":"{% load mathfilters %}\n\n<p>Subtraction: {{ 10|sub:3 }}</p>\n<p>Multiplication: {{ 5|mul:2.5 }}</p>\n<p>Division: {{ 10|div:4 }}</p>\n<p>Integer Division: {{ 10|intdiv:4 }}</p>\n<p>Absolute Value: {{ -7|abs }}</p>\n<p>Modulo: {{ 10|mod:3 }}</p>\n<p>Addition (float/decimal support): {{ 5.5|addition:2.3 }}</p>","lang":"django_template","description":"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."},"warnings":[{"fix":"Upgrade django-mathfilters to version 0.4.0 or higher. The current version 1.0.0 includes the 'addition' filter with float/decimal support.","message":"In versions prior to 0.4.0, the 'addition' filter might not have been available or behaved differently, causing 'TemplateSyntaxError'.","severity":"breaking","affected_versions":"<0.4.0"},{"fix":"Ensure that the variables or literals used with mathfilters are always of a numeric type. Perform type conversions in your Django views or models if necessary before passing data to the template.","message":"Values passed to math filters must be compatible numeric types (int, float, or Decimal). Passing non-numeric values (e.g., strings that cannot be converted) will likely result in a runtime error or an empty string output in the template.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure `'mathfilters'` is present in your `INSTALLED_APPS` list in `settings.py`: `INSTALLED_APPS = ['...', 'mathfilters', ...]`.","message":"Forgetting to add 'mathfilters' to your `INSTALLED_APPS` in `settings.py` will prevent the template tags from being recognized, leading to a `TemplateSyntaxError` for 'mathfilters' not being a registered tag library.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}