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版)
2
应用的基本结构
本章将带领你了解
Flask
应用各部分的作用,编写并运行第一个
Flask Web
应用。
2.1
 初始化
所有
Flask
应用都必须创建一个
应用实例
Web
服务器使用一种名为
Web
服务器网关接口
WSGI
Web server gateway interface
,读作“
wiz-ghee
”)的协议,把接收自客户端的所有
请求都转交给这个对象处理。应用实例是 Flask 类的对象,通常由下述代码创建:
from flask import Flask
app = Flask(__name__)
Flask 类的构造函数只有一个必须指定的参数,即应用主模块或包的名称。在大多数应用
中,
Python
__name__ 变量就是所需的值。
传给
Flask
应用构造函数的
__name__
参数可能会让
Flask
开发新手心生困惑。
Flask
用这个参数确定应用的位置,进而找到应用中其他文件的位置,例如
图像和模板。
后文会介绍更复杂的应用初始化方式,不过对简单的应用来说,上面的代码足够了。
2.2
 路由和视图函数
客户端(例如
Web
浏览器)把
请求
发送给
Web
服务器,
Web
服务器再把请求发送给
Flask
应用实例。应用实例需要知道对每个
URL
的请求要运行哪些代码,所以保存了一个
URL
7
8
2
Python ...
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