Is there a filter for divide for Django Template?

Using a custom filter:

register = template.Library()

@register.filter
def divide(value, arg):
    try:
        return int(value) / int(arg)
    except (ValueError, ZeroDivisionError):
        return None

There is not it. But if you are a little hacker....

http://slacy.com/blog/2010/07/using-djangos-widthratio-template-tag-for-multiplication-division/

to compute A*B: {% widthratio A 1 B %}

to compute A/B: {% widthratio A B 1 %}

to compute A^2: {% widthratio A 1 A %}

to compute (A+B)^2: {% widthratio A|add:B 1 A|add:B %}

to compute (A+B) * (C+D): {% widthratio A|add:B 1 C|add:D %}

Also you can create a filter to division in 2 minutes

Tags:

Python

Django