Search code examples
djangodjango-settings

USE_L10N deprecated, but it disables DATETIME_INPUT_FORMATS when removed


there's an expanded version of the accepted correct answer in my update to this question below

Have I found a bug in Django?

The config setting USE_L10N is deprecated since 4.0 and scheduled for removal in 5.0. However, the documentation of forms.DateField for 5.0 still states

If no input_formats argument is provided, the default input formats are taken from the active locale format DATE_INPUT_FORMATS key, or from DATE_INPUT_FORMATS if localization is disabled.

Which begs the question, how does one disable localisation and enable the DATE_INPUT_FORMATS setting once USE_L10N = False is not longer possible?

I don't want to upgrade to 5.0 yet, so I cannot easily test this. I have established that on 4.2, if I comment out the deprecated USE_L10N = False in my settings.py, then DATE_INPUT_FORMATS ceases to be honoured.

Update, after lucutzu33's answer (which works with Django 4.2.)

I found it extremely non-intuitive to put something that has global effects into one arbitrary app's tree, and one doesn't have to...

settings.py

# USE_L10N = False      # deprecated -> removed
LANGUAGE_CODE = 'en-GB'
FORMAT_MODULE_PATH = ['formats',] # <project>/formats, because this has global effect

So, create <project>/formats, project/formats/en_GB, <project>/formats/__init__.py, project/formats/en_GB/__init__.py

Then edit project/formats/en_GB/formats.py to contain the desired overrides to Django defaults. See Django doc

SHORT_DATE_FORMAT = 'd/m/y'
DATE_INPUT_FORMATS = [
    '%d/%m/%y', '%d/%m/%Y',             # '25/10/06', '25/10/2006',
    '%d.%m.%Y', '%d.%m.%y',             # Germany, other EU use dots
    '%Y-%m-%d',                         # ISO yyyy-mm-dd
    # etc
]
DATETIME_INPUT_FORMATS = [
    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
    # and variants,
    '%Y-%m-%d',              # '2006-10-25'
    '%d/%m/%Y %H:%M:%S',     # '25/10/2006 14:30:59'
    # and variants...
]

(This content was previously in settings.py and activated by USE_L10N=True, so cut it from there and paste it here)


Solution

  • This might be a late response, but I have found a solution:

    Copy the formats.py file you want to in your app's directory. The path should look like <your_project>/<your_app>/formats/ro/formats.py

    then in settings.py you can set:

    LANGUAGE_CODE = 'ro-RO'
    FORMAT_MODULE_PATH = ['yourapp.formats',]