【问题标题】:Humanize not working for floats in template人性化不适用于模板中的浮动
【发布时间】:2019-08-23 09:18:37
【问题描述】:

我正在开发一个将 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)&lt;p&gt;El total de su pedido es: S/ {{ total|floatformat:'2' }}&lt;/p&gt;

不起作用,在需要 S/ 18.00 时呈现:S/ 18,00

2)&lt;p&gt;El total de su pedido es: S/ {{ total|stringformat:"f" }}&lt;/p&gt;

有效但使用超过 2 位小数:S/ 18.00000000 当需要 S/ 18.00 时。

3)&lt;p&gt;El total de su pedido es: S/ {{ total|stringformat:"2f" }}&lt;/p&gt;

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

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

【问题讨论】:

标签: python django


【解决方案1】:

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

USE_I18N = False #Before True

USE_L10N = False #Before True

在模板中我可以使用:

{{ total }}

感谢 Kendoka 的评论为我指明了正确的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2014-09-23
    相关资源
    最近更新 更多