Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Use of DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT in settings.py?

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?

like image 416
Heyl1 Avatar asked Sep 01 '10 12:09

Heyl1


3 Answers

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.

like image 72
dr jimbob Avatar answered Sep 23 '22 18:09

dr jimbob


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.VariableNodes are rendered.

I'm not sure when exactly VariableNodes 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.

like image 20
Seth Avatar answered Sep 22 '22 18:09

Seth


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:

  1. Set FORMAT_MODULE_PATH = 'yourproject.formats' in settings.py
  2. Create directory structure yourproject/formats/en (replacing en 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
  3. 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.

like image 38
mrts Avatar answered Sep 25 '22 18:09

mrts