Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable formatting for FloatField in template for Django

i just can't seem to find a definitive answer to this issue, and django's irc needs auth to services... So my question is : how can you force some kind of formatting for FloatFields in template when you're using Django ?

The problem is simple i need simple dot separated numbers like this : 42547.34 And i end up with comma separated values...

here is a example of template where the problem occurs :

{% for point in zone.points.all  %}   {% if forloop.last %}     new google.maps.LatLng({{point.latitude|floatformat}},{{point.longitude|floatformat}})   {% else %}      new google.maps.LatLng({{point.latitude|floatformat}},{{point.longitude|floatformat}}),   {% endif %} {% endfor %}]; 

Thank you for your time.

P.S. i don't have this problem when using the admin generated forms where the floats appear correctly (My locale is en_US)

like image 853
Olivier Girardot Avatar asked Aug 23 '10 20:08

Olivier Girardot


People also ask

What {{ name }} means in a Django template?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

What is Autoescape in Django template?

autoescape. Controls the current auto-escaping behavior. This tag takes either on or off as an argument and that determines whether auto-escaping is in effect inside the block. The block is closed with an endautoescape ending tag.

What is the purpose of {# #} in template language of Django?

To comment-out part of a line in a template, use the comment syntax: {# #} . This syntax can only be used for single-line comments (no newlines are permitted between the {# and #} delimiters). If you need to comment out a multiline portion of the template, see the comment tag.


2 Answers

{{ float_var|stringformat:"f" }} 
like image 58
Stef Avatar answered Sep 21 '22 16:09

Stef


You can now force the value to be printed without localization.

{% load l10n %}  {{ value|unlocalize }} 

Taken from https://docs.djangoproject.com/en/2.2/topics/i18n/formatting/#std:templatefilter-unlocalize

like image 40
JustinHui Avatar answered Sep 17 '22 16:09

JustinHui