20

I have a form with a DateField. The existing date value is formatted to render like this:

2009-10-03

How can I format that so that it look like this:

03.10.2009

I found a widget which renders it like this, but validation doesn't allow the values I enter then.

1
  • So many reputation has been wasted on this one. :( Sep 23, 2014 at 1:33

4 Answers 4

37

To display initial field value properly formatted, use DateInput widget. To customize validation, use input_formats keyword argument of DateField.

Why do you need both format and input_formats?

format - The format in which this field’s initial value will be displayed. See here.

input_formats - A list of formats used to attempt to convert a string to a valid datetime.date object. See here.

6
  • 14
    Thanks this worked... It looks like this now: my_date = forms.DateField(initial=date.today(), widget=forms.DateInput(format = '%d.%m.%Y'), input_formats=('%d.%m.%Y',))
    – falstaff
    Oct 3, 2009 at 13:37
  • 1
    Note: I believe this works only with Django 1.1 and above. I'm using 1.0.
    – dfrankow
    Nov 1, 2009 at 16:14
  • Why is it that both format and input_formats are necessary?
    – radtek
    Sep 12, 2014 at 15:08
  • 1
    A note for whoever sees this all these years after the original question. Careful with this: initial=date.today(). By having the parenthesis you are effectively calling the function immediately, which means the default date for that field will be the date when the code is executed (whenever you start the WSGI server). I suspect this is not what you want and if so you should remove the parenthesis so that Django only calls date.today when it instantiates a new model.
    – borfast
    Dec 17, 2016 at 15:19
  • @radtek, format property is for the rendering of the date inside the input, while input_formats is used to define the valid format to be accepted Dec 27, 2016 at 12:46
13

Method 1 : manual format rendering but fails datefield format validation

So best formating solution for you would be "."

{{field.value.day|stringformat:"02d"}}.{{field.value.month|stringformat:"02d"}}.{{field.value.year}}

to get 24.05.2010

I added stringformat:"02d" to not get 25.5.2010 as output value.

I post the full code of a field to get an idea :

<input class="form-control" data-inputmask="'mask':'99.99.9999'" data-toggle="masked"  name="{{field.html_name}}" placeholder="{{ field.label }}" {% if field.field.required %}required=""{% endif%} type="text" value="{{field.value.day|stringformat:"02d"}}.{{field.value.month|stringformat:"02d"}}.{{field.value.year}}" " >

I mainly used this formatting for the date value on an existing instance of an update (model) form (UpdateView class).

This solution may fail when submitting the update form if it will not satisfy the format

Method 2 : (Better) automatize field value formatting + correct validation using settings

It's done by adding custom local settings to settings.py file : The most important part is the DATE_INPUT_FORMATS tuple

..
..
USE_L10N = True
..

DATE_INPUT_FORMATS = (
    '%d.%m.%Y', '%d.%m.%Y', '%d.%m.%y',  # '25.10.2006', '25.10.2006', '25.10.06'
    '%d-%m-%Y', '%d/%m/%Y', '%d/%m/%y',  # '25-10-2006', '25/10/2006', '25/10/06'
    '%d %b %Y',  # '25 Oct 2006', 
    '%d %B %Y',  # '25 October 2006', 
)

DATE_FORMAT = 'j F Y'
TIME_FORMAT = 'H:i'
DATETIME_FORMAT = 'j F Y H:i'
YEAR_MONTH_FORMAT = 'F Y'
MONTH_DAY_FORMAT = 'j F'
SHORT_DATE_FORMAT = 'j N Y'
SHORT_DATETIME_FORMAT = 'j N Y H:i'
FIRST_DAY_OF_WEEK = 1

The most important:

  1. The validation of datefields is done on insert and update with all formats present in DATE_INPUT_FORMATS tuple
  2. On updateView, the date field will be rendered using the first format of DATE_INPUT_FORMATS tuple (i mean using DATE_INPUT_FORMATS[0] == '%d.%m.%Y')

So the choice of the first element is important in this tuple as it defines the formating of existing value of datefield on update forms

You can apply javascript validation on UpdateViews using this first format too.

Tested on django 1.6 and 1.7 , don't know for previous versions

1
  • As a note, the DATE_INPUT_FORMATS is a list in the global_settings files, not a tuple.
    – Bobort
    Feb 2, 2018 at 3:47
6

Subclass custom field like this:

class CustomDateField(forms.DateField):
  def __init__(self, *args, **kwargs):
    kwargs.setdefault('input_formats', ("%d.%m.%Y",))
    super(CustomDateField, self).__init__(*args, **kwargs)
4

You can show date in template by means of

{{ your_object.date_field.day }}.{{ your_object.date_field.month }}.{{ your_object.date_field.year }}
1
  • 1
    Sorry, I wasn't attentive Oct 8, 2013 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.