Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

European date input in Django Admin

Django has a DATE_FORMAT and a DATE_TIME_FORMAT options that allow us to choose which format to use when viewing dates, but doesn't apparently let me change the input format for the date when editing or adding in the Django Admin.

The default for the admin is: YYYY-MM-DD

But would be awesome to use: DD-MM-YYYY

Is this integrated in any case in i18n? Can this be changed without a custom model?

like image 208
Andor Avatar asked May 25 '09 16:05

Andor


2 Answers

There is an official way to do this now since the closing of Django ticket 6483 & release of Django 1.2.

If you have USE_L10N set to False, what you should do is specify the DATE_INPUT_FORMATS and DATETIME_INPUT_FORMATS in your settings.py. Here are the settings I use for this, based on converting the defaults:

#dd/mm/yyyy and dd/mm/yy date & datetime input field settings
DATE_INPUT_FORMATS = ('%d-%m-%Y', '%d/%m/%Y', '%d/%m/%y', '%d %b %Y',
                      '%d %b, %Y', '%d %b %Y', '%d %b, %Y', '%d %B, %Y',
                      '%d %B %Y')
DATETIME_INPUT_FORMATS = ('%d/%m/%Y %H:%M:%S', '%d/%m/%Y %H:%M', '%d/%m/%Y',
                          '%d/%m/%y %H:%M:%S', '%d/%m/%y %H:%M', '%d/%m/%y',
                          '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', '%Y-%m-%d')

If you have USE_L10N set to True, then you will need to use the FORMAT_MODULE_PATH instead.

For example, my LANGUAGE_CODE is set to en-au, my site is called golf, and my FORMAT_MODULE_PATH is set to golf.formats, so my directory structure looks like this:

golf/
    settings.py
    ...
    formats/
        __init__.py
        en/
            __init__.py
            formats.py

and the DATE_INPUT_FORMATS and DATETIME_INPUT_FORMATS settings are in formats.py instead of settings.py.

like image 186
Caspar Avatar answered Nov 15 '22 06:11

Caspar


I've modified settings so that it disables l10n and set date format explicite:

USE_L10N = False
DATE_FORMAT = 'd-m-Y'
DATETIME_FORMAT = 'd-m-Y H:i'

You can set DATE_INPUT_FORMATS as well.

like image 5
alekwisnia Avatar answered Nov 15 '22 07:11

alekwisnia