Epicserve

Change the DateField format in the Django Admin

July 15, 2011 | 4:17 p.m. CDT

Recently I was working on a Django project where I decided I wanted a different format other than the standard YYYY-MM-DD. I searched the online documentation and Google and it took a little longer than expected to figure out how to make the format change, so I thought I’d make a blog post to help the next guy or girl that needs to figure it out.

Why the default format for English is YYYY-MM-DD, I’ll never know. Everyone I know in the US uses MM-DD-YYYY.

The default Django 1.3 settings file comes with USE_L10N set to True which means that if you try to override a setting like DATE_INPUT_FORMATS in your settings file it doesn’t work. So in order to change the default date and time formats you need to use the setting, FORMAT_MODULE_PATH in your settings file to give Django the path to where you want to put your custom locale formats.

So if you add the following to your settings file like I did:


FORMAT_MODULE_PATH = 'config.locale'

...then you would create the following directories and files in your project, if your LANGUAGE_CODE is set to en-us.


mysite/
    config/
        __init__.py
        locale
            __init__.py
            en/
                __init__.py
                formats.py

Next you would copy the default settings for the formats.py file and add them to the new formats.py file that you just created.

In order to change the date and time formats in the Django admin for the DateField and the DateTimeField you need to change the DATE_INPUT_FORMATS and make the first index in the DATETIME_INPUT_FORMATS tuple so it is the format that you want. I’ve updated my django-base-site repository on github.com if you want to look at how I set my project up.

I hope this helps!

Related tags: Django

Post Your Comment

(Optional & Not Displayed)

(Optional)


(allowed tags - strong, em, cite, & code)

Post Guidelines

Please be considerate of others. Keep comments relevant. Content deemed inappropriate or offensive may be edited and/or deleted. Email addresses are never displayed.

Line breaks and paragraphs are automatically converted — no need to use p or br/. Quotes, apostrophes, and double-dashes are automatically converted to smart punctuation. Be careful when copying and pasting portions of entries or other comments.

Links can be created using the standard <a href="http://url">urlName</a>. The following inline HTML elements may also be used: strong, em, cite, & code. The title attribute is allowed within any element. All other code will get removed before posting.