django date format 'dd-mm-yyyy'
In Sevenearths answer there's the comment
Why
USE_L10N = True
can't just do this I don't know!
The documentation says that USE_L10N = True
is the default setting, but to get UK dates to work the LANGUAGE_CODE
must also be changed from its default setting of en-us
to en-GB
.
Once changed, date fields automatically accept the formats "dd/mm/yyyy" and "yyyy-mm-dd" (and possibly others, I've not tested them all) without needing to set input_formats = settings.DATE_INPUT_FORMATS
in every form date field.
DATE_INPUT_FORMATS
in settings.py has no effect with USE_I18N = True
. This is because django will load specific format for active locale. Django ships with format definitions for many different locales.
You can override django default format definitions:
mysite/
formats/
__init__.py
en/
__init__.py
formats.py
As described in django documentation: Creating custom formats file
The problem turned out to be that I needed both the ISO and UK date formats in the settings.py
file like so:
DATE_INPUT_FORMATS = ('%d-%m-%Y','%Y-%m-%d')
and then the forms.py adjusted to the following:
class ClientDetailsForm(ModelForm):
date_of_birth = DateField(input_formats=settings.DATE_INPUT_FORMATS)
class Meta:
model = ClientDetails
Why USE_L10N = True
can't just do this I don't know!
[Thanks to this and this]