I have a very simple form to submit a date. By default, the date format is 'yyyy-MM-dd' and the form works. However as soon as I change format to anything (I tried False and 'LL'), I get an error message on submission: 'Enter a valid date'. So it looks like on submission the default format is used rather than the one I've set. How do I get the preferred format working?
Here is the form:
class DateForm(forms.Form):
date_field = forms.DateField(
required=True,
label='The Date',
widget=DatePicker(
options={
'minDate': '1900-01-01',
'maxDate': '2050-12-31',
'format': 'LL',
},
),
initial='2001-01-01',
)
And here is the view:
def date_input(request):
if request.method == 'POST':
form = DateForm(request.POST)
if form.is_valid():
the_date = form.cleaned_data['date_field']
print(the_date)
else:
form = DateForm()
return render(request, 'calculators/date-input.html', {
'form': form,
})
The solution appears to be to use
input_formatson the field that would contain the format specified on the widget. In my case, I used'MMM D, YYYY'on the widget andinput_formats=['%b %d, %Y'].