Multiply by -1 in a Django template
django, django-templates
Solution
You can abuse some string filters:
{% if qty > 0 %}
Please, sell {{ qty }} products.
{% elif qty < 0 %}
Please, buy {{ qty|slice:"1:" }} products.
{% endif %}
or
Please, sell {{ qty|stringformat:"+d"|slice:"1:" }} products.
But you should probably do it in your view or write a custom filter.
Problem
I'd like to always use a positive value of my variable in a Django template. The variable's sign is just a textual meaning: ``` {% if qty > 0 %} Please, sell {{ qty }} products. {% elif qty < 0 %} Please, buy {{ -qty }} products. {% endif %} ``` Of course, `{{ -qty }}` doesn't work. Is there a workaround without passing a second variable containing the absolute value? Something like a template filter that would convert the value to an unsigned integer. Thanks!