开发者

Django separate thousands in template

My model is returning a Decimal(1234567.50), I can't seem to display the Decimal with a thousands separator. Does Django have a way to do this? Do I need to create my own temp开发者_如何学运维late filter?

Thanks.


You can use the intcomma filter.


Add humanize to the installed apps in settings.py and load it in the template.

{% load humanize %}
{{ my_num|intcomma }}


One easy way is to import locale and do the formatting in your view function.

Even easier to read this: http://docs.djangoproject.com/en/dev/topics/i18n/#overview

For format localization, it’s just necessary to set USE_L10N = True in your settings file. If USE_L10N is set to True, Django will display numbers and dates in the format of the current locale. That includes field representation on templates, and allowed input formats on the admin.


You could add the following to settings.py:

USE_THOUSAND_SEPARATOR = True

That should work and you don't have to act with loading stuff. https://docs.djangoproject.com/en/4.1/ref/settings/#use-thousand-separator

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜