'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?
Solution 1:[1]
I stumbled upon this, and the answer from @Doobeh is not enough for me, as I want two dates - today and "in two weeks day".
So here's general code using constructor.
def __init__(self, formdata=None, obj=None, **kwargs):
super().__init__(formdata=formdata, obj=obj, **kwargs)
self.date_from.data = datetime.today()
self.date_to.data = datetime.today() + timedelta(days=14)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | janpeterka |
