为什么Django使用逗号作为小数点分隔符 [英] Why django uses a comma as decimal separator

查看:82
本文介绍了为什么Django使用逗号作为小数点分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python 2.6和django 1.27

I am using python 2.6 and django 1.27

我的模型

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

在我的模板中

{{plan.price}}

我的问题是在本地计算机上,我将点用作分隔符例如'2.54'

在生产机器上时,我会得到'2,54'-逗号用作分隔符。

我希望它在所有地方都使用点。

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.


https://docs.djangoproject.com/zh-CN/1.2/ref/settings/#decimal-separator

,它说有 DECIMAL_SEPARATOR选项,默认情况下是点。

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 [2]: from django.conf import settings
In [3]: print settings.DECIMAL_SEPARATOR
.

解决方案:

@Marcin指出

在生产计算机上将USE_L10N设置为False。

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

推荐答案

首先全部,我假设您在 settings.py 中启用了L10N和I18N,因为这是默认设置。您看到的差异可能是因为您是从具有两种不同语言环境的两台不同计算机访问该网站。 Django尝试为浏览器报告的语言环境设置格式。

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.

但是,您可以禁用此行为。请参阅 https://docs.djangoproject.com/en/dev/ref/settings/ 。设置 USE_L10N = False ,然后设置链接页面上指定的各种分隔符选项。

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.

这篇关于为什么Django使用逗号作为小数点分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆