使用Flask-Moment本地化日期和时间
1.安装flask-moment模块
(venv) $ pip install flask-moment
(venv) $ pip install flask-script
2.初始化 Flask-Moment(以下为Hello.py所要添加的代码)
from datetime import datetime from flask_script import Manager(切记一定要引入这个flask_script) from flask_moment import Moment moment = Moment(app) manager = Manager(app) @app.route('/') def index(): return render_template('index.html',current_time=datetime.utcnow())
3.index.html
{% extends "base.html" %} {% import "bootstrap/wtf.html" as wtf %} {% block title %}Flasky{% endblock %} {% block page_content %} <div class="page-header"> <h1>Hello,world!</h1> </div> <p>The local date and time is {{ moment(current_time).format('LLL') }}.</p> <p>That was {{ moment(current_time).fromNow(refresh=True) }}.</p> {% endblock %}