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?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With