O'Reilly logo

Instant Flask Web Development by Ron DuPlain

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using custom template filters in Jinja (Advanced)

To wrap up our templates, we need to clean up the display of some of our fields.

Getting ready

We move to the filters.py file from the sched directory and work with app.py again.

How to do it...

  1. We want to display time with a clean format. In filters.py:
    def do_datetime(dt, format=None): """Jinja template filter to format a datetime object.""" if dt is None: # By default, render an empty string. return '' if format is None: # No format is given in the template call. # Use a default format. # # Format time in its own strftime call in order to: # 1. Left-strip leading 0 in hour display. # 2. Use 'am'/'pm' (lower case) instead of 'AM'/'PM'. formatted_date = dt.strftime('%Y-%m-%d - %A') formatted_time =\ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required