简体   繁体   中英

WTForms DateField returning None

I have a DateField that is part of a WTForm in Flask

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

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

When the form is submitted it returns 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. The corrected field looks like this:

dob = DateField([InputRequired()])

When submitted the form.dob.data now outputs the correct date. This answer on another semi-unrelated question helped me out: 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.

Thanks for sharing that solution. It was what I needed to solve my own form. InputRequired worked like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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