Hey Henri,
Here you go:

# common/templatetags/mathtags.pyfrom django import templateregister = template.Library()@register.filter()
def divide(n1, n2):
try:
return n1 / n2
except (ZeroDivisionError, TypeError):
return None
@register.filter()
def floor_divide(n1, n2):
try:
return n1 // n2
except (ZeroDivisionError, TypeError):
return None
@register.filter()
def percentof(amount, total):
try:
return '{:.1f}%'.format(amount / total * 100)
except ZeroDivisionError:
return None
@register.filter()
def multiply(n1, n2):
try:
return n1 * n2
except TypeError:
return None

Written by

Full Stack Developer, Team Leader, Independent. More from me at https://hakibenita.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store