How to display current year in Flask template?

Use a template context processor to pass the current date to every template, then render its year attribute.

from datetime import datetime

@app.context_processor
def inject_now():
    return {'now': datetime.utcnow()}
{{ now.year }}

Or pass the object with render if you don’t need it in most templates.

return render_template('show.html', now=datetime.utcnow())

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)