我想用烧瓶获取当前的UTC时间,所以我写了以下代码:
from flask import Flask
from flask_moment import Moment
from flask import render_template
from datetime import datetime
app = Flask(__name__)
moment = Moment(app)
@app.route('/')
def index():
return render_template('index.html', current_time=datetime.utcnow())
index.html
包含以下代码:
<p>The local date and time is {{ moment(current_time).format('LLL') }}.</p>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}</p>
当我运行应用程序并刷新时,我得到以下结果:
The local date and time is.
That was
问题是什么,我该如何解决?
如果您检查页面源代码,您会看到内容在那里,但span
标记具有内联style="display: none"
CSS,这会隐藏实际视图中的内容。要解决此问题,您需要将时刻脚本包含在您的templates/index.html
中。
<!doctype html>
<head>
<title>FlaskMoment UTC Current Time</title>
{{ moment.include_jquery() }}
{{ moment.include_moment() }}
</head>
<body>
<p>The local date and time is {{ moment(current_time).format('LLL') }}.</p>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}</p>
</body>