python - WTForms 等同于 <input type ="datetime-local">

标签 python flask flask-wtforms wtforms


我想知道是否可以在 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/

相关文章:

python - 为 'group number' 和 '0' s 的 pandas 数据框列创建 '1' 列

javascript - 相当于 Python str() 方法的 JS

python - 计算第一个 1000 位的斐波那契数(欧拉计划 #25)

python - tar 文件并使用 Flask 应用程序下载

python - 从 Jupyter 停止 Flask 服务器

python - 为什么 PyTorch 找不到我的 NVIDIA 驱动程序来支持 CUDA?

python - Flask SQLAlchemy NOT NULL 约束在主键上失败

python - 有条件地使字段为必填字段 Field Flask Forms

python - 在 SelectField 和 HiddenField 之间动态更改 WTForms 字段类型

python - 使用 WTForms 进行表单验证,并使用 Flask 中的表单数据自动填充 SQLAlchemy 模型