You should use a custom filter for this.
Here's two different ways to do it:
1) You can define a negate
filter:
# negate_filter.py
from django import template
register = template.Library()
@register.filter
def negate(value):
return -value
Then in your template, add the code {% load negate_filter %}
to the top and then replace {{ -qty }}
with {{ qty|negate }}
.
2) You can also replace the entire thing with one buy_sell
filter if you'd like:
# buy_sell_filter.py
from django import template
register = template.Library()
@register.filter
def buy_sell(value):
if value > 0 :
return 'sell %s' % value
else :
return 'buy %s' % -value
Then your template should just be
{% if qty %} Please, sell {{ qty|buy_sell }} products.{% endif %}
You could even include the entire string in the filter and just have then entire template be {{ qty|buy_sell }}.
Both options are reasonable, depending on the rest of your template. For example, if you have a lot of strings that use buy for negative and sell for positive, do the second one.