我正在开发一个 Django 应用程序,它有 LANGUAGE_CODE
设置为es
西类牙语。
我正在尝试格式化数字在模板中的呈现方式。现在它们呈现为: 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.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
最佳答案
我只需在我的 settings.py
文件中关闭这两个变量:
USE_I18N = False #Before True
USE_L10N = False #Before True
在模板中我可以使用:
{{ total }}
感谢 Kendoka 的评论为我指明了正确的方向。
关于python - Humanize 不适用于模板中的 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55468139/