Skip to Content
Flask Web开发:基于Python的Web应用开发实战(第2版)
book

Flask Web开发:基于Python的Web应用开发实战(第2版)

by Miguel Grinberg
August 2018
Intermediate to advanced
230 pages
6h 42m
Chinese
O'Reilly Media, Inc. Beijing
Content preview from Flask Web开发:基于Python的Web应用开发实战(第2版)
模板
31
3.6
 使用
Flask-Moment
本地化日期和时间
如果
Web
应用的用户来自世界各地,那么处理日期和时间可不是一个简单的任务。
服务器需要统一时间单位,这和用户所在的地理位置无关,所以一般使用协调世界时
UTC
coordinated universal time
)。不过用户看到
UTC
格式的时间会感到困惑,他们更希
望看到当地时间,而且采用当地惯用的格式。
要想在服务器上只使用
UTC
时间,一个优雅的解决方案是,把时间单位发送给
Web
浏览
器,转换成当地时间,然后用
JavaScript
渲染。
Web
浏览器可以更好地完成这一任务,因
为它能获取用户计算机中的时区和区域设置。
有一个使用
JavaScript
开发的优秀客户端开源库,名为
Moment.js
,它可以在浏览器中渲染
日期和时间。
Flask-Moment
是一个
Flask
扩展,能简化把
Moment.js
集成到
Jinja2
模板中
的过程。
Flask-Moment
使用
pip
安装:
(venv) $
pip install flask-moment
这个扩展的初始化方法与
Flask-Bootstrap
类似,所需的代码如示例
3-11
所示。
示例
3-11
 
hello.py
:初始化
Flask-Moment
from flask_moment import Moment
moment = Moment(app)
除了
Moment.js
Flask-Moment
还依赖
jQuery.js
。因此,要在
HTML
文档的某个地方引入
这两个库,可以直接引入,这样可以选择使用哪个版本 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python机器学习基础教程

Python机器学习基础教程

Andreas C. Müller, Sarah Guido
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
程序设计导论:Python语言实践

程序设计导论:Python语言实践

罗伯特 塞奇威克, 凯文 韦恩, 罗伯特 唐德罗

Publisher Resources

ISBN: 9787115489456