【问题标题】:Flask doesn't show UTC time with moment.jsFlask 不使用 moment.js 显示 UTC 时间
【发布时间】:2020-07-04 00:01:04
【问题描述】:

我想用flask获取当前的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

有什么问题,我该如何解决?

【问题讨论】:

    标签: python flask web-applications momentjs


    【解决方案1】:

    如果您检查您的页面源代码,您会看到内容在那里,但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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2015-12-14
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2015-04-28
      相关资源
      最近更新 更多