Solutons Lounge

How to use FORMAT_MODULE_PATH – Forms & APIs


I am trying to make a change to the format of date time in a template. In DJ 5, it seems that USE_L10N is always true, and I am supposed to use the “FORMAT_MODULE_PATH” to specify where a formats.py file is that then defines some things like DATETIME_FORMAT.

This all makes sense conceptually, but it also doesn’t seem to work. I have this in my settings file:

LANGUAGE_CODE='en-us'
TIME_ZONE = 'America/New_York'
FORMAT_MODULE_PATH = [
    "student_tracker.formats",
    "student_track.formats",
]
USE_L10N=True
USE_I18N = True
USE_TZ = True
USE_THOUSAND_SEPARATOR = True

and then in my student_track app I have:

/formats/en_us/formats.py which contains this:

DATE_FORMAT = "M. d, Y"
TIME_FORMAT = "g:i a"

DATETIME_FORMAT = "M. d Y H:i a"
# DATETIME_FORMAT = f"{DATE_FORMAT} {TIME_FORMAT}"


DATE_INPUT_FORMATS = [
    '%m/%d/%Y', '%m/%d/%y', '%Y-%m-%d',  # '2006-10-25', '10/25/2006', '10/25/06'
    '%b %d %Y', '%b %d, %Y',  # 'Oct 25 2006', 'Oct 25, 2006'
    '%d %b %Y', '%d %b, %Y',  # '25 Oct 2006', '25 Oct, 2006'
    '%B %d %Y', '%B %d, %Y',  # 'October 25 2006', 'October 25, 2006'
    '%d %B %Y', '%d %B, %Y',  # '25 October 2006', '25 October, 2006'
]

But when I render my template which has this:

<td class="text-start">{{ record.actual_date|date:"DATETIME_FORMAT" }} </td>

I still get the default (and not what I want) DateTime format.

I don’t understand why it is so difficult to simply get a format for all date/times that I want.

No idea how to figure out what is wrong or debug what is being done and could use help.



Source link

Exit mobile version