[Fixed]-Django Enter a valid date. validation error

27👍

input_formats needs to be a list, see

example:

['%Y-%m-%d',      # '2006-10-25'
'%m/%d/%Y',       # '10/25/2006'
'%m/%d/%y']       # '10/25/06'

https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.DateField

14👍

I prefer to set **DATE_INPUT_FORMATS
in settings.py and then define the field like:

dob = forms.DateField(input_formats=settings.DATE_INPUT_FORMATS)

This more DRY than setting it on a per form field basis.

If it’s a text field input, I almost always put the accepted format(s) in the field’s help_text so that the user can know what format(s) is(are) accepted.

👤j_syk

4👍

You do not need to add a new field once you already have a DateField in your Model.

You just have to set the input_formats for this field:

self.fields[ 'dob' ].input_formats = [ '%Y-%m-%d' ]

References: DateField and Format Localization

👤Diogo

0👍

The input_formats is your friend.

If you accept javascript, then you could make the field use something like jQuery calendar (i.e. a read-only text field and clicking on it will call the jquery code and pop up the calendar widget). You can have this calendar widget as a starting point.

Leave a comment