13π
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 with DATE_FORMAT
in your settings.py
Example:
>>> from django.conf import settings # imported to show my variables in settings.py
>>> settings.DATE_FORMAT # - showing my values; I modified this value
'm/d/Y'
>>> settings.TIME_FORMAT
'P'
>>> settings.DATETIME_FORMAT
'N j, Y, P'
>>> from django.template import Template, Context
>>> from datetime import datetime
>>> c = Context(dict(moon = datetime(1969, 7, 20, 20, 17, 39))) # Create context with datetime to render in a template
>>> print c['moon'] # This is the default format of a printing datetime object
1969-07-20 20:17:39
>>> print Template("default formatting : {{ moon }}\n"
"use DATE_FORMAT : {{ moon|date }}\n"
"use TIME_FORMAT : {{ moon|time }}\n"
"use DATETIME_FORMAT: {{ moon|date:'DATETIME_FORMAT' }}\n"
"use SHORT_DATETIME_FORMAT: {{ moon|date:'SHORT_DATETIME_FORMAT' }}"
).render(c)
default formatting : 1969-07-20 20:17:39
use DATE_FORMAT : 07/20/1969
use TIME_FORMAT : 8:17 p.m.
use DATETIME_FORMAT: July 20, 1969, 8:17 p.m.
use SHORT_DATETIME_FORMAT: 07/20/1969 8:17 p.m.
This makes sense; e.g., the template needs to know whether it should use the DATE_FORMAT
or the SHORT_DATE_FORMAT
or whatever.
11π
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 when django.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 have settings.USE_L10N
turned on and you have a VariableNode
, it will be localized.
localize
looks like this:
def localize(value):
"""
Checks if value is a localizable type (date, number...) and returns it
formatted as a string using current locale format
"""
if settings.USE_L10N:
if isinstance(value, (decimal.Decimal, float, int)):
return number_format(value)
elif isinstance(value, datetime.datetime):
return date_format(value, 'DATETIME_FORMAT')
elif isinstance(value, datetime.date):
return date_format(value)
elif isinstance(value, datetime.time):
return time_format(value, 'TIME_FORMAT')
return value
To answer your question, Iβd probably write a quick context processor that called localize()
on everything in the context.
- [Django]-Factory-boy create a list of SubFactory for a Factory
- [Django]-Django: For Loop to Iterate Form Fields
- [Django]-How to test "render to template" functions in django? (TDD)
8π
You can override DATE_FORMAT
, DATETIME_FORMAT
, TIME_FORMAT
and other date/time formats when USE_L10N = True
by creating custom format files as described in Django documentation.
In summary:
- Set
FORMAT_MODULE_PATH = 'yourproject.formats'
insettings.py
- Create directory structure
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 module - Add
formats.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.
- [Django]-How do I run tests against a Django data migration?
- [Django]-Add inline model to django admin site
- [Django]-Detect mobile, tablet or Desktop on Django
4π
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)
from django.conf.locale.en import formats as en_formats
en_formats.DATE_FORMAT = "%d/%m/%Y"
and for inputs (to accept 30/12/2017 or 30-12-2017)
en_formats.DATE_INPUT_FORMATS = ['%d/%m/%Y', '%d-%m-%Y']
Reference: https://mounirmesselmeni.github.io/2014/11/06/date-format-in-django-admin/
*tested on Django==1.10.7
- [Django]-Using the reserved word "class" as field name in Django and Django REST Framework
- [Django]-How can I serialize a queryset from an unrelated model as a nested serializer?
- [Django]-Filtering using viewsets in django rest framework
0π
You can partially format date and time but not entirely in a Django project according to my experiments with Django 4.2.1.
In Django Admin, set DATE_INPUT_FORMATS to format DateTimeField() and DateField() and set TIME_INPUT_FORMATS to format DateTimeField()
and TimeField() in settings.py
. *DATETIME_INPUT_FORMATS doesnβt work to any parts of a Django project and you can see my question explaining these 3 settings.
In Django Templates, set DATETIME_FORMAT, DATE_FORMAT and TIME_FORMAT to format DateTimeField()
, DateField()
and TimeField()
respectively in settings.py
and you can see my question explaining these 3 settings in addition to other 2 settings.
*You need to set USE_L10N False
in settings.py
to make these settings above work because USE_L10N
is prior to them.
- [Django]-Django β getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using {% url "music:fav" %}
- [Django]-How to test "render to template" functions in django? (TDD)
- [Django]-Negating a boolean in Django template