使用模板上下文处理器将当前日期传递给每个模板,然后呈现其year属性。
from datetime import datetime@app.context_processordef inject_now(): return {'now': datetime.utcnow()}{{ now.year }}render如果大多数模板中不需要它,也可以将其与一起传递。
return render_template('show.html', now=datetime.utcnow())
使用模板上下文处理器将当前日期传递给每个模板,然后呈现其year属性。
from datetime import datetime@app.context_processordef inject_now(): return {'now': datetime.utcnow()}{{ now.year }}render如果大多数模板中不需要它,也可以将其与一起传递。
return render_template('show.html', now=datetime.utcnow())