Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WTForms equivalent to <input type="datetime-local">


I would like to know if it's possible to create a dropdown datetime field for the DateTimeField in WTForms, the way renders in HTML.
A dropdown menu is much more user friendly than the default in WTForms where a user has to type in the date and time in the exact correct format.
And if it's not possible using only WTForms then how would I be able to implement the raw HTML into a WTForm as I would still like to use the security CSRF token that is provided by WTForms.
Thanks in advance!
like image 433
Markus Gilboa Avatar asked Nov 14 '25 21:11

Markus Gilboa


1 Answers

The html5 fields provided by WTForms do include a DateTimeLocalField - v2.3.x, v3.0.x. Thus (v2.3)

from wtforms.fields.html5 import DateTimeLocalField
dttm = DateTimeLocalField("Date/Time: ", format="%Y-%m-%dT%H:%M")

will show the html5 date picker and time picker popups on browsers that support them (support is varied).

I find you need to specify format="%Y-%m-%dT%H:%M" - note the T - because the default format="%Y-%m-%d %H:%M" fails validation (see quote below).

input type="datetime" appears to be deprecated, so browsers fall back to plain text entry: input type="datetime" | MDN. input type="datetime-local", and thus WTForms DateTimeLocalField, should always be used: input type="datetime-local" | MDN.

From the latter:

One thing to note is that the displayed date and time formats differ from the actual value; the displayed date and time are formatted according to the user's locale as reported by their operating system, whereas the date/time value is always formatted YYYY-MM-DDThh:mm.

like image 184
Joe P Avatar answered Nov 17 '25 11:11

Joe P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!