94
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
34
- Create a
templatetags
package within your Django app:my_app/templatetags/__init__.py
- Create a module within
templatetags
with the following code:
# my_app/templatetags/my_custom_filters.py
from django import template
register = template.Library()
@register.filter
def divide(value, arg):
try:
return int(value) / int(arg)
except (ValueError, ZeroDivisionError):
return None
- In your template, add the following, replacing
my_custom_filters
with the module name in step 2:
{% load my_custom_filters %}
{{ 100|divide:2 }}
See https://docs.djangoproject.com/en/4.1/howto/custom-template-tags/ for more information
- [Django]-Django model constraint for related objects
- [Django]-AssertionError: database connection isn't set to UTC
- [Django]-How to remove all of the data in a table using Django
15
There is a Python module to do math operations in your templates: Django-Mathfilters.
It contains add
as you said, but also div
to divide:
8 / 3 = {{ 8|div:3 }}
- [Django]-Django FileField upload is not working for me
- [Django]-Difference between Django's filter() and get() methods
- [Django]-The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error
6
I would use a custom template, but if you donโt want to you can use the widthratio built in tag,
{% widthratio request.session.get_expiry_age 3600 1 %}
Another example
{% widthratio value 1150000 100 %}
Syntax:
{% widthratio parm1 parm2 parm3 %}
So basically its used for scaling images, but you can use it for division. What it does is: parm1/parm2 * parm3.
Hope this helps, more on widthratio here.
- [Django]-Find object in list that has attribute equal to some value (that meets any condition)
- [Django]-Django, ModelChoiceField() and initial value
- [Django]-Django self-referential foreign key
0
Instead of having the percentage already calculated like in your question, you can use this tag to calculate the percentage in the template from the original value and total value against which your percentage is calculated. It does the job if you only need an integer percentage without precision:
{% widthratio value total_value 100 %}
Ref: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#widthratio
- [Django]-Serving Media files during deployment in django 1.8
- [Django]-Can you migrate backwards to before the first migration in South?
- [Django]-How to check if ManyToMany field is not empty?
-2
You can use divisibleby
Returns True if the value is divisible by the argument.
For example:
{{ value|divisibleby:"3" }}
If value is 21, the output would be True.
You can see django docs
- [Django]-PicklingError: Can't pickle <class 'decimal.Decimal'>: it's not the same object as decimal.Decimal
- [Django]-Location of Django logs and errors
- [Django]-Django: best practice way to get model from an instance of that model