Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wtforms+flask today's date as a default value

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?

like image 226
marco Avatar asked Dec 11 '14 10:12

marco


1 Answers

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()]
    )
like image 181
Doobeh Avatar answered Nov 10 '22 23:11

Doobeh