the problem is when is use add filter i get a raw result or a round off result like say 10 + 0.2 = 10 only (instead i want it to print 10.2). My models.py and templates file below
models.py
class Shop(models.Model):
price = models.FloatField()
i filled the value of price in database as 5.
home.html
{{ object.price }}
it prints 5 (good) but when i use filter -> {{ object.price|add:0.2 }} it returns 10 (i want 10.2) on my homepage
Django add
template tag will first try to coerce both values to integers.
From Django builtin tags documentation
This filter will first try to coerce both values to integers. If this fails, it’ll attempt to add the values together anyway. This will work on some data types (strings, list, etc.) and fail on others. If it fails, the result will be an empty string.
If you desire your behavior you should create your own custom tag
@register.filter
def addfloat(value, arg):
return float(value) + float(arg)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments