简体   繁体   中英

Thousand separator not applied in Django admin

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.

I am using Django 1.11.9, with Python 3.6.

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

Is there a way for the django-admin to apply thousand separators to my read-only fields?

-- EDIT --

This similar question (from sep 2015) does not have a simple answer that applies to all fields automaticly.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM