如何在Flask模板中显示当前年份? [英] How to display current year in Flask template?

查看:147
本文介绍了如何在Flask模板中显示当前年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何在Flask模板中输出当前年份。我知道在Django中您可以使用 {%now Y%}。,但是有Flask等效项吗?到目前为止,我一直无法找到任何东西。

I am looking to find out how to output the current year in a Flask template. I know in Django you can use {% now "Y" %}., but is there a Flask equivalent? I have been unable to find anything during my research thus far.

推荐答案

使用模板上下文处理器,将当前日期传递给每个模板,然后呈现其 year 属性。

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 }}

或者如果大多数模板中不需要它,请使用 render 传递对象。

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

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

这篇关于如何在Flask模板中显示当前年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆