系统环境:Ubuntu 18.04.1 LTS
Python使用的是虚拟环境:virutalenv
Python的版本:Python 3.6.9
【简说Python WEB】Flask-Moment
Javascript
有一个非常不错的优秀的客户端开源库,Moment.js
。在客户端的浏览器上加载时间。
安装Flask-Moment
$ pip install Flask-Moment
templates/base.html
引入Moment.js
库
{% block scripts %}
{{ super() }}
{{moment.include_moment() }}
{{moment.locale('zh-cn') }}
{% endblock %}
其中:moment.locale('zh-cn')
代表支持中文,具体参考文档:http://momentjs.cn/
hello.py`moment实例初始化
from flask_moment import Moment
moment = Moment(app)
hello.py
添加datatime变量
from datetime import datetime
def user(name):
return render_template('user.html', name=name,current_time=datetime.utcnow())
user.html
渲染moment
<p>当前时间:{{ moment(current_time).format('LLL') }}.</p>
源码不贴了,以后放到github下面,供大家下载测试。
演示效果: