How to enable decimal values in Django template?

Yash Marmat

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

iklinac

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.

edited at
0

Comments

0 comments
Login to comment

Related

Django – remove trailing zeroes for a Decimal in a template

enable a disabled html button based in django template

How to obtain dicts key and values in Django template?

template class specialization enable_if and default values

Django 2.1, How to show Foreign key values in template?

How to do an if statement in a Django template to find if a decimal value is 0

How to pass values of drop down from html template to views in Django?

how to operate with values from dictionary in a condtional statement in a django template

Django template: How to use values/values_list

How do I iterate dictionary with lists as values in Django template?

How to remove decimal on smarty template

How to get values from the Ajax() into Django template?

How to autofill a form template with values stored in database in Django?

Comparing values in django template

Django Template Filters on Session Values?

unique values in django template

Template for extracting POST values in Django

How to bind nullable decimal values

Django Template: How to display the value of Foreign Key in a values_list

how to pass many values through the same variable in Django template

Thousand separator with commas and decimal with dots on django template

How to align decimal point of price in django template?

Django - Comparing datetime values in template

How to access the values in nested dictionary sent to django template

How to decrement decimal values in javascript

how to render values from multiple methods in table format in template django

how to get multiple input values in django loop (template) using javascript?

How to add decimal values in Bash

How to acess Foreign key on django template languages from queryset values