Why django uses a comma as decimal separator

五迷三道 提交于 2019-12-03 16:36:43

问题


I am using python 2.6 and django 1.27

my model

class Plan(models.Model):
     price = models.DecimalField(max_digits=5, decimal_places=2,default=0)
     .....

in my template i have

{{plan.price}}

My problem is that on my local machine i get dot used as separator for example '2.54'
While on my production machine i get '2,54' - comma is used as separator.
I would like it to use dot everywhere.

in the django docs https://docs.djangoproject.com/en/1.2/ref/settings/#decimal-separator
it say there is the "DECIMAL_SEPARATOR" option the default it dot.

btw in both machines

In [2]: from django.conf import settings
In [3]: print settings.DECIMAL_SEPARATOR
.

SOLUTION:

as @Marcin pointed out
setting USE_L10N to False on production machine.


回答1:


First of all, I assume you have L10N and I18N turned on in your settings.py, because that's the default. The difference you see is likely because you are accessing the website from two different computers with two different locales. Django tries to format things for the locale reported by the browser.

However, you can disable this behaviour. See https://docs.djangoproject.com/en/dev/ref/settings/. Set USE_L10N=False, and set the various separator options specified on the linked page.




回答2:


You can use this alternative way directly on your template:

{% load l10n %}

{% localize off %}
{{ my_floatvar }}
{% endlocalize %}

or this one:

{% load l10n %}

{{ my_floatvar|unlocalize }}

More info in https://docs.djangoproject.com/en/dev/topics/i18n/formatting/#controlling-localization-in-templates



来源:https://stackoverflow.com/questions/19566673/why-django-uses-a-comma-as-decimal-separator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!