How to display a date_time field in jinja2 template? (flask)

1,181 views
Skip to first unread message

Kash via StackOverflow

unread,
Nov 8, 2014, 3:28:37 PM11/8/14
to google-appengin...@googlegroups.com

I have a form with these two values (ndb is the Google App Engine ndb model):

model:

class Appointment(model.Base):
    start_time = ndb.DateTimeProperty(required=True)
    end_time = ndb.DateTimeProperty(required=True)

form (using wtforms):

class AppointmentUpdateForm(wtf.Form):
    start_time = wtforms.DateField('Start at', [wtforms.validators.required()])
    end_time = wtforms.DateField('End at', [wtforms.validators.required()])

And in the jinja2 template I have:

  {{forms.date_field(form.start_time, format='%Y-%m-%d %H:%M:%S')}}
  {{forms.date_field(form.end_time)}}

I want to display the form inputs as time widget but it looks like a date field. I think wtforms has a date_time field but how can I implement that?



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/26821713/how-to-display-a-date-time-field-in-jinja2-template-flask

dpgaspar via StackOverflow

unread,
Nov 8, 2014, 4:03:33 PM11/8/14
to google-appengin...@googlegroups.com

If you want to display a datetime widget you need to create a widget. when defining the form define the widget for it with your own widget

Define a widget take a look at this example:

https://github.com/dpgaspar/Flask-AppBuilder/blob/master/flask_appbuilder/fieldwidgets.py#L5

Then define the field like this:

class AppointmentUpdateForm(wtf.Form):
    start_time = wtforms.DateField('Start at', [wtforms.validators.required()], widget=DatePickerWidget())
    end_time = wtforms.DateField('End at', [wtforms.validators.required()], widget=DatePickerWidget())

Remember this is using bootstrap Datetime picker so you have to include it CSS and js.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/26821713/how-to-display-a-date-time-field-in-jinja2-template-flask/26822027#26822027

dpgaspar via StackOverflow

unread,
Nov 17, 2014, 4:19:24 AM11/17/14
to google-appengin...@googlegroups.com

If you want to display a datetime widget you need to create a widget. when defining the form pass the widget for it with your own widget

Define the widget, take a look at this example:

https://github.com/dpgaspar/Flask-AppBuilder/blob/master/flask_appbuilder/fieldwidgets.py#L5

Then define the field like this:

class AppointmentUpdateForm(wtf.Form):
    start_time = wtforms.DateField('Start at', [wtforms.validators.required()], widget=DatePickerWidget())
    end_time = wtforms.DateField('End at', [wtforms.validators.required()], widget=DatePickerWidget())

Remember this is using bootstrap Datetime picker so you have to include the CSS and js.

Reply all
Reply to author
Forward
0 new messages