Django has a DATE_FORMAT and a DATE_TIME_FORMAT options that allow us to choose which format to use when viewing dates, but doesn\'t apparently let me change the input forma
I've modified settings so that it disables l10n and set date format explicite:
USE_L10N = False
DATE_FORMAT = 'd-m-Y'
DATETIME_FORMAT = 'd-m-Y H:i'
You can set DATE_INPUT_FORMATS as well.
just beware not to write it as a string but as a tuple. eg:
DATE_INPUT_FORMATS = ('%d.%m.%Y',)
if you want to have only one valid way of writing date in a form.
You have to do this yourself for now, but it's quite easy to do with a custom form field class that sets the input_formats argument of DateField. This should do it:
class MyDateField(forms.DateField):
def __init__(self, *args, **kwargs):
kwargs.setdefault('input_formats', ("%d-%m-%Y",))
super(MyDateField, self).__init__(*args, **kwargs)
Note that input_formats is a list, so you can specify multiple possibilities and it will try them in order when parsing user input.
There is an official way to do this now since the closing of Django ticket 6483 & release of Django 1.2.
If you have USE_L10N set to False
, what you should do is specify the DATE_INPUT_FORMATS and DATETIME_INPUT_FORMATS in your settings.py
. Here are the settings I use for this, based on converting the defaults:
#dd/mm/yyyy and dd/mm/yy date & datetime input field settings
DATE_INPUT_FORMATS = ('%d-%m-%Y', '%d/%m/%Y', '%d/%m/%y', '%d %b %Y',
'%d %b, %Y', '%d %b %Y', '%d %b, %Y', '%d %B, %Y',
'%d %B %Y')
DATETIME_INPUT_FORMATS = ('%d/%m/%Y %H:%M:%S', '%d/%m/%Y %H:%M', '%d/%m/%Y',
'%d/%m/%y %H:%M:%S', '%d/%m/%y %H:%M', '%d/%m/%y',
'%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', '%Y-%m-%d')
If you have USE_L10N
set to True
, then you will need to use the FORMAT_MODULE_PATH instead.
For example, my LANGUAGE_CODE is set to en-au
, my site is called golf
, and my FORMAT_MODULE_PATH
is set to golf.formats
, so my directory structure looks like this:
golf/
settings.py
...
formats/
__init__.py
en/
__init__.py
formats.py
and the DATE_INPUT_FORMATS
and DATETIME_INPUT_FORMATS
settings are in formats.py
instead of settings.py
.
Looks like this is not yet supported, as I understand it, so yes, you'll have to do some custom work for now.
Based on this idea I made new db.fields class EuDateField:
mydbfields.py
from django import forms
from django.forms.fields import DEFAULT_DATE_INPUT_FORMATS
from django.db import models
class EuDateFormField(forms.DateField):
def __init__(self, *args, **kwargs):
kwargs.update({'input_formats': ("%d.%m.%Y",)+DEFAULT_DATE_INPUT_FORMATS})
super(EuDateFormField, self).__init__(*args, **kwargs)
class EuDateField(models.DateField):
def formfield(self, **kwargs):
kwargs.update({'form_class': EuDateFormField})
return super(EuDateField, self).formfield(**kwargs)
Note that it adds my format (e.g. 31.12.2007) to existing "standard" django formats at first place.
Usage:
from mydbfields import EuDateField
class Person(models.Model):
...
birthday = EuDateField("Birthday", null=True, blank=True, help_text="")
In my case this renders good in admin, but most probably will in ModelForm too (haven't tried it).
My django version is:
>>> import django
>>> django.get_version()
u'1.1 alpha 1 SVN-10105'