人性化不适用于模板中的浮动

时间:2019-04-02 06:21:46

标签: python django

我正在使用将LANGUAGE_CODE设置为es(西班牙语)的Django APP。

我正在尝试格式化数字在模板中的呈现方式。现在,它们的呈现方式如下: S/ 18,00,当需要S/ 18.00

我已经搜索并找到了另一个相关问题:

Format numbers in django templates

但是在应用Humanize之后,我 不是 获得了预期的结果:

template.html

{% load humanize %}

<p>El total de su pedido es: S/ {{ total|intcomma }}</p> #renders S/ 18,00

settings.py

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'shop',
    'django.contrib.humanize',
]

我也尝试了其他解决方案:

1)<p>El total de su pedido es: S/ {{ total|floatformat:'2' }}</p>

无效,在需要S/ 18,00时呈现S/ 18.00

2)<p>El total de su pedido es: S/ {{ total|stringformat:"f" }}</p>

可以,但是使用两个以上的小数:S/ 18.00000000,当需要S/ 18.00时。

3)<p>El total de su pedido es: S/ {{ total|stringformat:"2f" }}</p>

这不起作用,并且在需要S/ 18.00000000时也会返回:S/ 18.00

models.py:

class Order(models.Model):

    token = models.CharField(max_length=100, blank=True, null=True)
    first_name = models.CharField(max_length=50, blank=True, null=True)
    last_name = models.CharField(max_length=50, blank=True, null=True)
    total = models.DecimalField(max_digits=10, decimal_places=2)

views.py

def thanks_deposit_payment(request):
    order_number = Order.objects.latest('id').id

    total = Order.objects.latest('id').total

    response = render(request, 'thanks_deposit_payment.html', dict(order_number=order_number, total=total))
    return response

其他可能有用的语言设置:

LANGUAGE_CODE = 'es'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

enter image description here

1 个答案:

答案 0 :(得分:0)

我只需要关闭settings.py文件中的这两个变量:

USE_I18N = False #Before True

USE_L10N = False #Before True

在模板中,我只能使用:

{{ total }}

感谢Kendoka的评论,指出了我的正确方向。