Question

This code works, except that the populated date is format 2000-08-08. and the date picker is looking for 01-01-2000.

<form class="form form-inline" method="post" role="form">
{{ form.hidden_tag() }}
{{ wtf.form_errors(form) }}
<input data-provide="datepicker" format="mm/dd/yyyy" id="deadline"  type="text" value="{{ form.deadline.data }}" name="deadline"   required></input>
{{ wtf.form_field(form.complete) }}
{{ wtf.form_field(form.note) }}<br>
{{ wtf.form_field(form.submit) }} 
</form>  

how do i change either the prepopulated format, or the format datepicker wants? in my model i have

deadline =DateField( 'Deadline (mm/dd/yyyy)',  format='%m/%d/%Y',validators = [Required()])

can i change it in the view?

@app.route('/edit/<name>', methods=['GET', 'POST'])
def edit_task(name):
    ptask=models.Tasks.query.filter_by(task=task).first()
    form = task_form(obj=ptask)
    form.populate_obj(ptask)
    tform=task_form(request.values)

update

changing the formatting line as in

 <input data-provide="datepicker" data-date-format="mm/dd/yyyy" id="deadline"  type="text" value="{{ form.deadline.data }}" name="deadline" required></input>

had no effect, and the date still renders Year/mo/da

But, adding this to my view (as suggested below)

ptask=models.Tasks.query.filter_by(task=task).first()
form = task_form(obj=ptask)
form.populate_obj(ptask)
form.deadline.data = ptask.deadline.strftime("%m/%d/%Y")

fixed it. Thanks

Was it helpful?

Solution

For Bootstrap Datepicker, the correct date format attribute is data-date-format as mentioned in the docs. Updated code for the datepicker –

<input data-provide="datepicker" data-date-format="mm/dd/yyyy" id="deadline"  type="text" value="{{ form.deadline.data }}" name="deadline" required></input>

Above correction should solve your issue. But since you asked, you can also set the desired date format in the view –

form.deadline.data = ptask.deadline.strftime("%m/%d/%Y")

You can insert this line at the last (or second last) line of your view code after you have populated the rest of the fields in the form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top