【发布时间】: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)<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>
有效但使用超过 2 位小数:S/ 18.00000000 当需要 S/ 18.00 时。
3)<p>El total de su pedido es: S/ {{ total|stringformat:"2f" }}</p>
这不起作用,当需要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
【问题讨论】:
-
你试过
{{ total|floatformat:2|intcomma }}吗? -
另外,试一试:
{% load l10n %} {% localize on %} {{ total }} {% endlocalize %} -
我已经测试过了,得到了同样的结果:
S/ 18,00 -
或:
{% load l10n %} {{ value|localize }}