Relevant doc:
https://docs.djangoproject.com/en/3.2/ref/forms/fields/#datetimefield
Small reproducible snippet:
{{{
test_sets = [
# Missing seconds
{"input_format": "%Y-%m-%d %H:%M:%S", "dt": "2018-12-10
21:00"},
# Hour in 24 format instead of 12
{"input_format": "%Y-%m-%d %I:%M", "dt": "2018-12-10 21:00"}
]
for incorrect_pair in test_sets:
dt_val = incorrect_pair["dt"]
input_format_val = incorrect_pair["input_format"]
class DateTimeFormTester(forms.Form):
dt =
forms.DateTimeField(input_formats=[incorrect_pair["input_format"]])
test_form = DateTimeFormTester({"dt": incorrect_pair["dt"]})
if test_form.is_valid():
print(f"Incorrectly determined {dt_val} as valid for
input_format {input_format_val}")
}}}
Was not a problem in Django 3.0.14, can reproduce in Django 3.1.14 with
the above snippet.
I believe the problem stems from this bit of code in django.forms.fields:
{{{
try:
result = parse_datetime(value.strip())
except ValueError:
raise ValidationError(self.error_messages['invalid'],
code='invalid')
if not result:
result = super().to_python(value)
}}}
which means that if we are able to `parse_datetime` we never run
`super().to_python(value)` which is what checks the format is correct.
--
Ticket URL: <https://code.djangoproject.com/ticket/34004>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.