I would like to globally (through my entire site, admin and front-end) adjust the way dates and time are displayed to my likings, but I cannot figure out what is going on with the DATE_FORMAT, DATETIME_FORMAT and TIME_FORMAT variables in settings.py.
In this question it says that the settings are ignored. The question is over a year old though. In the Django documentation it says they can be used when you have USE_L10N = True
and apparently something changed in Django 1.2. According to this however there might be a bug.
I am currently using Django 1.2 and when I have USE_L10N = True
it just ignores the date(time) format in settings.py. When I have USE_L10N = False
it also seems to ignore them.
Is there a way to globally customize the date and time display? Or should I create my own custom formats file as Karen suggests in the Django Users Google Group post?
You can override
DATE_FORMAT
,DATETIME_FORMAT
,TIME_FORMAT
and other date/time formats whenUSE_L10N = True
by creating custom format files as described in Django documentation.In summary:
FORMAT_MODULE_PATH = 'yourproject.formats'
insettings.py
yourproject/formats/en
(replacingen
with the corresponding ISO 639-1 locale code if you are using other locale than English) and add__init__.py
files to all directories to make it a valid Python moduleformats.py
to the leaf directory, containing the format definitions you want to override, e.g.DATE_FORMAT = 'j. F Y'
.Example from an actual project here.
Had same problem, solution is simple and documented. Whenever you render a date, you need to specify you want the template to render it as a date/time/short_date/datetime (e.g.,
{{ some_date_var | date }}
and then it will render it as specified withDATE_FORMAT
in your settings.pyExample:
This makes sense; e.g., the template needs to know whether it should use the
DATE_FORMAT
or theSHORT_DATE_FORMAT
or whatever.A late response, but hopefully this will help anyone else searching for this. By setting USE_L10N = True in your settings, Django looks for locale specific formats, giving them precedence over non-locale related settings.
The solution: (to display 30/12/2017 on a DateField)
and for inputs (to accept 30/12/2017 or 30-12-2017)
Reference: https://mounirmesselmeni.github.io/2014/11/06/date-format-in-django-admin/
*tested on Django==1.10.7
Searching through the source shows that DATETIME_FORMAT, etc., are only used when
django.utils.formats.localize()
is called, and that only seems to be called whendjango.template.VariableNode
s are rendered.I'm not sure when exactly
VariableNode
s are used in template rendering, but I would guess that if you havesettings.USE_L10N
turned on and you have aVariableNode
, it will be localized.localize
looks like this:To answer your question, I'd probably write a quick context processor that called
localize()
on everything in the context.