How to find out decimal-point format currently used by authorized django user?

522 views Asked by At

in Django 1.10 & py 2.7 how can I find out the current authorized user's locale decimal-point, i.e. does the current user use ',' or '.' as decimal separator (should it be 11,23 or 11.23)?

I am using _thread_locals for some other user settings, all things works fine and ',' vs. '.' is used correctly everywhere - but I now need to find out inside a view which is used and have not been able to figure this out...

BTW - each authorized user has own locale so one user can have ',' and another '.' depending on the user locale.

/ Jens

3

There are 3 answers

1
Cameron McFee On

Django has native localization built in for both templates and forms. Where are you determining this?

In a template:

{% load l10n %}{% localize on %}{{ value }}{% endlocalize %}

Or in a form:

To enable a form field to localize input and output data simply use its localize argument:

class CashRegisterForm(forms.Form):
  product = forms.CharField()
  revenue = forms.DecimalField(max_digits=4, decimal_places=2, localize=True)

You have to enable the localization module:

The formatting system is disabled by default. To enable it, it’s necessary to set USE_L10N = True in your settings file.

0
Jens Lundstrom On

Here is what I ended up doing - completely naked and without any test/check/exception-handling:

from importlib import import_module 
from django.utils.translation import get_language

fm = import_module('.formats', 'django.conf.locale.%s' % get_language())

try:
    return getattr(fm, 'DECIMAL_SEPARATOR')
except AttributeError:
    return '.'
0
durdenk On
from django.util.format import get_format #or get_format_lazy
from django.utils.translation import get_language

decimal_seperator = get_format('DECIMAL_SEPARATOR',get_language())

Feel free to use 'DATE_FORMAT', 'THOUSAND_SEPARATOR','NUMBER_GROUPING' etc instead of 'DECIMAL_SEPARATOR'

I got import error at some point using @jens-lundstrom answer.