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!
As with everything in Python, there is a library for that: django-mathfilters.
Then you can simply use the abs
filter like this:
Please, sell {{ qty|abs }} products.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With