千位分隔符未在Django管理中应用 [英] Thousand separator not applied in Django admin
问题描述
我在Django Admin中注册了我的模型,但是显示的Integer字段(它们是只读的)没有千位分隔符。
I registered my model in the Django Admin, but the Integer fields (they are read-only) show up without thousand separators; this happens in the admins list view and also in the admins model form.
我正在使用Django 1.11.9和Python 3.6。
I am using Django 1.11.9, with Python 3.6.
在我的 settings.py中,我有以下内容:
In my 'settings.py' I have the following:
USE_I18N = False
USE_L10N = False
DECIMAL_SEPARATOR = ','
THOUSAND_SEPARATOR = '.'
USE_THOUSAND_SEPARATOR = True
NUMBER_GROUPING = 3
django-admin是否可以将千位分隔符应用于我的只读字段?
Is there a way for the django-admin to apply thousand separators to my read-only fields?
- -编辑-
此相似的问题(自2015年9月起)没有一个自动适用于所有字段的简单答案。
This similar question (from sep 2015) does not have a simple answer that applies to all fields automaticly.
推荐答案
我在django-admin中无法正常工作。由于我不在面向用户的网站上使用admin,因此现在让数字以逗号而不是点的形式设置。
I did NOT get it working properly in the django-admin. Since I do not use the admin for user-facing sites, I'll let the numbers be formatted with commas instead of dots for now.
但是,在其他模板中根据 django文档。
However, in other templates I resorted to using a custom template tag, based on the django documentation.
from django import template
from django.utils.encoding import force_text
register = template.Library()
@register.filter
def intdot(val_orig):
"""
Similar to 'django.contrib.humanize.intcomma', but with dots.
"""
val_text = force_text(val_orig)
try:
val_new = int(val_text)
except ValueError:
return ''
val_new = '{:,d}'.format(val_new)
val_new = val_new.replace(',', '.')
return val_new
这篇关于千位分隔符未在Django管理中应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!