This site talks about the many challenges on computer programming.

There’s no doubt about Django and its agile development nature, but there are some circustances where we need take more care on user input, one of these circustances is date input, after few hours googling about it I finally found a way to fix date input by just using DateTimeInput widget on DateField of Django ModelForm.

In the code above we are creating a Django ModelForm that contains only one DateField called birthDate (line 6), this field contains a custom date input format that allow the user to input date in ‘dd/mm/yyyy’ format.

But what about output dates in this format when restoring a date from database? Simple, just attach a DateTimeInput widget as we have above (line 7) and pass the same date format used on DateField.

And now the best part, we will add a JQuery widget to make date input a little more interesting as we can see below:

In the lines 5 to 12 we are importing a css and javascript files that contains everything need to get datepicker working in this page, note a javascript call to initialize the datepicker widget at line 10, we are initializing this widget with a custom date format, the same as defined in Django ModelForm, when the user put focus on birth date field of the page above, a calendar is displayed, when the user select a date in this calendar the selected date goes to form input in ‘dd/mm/yyyy’ format. If the user clicks on “Save” button, the page form is submitted and the date entered by user is correctly validated and stored in database.

Currently you have JavaScript disabled. In order to post comments, please make sure JavaScript and Cookies are enabled, and reload the page.Click here for instructions on how to enable JavaScript in your browser.