gpt4 book ai didi

jinja2 - 如何获取金贾的当前时间

转载 作者:行者123 更新时间:2023-12-01 16:11:18 28 4
gpt4 key购买 nike

获取jinja标签中当前日期时间值的方法是什么?

在我的项目中,我需要在网站右上角显示 UTC 的当前时间。

最佳答案

我喜欢@Assem的answer 。我将在 Jinja2 模板中演示它。

#!/bin/env python3
import datetime
from jinja2 import Template

template = Template("""
# Generation started on {{ now() }}
... this is the rest of my template...
# Completed generation.
""")

template.globals['now'] = datetime.datetime.utcnow

print(template.render())

输出:

# Generation started on 2017-11-14 15:48:06.507123
... this is the rest of my template...
# Completed generation.

注意:

我拒绝了某人的编辑,他说“datetime.datetime.utcnow() 是一种方法,而不是属性”。虽然他们是“正确的”,但他们误解了不包括 () 的意图。包含 () 会导致在定义对象 template.globals['now'] 时调用该方法。这将导致模板中每次使用 now 都会呈现相同的值,而不是从 datetime.datetime.utcnow 获取当前结果。

关于jinja2 - 如何获取金贾的当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104914/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com