Reputation: 900
I have a page where the user inputs some dates in the dd/mm/yyyy
format.
In my settings file I have this:
DATE_INPUT_FORMATS = ('%d/%m/%Y','%Y/%m/%d', '%Y-%m-%d',)
USE_TZ = True
USE_L10N = True
TIME_ZONE = 'Europe/Rome
I use the django bootstrap4 datetime picker plus
The field renders as such: (i apologize about the screenshot, i couldnt copy the html without it being escaped and it looked extra-messy)
The problem is that when i input dates in dd/mm/yyyy format, it uses the american format (mm/dd/yyyy) to validate them. So if i enter 27/12/2021 it will try to save [day 12, month 27, year 2021] and fail the validation. Preventing the formset from being saved.
What i don't understand is where in hell does django get the idea that it should use the american format to validate dates when I have set three separate DATE_INPUT_FORMATS
and none of them are in the american format?
Here an image showing that on the page the date is collected in the right format
And the response : [{'start_date': ['Enter a valid date.'], 'end_date': ['Enter a valid date.']}]
this is where i am now:
class EducationForm(forms.ModelForm):
# def clean_start_date(self):
# print(self.cleaned_data['start_date'])
# return datetime.strptime((self.cleaned_data['start_date']), '%Y-%m-%d')
# def clean_end_date(self):
# return datetime.strptime((self.cleaned_data['end_date']), '%Y-%m-%d')
class Meta:
model = Education
fields = ['start_date', 'end_date', 'institution', 'title']
# localized_fields = ('start_date', 'end_date',)
widgets = {
'start_date': DatePickerInput(attrs={
'data-provide' : 'datepicker',
'class' : 'form-control my-date-picker',
# 'data-date-format' : "DD/MM/YYYY"
},
options={
"format": "DD/MM/YYYY",
}),
'end_date': DatePickerInput(attrs={
'placeholder': 'Ongoing',
'data-provide' : 'datepicker',
'class' : 'form-control my-date-picker',
# 'data-date-format' : "DD/MM/YYYY"
},
options={
"format": "DD/MM/YYYY",
}),
In the template there's nothing particular going on. I just render the datetime fields as {{ field }}.
Upvotes: 0
Views: 1094
Reputation: 900
In the end the solution was adding USE_L10N = False
to my settings file.
It appears that unless specified, django will disregard the contents of DATE_INPUT_FORMATS
Upvotes: 1