python - Humanize 不适用于模板中的 float

标签 python django

我正在开发一个 Django 应用程序,它有 LANGUAGE_CODE设置为es西类牙语。

我正在尝试格式化数字在模板中的呈现方式。现在它们呈现为: S/ 18,00S/ 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,00S/ 18.00是需要的。

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

可以工作,但使用超过 2 位小数:S/ 18.00000000S/ 18.00是需要的。

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

这不起作用,还会返回:S/ 18.00000000S/ 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

最佳答案

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

USE_I18N = False #Before True

USE_L10N = False #Before True

在模板中我可以使用:

{{ total }}

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

关于python - Humanize 不适用于模板中的 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55468139/

相关文章:

python - 在MacOSX 10.6.8 Snow Leopard中,Python 3.4 IDLE无法正常工作,启动编辑器时会崩溃

python - 是否有任何内置函数会阻塞 I/O 而不允许其他线程运行?

python - 访问后如何将django表中的按钮更改为文本

python - 有哪些有用的非内置 Django 标签?

html - 应用的 CSS 导致 anchor 不可点击

python - 当我不使用 BeautifulSoup 时如何摆脱 BeautifulSoup html 解析器错误

python - 当我增加框架厚度时,信号器消失

python - 有没有办法在 urls.py 中使用 django 捕获 URL 参数?

python - Django 无法在 PostgreSQL 上创建 super 用户。 "server closed the connection unexpectedly"

python - 在Python中将矩阵项K_ij除以K_ii*K_jj的快速方法