logo资料库

Python web框架.Flask中文手册.pdf

第1页 / 共219页
第2页 / 共219页
第3页 / 共219页
第4页 / 共219页
第5页 / 共219页
第6页 / 共219页
第7页 / 共219页
第8页 / 共219页
资料共219页,剩余部分请下载后查看
I 用户指南
前言
``micro''是什么意思?
一个框架和一个例子
Web开发是危险的
Python 3的状态
安装
virtualenv
安装到系统全局
生活在边缘
Windows 平台下的 easy_install
快速上手
一个最小的应用
调试模式
路由
静态文件
模板渲染
访问 Request 数据
跳转和错误
会话
消息闪烁
日志记录
WSGI 中间件集成
教程
介绍 Flaskr
初始准备: 创建目录
第一步: 数据库模式
第二步: 应用程序构建代码
第三步: 创建一个数据库
第四步:请求数据库连接
第五步: 视图函数
第六步: 模版
第七步: 添加样式
附加: 自动测试
模版
Jinja安装
标准上下文
标准过滤器
控制自动转义
引入过滤器
上下文处理器
测试Flask应用程序
要先有应用程序
测试骨架
处女测
日志的输入输出
测试添加功能
其他测试技巧
保持现场
处理应用异常
报错邮件
日志文件
日志格式
其他代码库
Configuration Handling
Configuration Basics
Builtin Configuration Values
Configuring from Files
Configuration Best Practices
Development / Production
Signals
Subscribing to Signals
Creating Signals
Sending Signals
Decorator Based Signal Subscriptions
Core Signals
Pluggable Views
Basic Principle
Method Hints
Method Based Dispatching
The Request Context
Diving into Context Locals
How the Context Works
Callbacks and Errors
Teardown Callbacks
Notes On Proxies
Context Preservation on Error
Modular Applications with Blueprints
Why Blueprints?
The Concept of Blueprints
My First Blueprint
Registering Blueprints
Blueprint Resources
Building URLs
Working with the Shell
Creating a Request Context
Firing Before/After Request
Further Improving the Shell Experience
Patterns for Flask
Larger Applications
Application Factories
Application Dispatching
Using URL Processors
Deploying with Distribute
Deploying with Fabric
Using SQLite 3 with Flask
SQLAlchemy in Flask
Uploading Files
Caching
View Decorators
Form Validation with WTForms
Template Inheritance
Message Flashing
AJAX with jQuery
Custom Error Pages
Lazily Loading Views
MongoKit in Flask
Adding a favicon
Deployment Options
mod_wsgi (Apache)
CGI
FastCGI
uWSGI
Other Servers
搞大了?!
干嘛要开分舵?
像大师一样游刃有余
通过网络社区进行交流
II API 参考
API
Application Object
Module Objects
Incoming Request Data
Response Objects
Sessions
Application Globals
Useful Functions and Classes
Message Flashing
Returning JSON
Template Rendering
Configuration
Useful Internals
Signals
III 其它事项
Design Decisions in Flask
The Explicit Application Object
One Template Engine
Micro with Dependencies
Thread Locals
What Flask is, What Flask is Not
HTML/XHTML FAQ
History of XHTML
History of HTML5
HTML versus XHTML
What does ``strict'' mean?
New technologies in HTML5
What should be used?
Security Considerations
Cross-Site Scripting (XSS)
Cross-Site Request Forgery (CSRF)
JSON Security
Unicode in Flask
Automatic Conversion
The Golden Rule
Encoding and Decoding Yourself
Configuring Editors
Flask Extension Development
Anatomy of an Extension
``Hello Flaskext!''
Initializing Extensions
The Extension Code
Adding an init_app Function
End-Of-Request Behavior
Learn from Others
Approved Extensions
Pocoo Styleguide
General Layout
Expressions and Statements
Naming Conventions
Docstrings
Comments
Upgrading to Newer Releases
Version 0.7
Version 0.6
Version 0.5
Version 0.4
Version 0.3
Flask Changelog
Version 0.6
Version 0.5.2
Version 0.5.1
Version 0.5
Version 0.4
Version 0.3.1
Version 0.3
Version 0.2
Version 0.1
License
Authors
General License Definitions
Flask License
Flask Documentation Release 0.7.2 September 06, 2011
CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 3 3 4 4 4 5 5 6 6 7 . . . . . . . . 9 . 9 . 10 . 11 . 14 . 15 . 16 . 18 . 19 . 20 . 20 . 21 23 . 23 . 24 . 25 . 25 . 26 . 27 . 28 . 30 . 31 i I 用户指南 1 前言 1.1 ``micro''是什么意思? . 1.2 一个框架和一个例子 . 1.3 Web开发是危险的 . . . . 1.4 Python 3的状态 . . . . . . . . . . . . . . . . . . . . . . . 2 安装 . . . . . 2.1 virtualenv . . 2.2 安装到系统全局 . 2.3 生活在边缘 . . . 2.4 Windows 平台下的 easy_install . . . . . . . . . . . . . . . . . . . . . . . 3 快速上手 . . . . . . . . . . . . . . . 3.1 一个最小的应用 . . 3.2 调试模式 . . . . 3.3 路由 . . . . 3.4 静态文件 . 3.5 模板渲染 . . . 3.6 访问 Request 数据 . . 3.7 跳转和错误 . . . 3.8 会话 . . 3.9 消息闪烁 . . . 3.10 日志记录 . . . 3.11 WSGI 中间件集成 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 教程 . . . . . . . . . . . . . . 4.1 介绍 Flaskr . . . 4.2 初始准备: 创建目录 . . 4.3 第一步: 数据库模式 . 4.4 第二步: 应用程序构建代码 . 4.5 第三步: 创建一个数据库 . 4.6 第四步:请求数据库连接 . 4.7 第五步: 视图函数 . . . 4.8 第六步: 模版 . . 4.9 第七步: 添加样式 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.10 附加: 自动测试 . 5 模版 . . . . 5.1 Jinja安装 . . . 5.2 标准上下文 . 5.3 标准过滤器 . . 5.4 控制自动转义 . 5.5 引入过滤器 . . 5.6 上下文处理器 . . . . . . . . . 6 测试Flask应用程序 . . . . . . . 6.1 要先有应用程序 . . 6.2 测试骨架 . 6.3 处女测 . . . 6.4 日志的输入输出 . . 6.5 测试添加功能 . . 6.6 其他测试技巧 . 6.7 保持现场 . . . . . 7 处理应用异常 . 7.1 报错邮件 . . 7.2 日志文件 . 7.3 日志格式 . . 7.4 其他代码库 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Configuration Handling . . 8.1 Configuration Basics . . 8.2 Builtin Configuration Values . 8.3 Configuring from Files . . 8.4 Configuration Best Practices . 8.5 Development / Production . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Signals . 9.1 Subscribing to Signals . . . 9.2 Creating Signals . . 9.3 Sending Signals . . 9.4 Decorator Based Signal Subscriptions . 9.5 Core Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Pluggable Views . 10.1 Basic Principle . 10.2 Method Hints . . 10.3 Method Based Dispatching . . . . . . . . . . . . . . . 11 The Request Context 11.1 Diving into Context Locals . 11.2 How the Context Works . . . . 11.3 Callbacks and Errors . 11.4 Teardown Callbacks . . . . . . . . . ii . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 33 . 33 . 33 . 34 . 34 . 35 . 35 37 . 37 . 37 . 38 . 39 . 40 . 40 . 41 43 . 43 . 44 . 45 . 46 47 . 47 . 48 . 49 . 49 . 50 53 . 53 . 55 . 55 . 56 . 56 59 . 59 . 60 . 61 63 . 63 . 64 . 65 . 65
11.5 Notes On Proxies . . 11.6 Context Preservation on Error . . . . . . . . . . 12 Modular Applications with Blueprints . . . . . . 12.1 Why Blueprints? . . 12.2 The Concept of Blueprints . . 12.3 My First Blueprint . 12.4 Registering Blueprints . . . . 12.5 Blueprint Resources . 12.6 Building URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Working with the Shell . 13.1 Creating a Request Context . 13.2 Firing Before/After Request . . 13.3 Further Improving the Shell Experience . . . . . . . . . . . . . . . 14 Patterns for Flask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 . 67 69 . 69 . 70 . 70 . 70 . 71 . 72 75 . 75 . 76 . 76 77 . 77 . 79 . 81 . 84 . 86 . 89 . 93 . 95 . 98 . 102 . 103 . 106 . 108 . 109 . 111 . 114 . 115 . 117 . 120 123 . 123 . 126 . 127 . 129 . 130 133 . 133 . 134 . 134 iii . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.1 Larger Applications . . . . . 14.2 Application Factories . . . . 14.3 Application Dispatching . . 14.4 Using URL Processors . . . . 14.5 Deploying with Distribute . . 14.6 Deploying with Fabric . . . 14.7 Using SQLite 3 with Flask . . . 14.8 SQLAlchemy in Flask . . 14.9 Uploading Files . . . . . . 14.10Caching . . 14.11View Decorators . . . . 14.12Form Validation with WTForms . . 14.13Template Inheritance . . . 14.14Message Flashing . . 14.15AJAX with jQuery . . 14.16Custom Error Pages . . . 14.17Lazily Loading Views . . . 14.18MongoKit in Flask . 14.19Adding a favicon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Deployment Options 15.1 mod_wsgi (Apache) . . 15.2 CGI . . . . . . 15.3 FastCGI . 15.4 uWSGI . . 15.5 Other Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 搞大了?! 16.1 干嘛要开分舵? . . . 16.2 像大师一样游刃有余 . . 16.3 通过网络社区进行交流 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
II API 参考 17 API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17.1 Application Object . . 17.2 Module Objects . . . 17.3 Incoming Request Data . . . . 17.4 Response Objects . . . 17.5 Sessions . . . 17.6 Application Globals . . . 17.7 Useful Functions and Classes . . 17.8 Message Flashing . . . 17.9 Returning JSON . . . . 17.10Template Rendering . . . 17.11Configuration . . 17.12Useful Internals . . . . . . 17.13Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . III 其它事项 18 Design Decisions in Flask . 18.1 The Explicit Application Object . . . 18.2 One Template Engine . . . . 18.3 Micro with Dependencies . 18.4 Thread Locals . . . . 18.5 What Flask is, What Flask is Not . . . . . . . . . . . . . . 19 HTML/XHTML FAQ . . . . . 19.1 History of XHTML . . . 19.2 History of HTML5 . . . 19.3 HTML versus XHTML . . 19.4 What does ``strict'' mean? . . 19.5 New technologies in HTML5 . . 19.6 What should be used? . . . . . . . . . . . . . . . . . . . . . . . 20 Security Considerations 20.1 Cross-Site Scripting (XSS) . . 20.2 Cross-Site Request Forgery (CSRF) . 20.3 JSON Security . . . . . . . . . . . . . . . . . 21 Unicode in Flask . 21.1 Automatic Conversion . . 21.2 The Golden Rule . . 21.3 Encoding and Decoding Yourself . 21.4 Configuring Editors . . . . . . . . . . . . . . . . . . . . . . 22 Flask Extension Development 22.1 Anatomy of an Extension . 22.2 ``Hello Flaskext!'' . . . . . . . . . . . . . . iv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 137 . 137 . 151 . 154 . 156 . 157 . 157 . 158 . 162 . 163 . 164 . 164 . 166 . 167 169 171 . 171 . 172 . 173 . 173 . 174 175 . 175 . 176 . 176 . 177 . 178 . 178 179 . 179 . 180 . 180 183 . 183 . 184 . 184 . 184 187 . 187 . 188
. . . 22.3 Initializing Extensions . 22.4 The Extension Code . . . 22.5 Adding an init_app Function . 22.6 End-Of-Request Behavior . . . . 22.7 Learn from Others . . 22.8 Approved Extensions . . . . . . . . . . . . . . . 23 Pocoo Styleguide . . . . . . 23.1 General Layout . 23.2 Expressions and Statements . . 23.3 Naming Conventions . 23.4 Docstrings . . . . . 23.5 Comments . . . . . . . . . . . . . . . . . . . . . 24 Upgrading to Newer Releases . . . . . 24.1 Version 0.7 . 24.2 Version 0.6 . 24.3 Version 0.5 . 24.4 Version 0.4 . 24.5 Version 0.3 . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Flask Changelog . 25.1 Version 0.6 . . 25.2 Version 0.5.2 . 25.3 Version 0.5.1 . 25.4 Version 0.5 . . 25.5 Version 0.4 . . 25.6 Version 0.3.1 . 25.7 Version 0.3 . . . 25.8 Version 0.2 . 25.9 Version 0.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 License 26.1 Authors . . 26.2 General License Definitions . 26.3 Flask License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 . 190 . 191 . 192 . 193 . 193 195 . 195 . 196 . 197 . 198 . 198 199 . 199 . 203 . 203 . 203 . 204 205 . 205 . 206 . 206 . 206 . 206 . 207 . 207 . 207 . 208 209 . 209 . 210 . 210 v
vi
分享到:
收藏