我想知道是否可以在 WTForms 中为 DateTimeField 创建一个下拉日期时间字段,即在 HTML 中呈现的方式。
下拉菜单比 WTForms 中的默认菜单更友好,用户必须以完全正确的格式输入日期和时间。
如果仅使用 WTForms 是不可能的,那么我将如何将原始 HTML 实现到 WTForm 中,因为我仍然想使用 WTForms 提供的安全 CSRF token 。
提前致谢!
最佳答案
WTForms 提供的 html5 字段确实包含 DateTimeLocalField - v2.3.x , v3.0.x .因此(v2.3)
from wtforms.fields.html5 import DateTimeLocalField
dttm = DateTimeLocalField("Date/Time: ", format="%Y-%m-%dT%H:%M")
将在支持它们的浏览器上显示 html5 日期选择器和时间选择器弹出窗口(支持多种多样)。
我发现您需要指定 format="%Y-%m-%dT%H:%M"
- 注意 T - 因为默认 format="%Y- %m-%d %H:%M"
验证失败(参见下面的引用)。
input type="datetime"
似乎已被弃用,因此浏览器回退到纯文本输入:input type="datetime" | MDN .
input type="datetime-local"
,因此应始终使用 WTForms DateTimeLocalField
:input type="datetime-local" | MDN .
来自后者:
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.
关于python - WTForms 等同于 <input type ="datetime-local">,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71071472/