12👍
✅
I would like to suggest installing django-mathfilters.
Then you can simply use the abs
filter like this:
{% for balance in balances %}
{{ balance.amount|abs }}
{% endfor %}
8👍
If you don’t want/can’t install django-mathfilters
You can make a custom filter quite easily:
from django import template
register = template.Library()
@register.filter(name='abs')
def abs_filter(value):
return abs(value)
- Django bulk create objects from QuerySet
- How do I use Django signals with an abstract model?
- How do I simulate connection errors and request timeouts in python unit tests
- Can't open file 'django-admin.py': [Errno 2] No such file or directory
7👍
This works without adding django-mathfilters but it’s not a very good practice.
{% if balance.amount < 0 %}
{% widthratio balance.amount 1 -1 %}
{% else %}
{{ balance.amount }}
{% endif %}
Widthratio is meant for creating bar charts but can be used for multiplication
- I get an error when return a queryset objects: Cannot resolve expression type, unknown output_field
- Django- getting a list of foreign key objects
- How do I get Django forms to show the html required attribute?
- Is the Global Request variable in Python/Django available?
4👍
from this SO:
{% if qty > 0 %}
Please, sell {{ qty }} products.
{% elif qty < 0 %}
Please, buy {{ qty|slice:"1:" }} products.
{% endif %}
Source:stackexchange.com