简体   繁体   English

WTForms DateField 返回 None

[英]WTForms DateField returning None

I have a DateField that is part of a WTForm in Flask我有一个 DateField,它是 Flask 中 WTForm 的一部分

from wtforms.fields.html5 import DateField

dob = DateField('Date of Birth', [InputRequired()], format='%m-%d-%Y')
    if form.is_submitted():
        print(form.dob.data)

HTML Template HTML模板

{{ form.dob.label }}
<input type="date" id="DateofBirth" name="dob" class="form-control">

When the form is submitted it returns None .当表单被提交时,它返回None All other fields of the form work properly.表单的所有其他字段都可以正常工作。 Any suggestions?有什么建议?

Solved it after a bit.过了一会儿解决了。 Figured out I needed to remove the format parameter in the DateField object.发现我需要删除 DateField 对象中的format参数。 The corrected field looks like this:更正后的字段如下所示:

dob = DateField([InputRequired()])

When submitted the form.dob.data now outputs the correct date.当提交form.dob.data现在输出正确的日期。 This answer on another semi-unrelated question helped me out: https://stackoverflow.com/a/9519493/16549743 .这个关于另一个半无关问题的答案帮助了我: https : //stackoverflow.com/a/9519493/16549743 I guess HTML5 can't accept different formats as explained in that answer and passing the format parameter was messing things up.我猜 HTML5 不能接受该答案中解释的不同格式,并且传递 format 参数会将事情搞砸。

Thanks for sharing that solution.感谢您分享该解决方案。 It was what I needed to solve my own form.这是我解决我自己的形式所需要的。 InputRequired worked like a charm. InputRequired 很有魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM