Search code examples
pythonjinja2wtforms

Python - WTForms datetime occurs str


I am wondering why I am getting error in jinja2:

AttributeError: 'str' object has no attribute 'strftime'

Even though datetime object has strftime():

order_date = DateField("Payment Date up to:", format="%Y-%m-%d", default=(datetime.date.today()+datetime.timedelta(days=60)).strftime("%Y-%m-%d"))

So, somehow it returns 'str' in datetime.date.today()+datetime.timedelta(days=60)

Could you, please, help me, how to fix this error and why it occured?

P.s. code in html:

<div class="input-group date">
    <input type="text" data-date-format="yyyy-mm-dd" data-provide="datepicker" autocomplete="off"
    class="form-control pull-right input-sm" name="order_date" id="order_date"
    placeholder="YYYY-MM-DD" value="{{cd.form.order_date.data}}" title="Default value is Now + 60 days">
    <div class="input-group-addon">
        <i class="fa fa-calendar"></i>
    </div>
</div>

Solution

  • strftime() returns a string. and DateField is expecting a datetime object based on the format line.

    order_date = DateField("Payment Date up to:", format="%Y-%m-%d", default=datetime.date.today()+datetime.timedelta(days=60))
    order_date = order_date.strftime("%Y-%m-%d")