<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>
You need to load the moment.js library in base.html, did you forget it?
Well, I was reading this recently and encountered the same problem as you. I don’t know if you have solved it. I tried it and it worked if I introduced js in the head.
{% block head %}
{{ moment.include_jquery( ) }}
{{ moment.include_moment() }}
{{ moment.lang("zh-CN") }}
{% endblock %}
But this style will conflict.
Hello questioner, have you solved this problem now? I was also reading the book "Flask Web Development" and encountered the same problem.