Django本地化

时间:2011-10-27 15:42:17

标签: django

我在settings.py文件中设置了以下内容:

USE_L10N = True
NUMBER_GROUPING = 3
THOUSAND_SEPARATOR = '#'
USE_THOUSANDS_SEPARATOR =True

然而,我的号码仍在打印12000.00。有人能指出我正确的方向吗?

(我在Django 1.3上)

2 个答案:

答案 0 :(得分:1)

Django(humanize)附带了一个帮助模板库,它有一个名为intcomma的过滤器,听起来像你想做的那样。

模板中的用法:

{% load humanize %}
${{ value|intcomma }}

答案 1 :(得分:0)

我找不到任何合理的原因,为什么本地化不会起作用所以在将值传递给模板之前最终使用following

def commify(n):
    if n is None: return None
    n = str(n)
    if '.' in n:
        dollars, cents = n.split('.')
    else:
        dollars, cents = n, None

    r = []
    for i, c in enumerate(str(dollars)[::-1]):
        if i and (not (i % 3)):
            r.insert(0, ',')
        r.insert(0, c)
    out = ''.join(r)
    if cents:
        out += '.' + cents
    return out
相关问题