Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a filter for divide for Django Template?

Tags:

python

django

I noticed there is built-in add filter, but I wasn't able to find divide.

I am new to Django and not sure if there is a such filter.

like image 706
Sam Avatar asked Dec 09 '11 15:12

Sam


People also ask

What does filter do in Django template?

Django Template Engine provides filters which are used to transform the values of variables;es and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own need.

What does {{ name }} this mean in Django templates?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

What is safe filter in Django template?

The safe filter indicates that the value is known to be safe and therefore does not need to be escaped. For example, given the following: blurb = '<p>You are <em>pretty</em> smart!</ p>' This would return unescaped HTML to the client: {{ blurb|safe }}


2 Answers

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

like image 103
Goin Avatar answered Sep 24 '22 20:09

Goin


Using a custom filter:

register = template.Library()  @register.filter def divide(value, arg):     try:         return int(value) / int(arg)     except (ValueError, ZeroDivisionError):         return None 
like image 23
sidarcy Avatar answered Sep 24 '22 20:09

sidarcy