meteorainer
meteorainer

Reputation: 952

Validating datetime-local input in Django 1.9 Form

The HTML5 input datetime-local returns it's value in %Y-%m-%dT%H:%M format. Notice the T between the day and hour. I've added that to the field in the form like this:

end = forms.DateTimeField(required=True, input_formats='%Y-%m-%dT%H:%M')

and the template manually creates the field:

<input type="datetime-local" id="id_end" name="end" value="{{ form.end.value }}">

Inspection of the request reveals the form data does indeed send the field in a format matching the validator: 2016-04-06T17:18

So what is the format validator returning a validation failure?

Upvotes: 4

Views: 3005

Answers (1)

Shang Wang
Shang Wang

Reputation: 25539

I think input_formats parameter is a list not a string(as the name implies). Please check the django doc about DateTimeField input_formats.

input_formats

A list of formats used to attempt to convert a string to a valid datetime.date object.

Upvotes: 4

Related Questions