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版)
应用的基本结构
11
def index():
return '<h1>Hello World!</h1>'
@app.route('/user/<name>')
def user(name):
return '<h1>Hello, {}!</h1>'.format(name)
如果你已经从
GitHub
上克隆了这个应用的
Git
仓库,那么可以执行
git
checkout 2b
检出应用的这个版本。
测试动态路由前,请确保服务器正在运行中,然后访问
http://localhost:5000/user/Dave
。应
用会显示一个使用
name
动态参数生成的欢迎消息。试着在
URL
中设定不同的名字,可以
看到,视图函数总是使用指定的名字生成响应。图
2-2
展示了一个示例。
2-2:动态路由
2.6
 调试模式
Flask
应用可以在
调试模式
中运行。在这个模式下,开发服务器默认会加载两个便利的工
具:
重载器
调试器
启用重载器后,
Flask
会监视项目中的所有源码文件,发现变动时自动重启服务器。在开
发过程中运行启动重载器的服务器特别方便,因为每次修改并保存源码文件后,服务器都
会自动重启,让改动生效。
调试器是一个基于
Web
的工具,当应用抛出未处理的异常时,它会出现在浏览器中。此
时,
Web
浏览器变成一个交互式栈跟踪,你可以在里面审查源码,在调用栈的任何位置计
12
2
算表达式。调试器的界面如图
2-3
所示。
2-3Flask 的调试器
调试模式默认禁用。若想启用,在执行 flask run 命令之前设定 ...
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