﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34004	DateTimeField for Forms does not always respect input_formats	Emily Hontoria	nobody	"When creating a new form with a single `DateTimeField` and supplying a string formatted datetime (for example `""2021-12-10 10:00:00""`) the form is considered valid even if the given datetime is not in a supported `input_formats`.

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."	Bug	closed	Forms	3.1	Normal	invalid			Unreviewed	0	0	0	0	0	0
