C
C
coalesce2017-11-10 13:01:13
Django
coalesce, 2017-11-10 13:01:13

How to work with dates on a form in Django?

I can not figure out how to accept the DateTime on the form.
I get an error
Please enter a valid date and time.
Enter a valid date/time.
On the form I use the DateInput widget with type datetime-local and localize=True
forms.py

date_field = forms.DateTimeField(required=False, widget=DateInput(attrs={'type': 'datetime-local'}),
                                     initial=datetime.date.today(), localize=True)

the template looks good
zioy16eiymyt0k7e6dlmsxisvpu.png
<input type="datetime-local" value="2017-11-10T00:00">

When submitting the form, I get validation errors.
Tried adding input_formats='%yyyy-%mm-%ddT%H:%M'
However, that didn't help. Perhaps you need to set a different input_formats format ?
Is it possible (how) to get by with the standard DateTimeField validation, without specifying your own form validation?
settings.py
LANGUAGE_CODE = 'ru-ru'
TIME_ZONE = 'Europe/Moscow'
USE_I18N = True
USE_L10N = True
USE_TZ = True

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2017-11-10
@coalesce

The format is invalid. If only I looked in the dock https://docs.python.org/3/library/datetime.html#st...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question