wtforms+flask today's date as a default value

可紊 提交于 2019-11-30 02:44:44

问题


I did a small Flask app with a form with two date fields, and this is how I populate the values:

class BoringForm(Form):
    until = DateTimeField("Until",
                          format="%Y-%m-%dT%H:%M:%S", 
                          default=datetime.today(),
                          validators=[validators.DataRequired()])

However, this is generated only once, server-side, which means that tomorrow I'll still get yesterday's date. I tried passing obj=something to the constructor, where something was an OrderedDict with a key called since, but it didn't work. Ideas?


回答1:


Just drop the brackets on the callable:

class BoringForm(Form):
    until = DateTimeField(
        "Until", format="%Y-%m-%dT%H:%M:%S",
        default=datetime.today, ## Now it will call it everytime.
        validators=[validators.DataRequired()]
    )


来源:https://stackoverflow.com/questions/27421166/wtformsflask-todays-date-as-a-default-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!