<p>The local date and time is {{ moment(current_time).format('LLL') }}.</p>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}</p>
在网站中不现实时间
然而直接打
<p>{{ current_time }}</p>
会显示时间
hello.py
from flask import Flask, render_template
from flask import Flask
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from datetime import datetime
app = Flask(__name__)
manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
now = datetime.now()
@app.route('/')
def index():
return render_template('index.html',current_time=datetime.utcnow())
@app.route("/user/<name>")
def user(name):
return render_template("user.html", name = name )
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html"), 404
if __name__ == "__main__":
app.run(debug = True)
manager.run()
index.html
templates/index.html: Jinja2
<h1>Hello world!</h1>
<p>The local date and time is {{ moment(current_time).format('LLL') }}.</p>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}</p>
<p>{{ current_time }}</p>
需要在base.html載入moment.js函式庫的,是不是忘了?
嗯我最近也在看這個,遇到和你一樣的問題,不知道你解決了沒有
我試了一下,如果在head引入js就可以
{% block head %}
{{ moment.include_jquery( ) }}
{{ moment.include_moment() }}
{{ moment.lang("zh-CN") }}
{% endblock %}
但是這樣樣式會衝突。
題主你好,請問你現在這個問題解決了嗎? 我也在看《Flask Web開發》這本書,也遇到了同樣的問題。