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版)
4
Web
表单
3
章编写的模板都是单向的,所有信息都从服务器流向用户。然而,对多数应用来说,
还需要沿相反的方向流动信息,把用户提供的数据交给服务器来处理。
使用
HTML
可以创建
Web
表单,供用户填写信息。表单数据由
Web
浏览器提交给服务
器,这一过程通常使用
POST
请求。第
2
章介绍的
Flask
请求对象包含客户端在请求中发送
的全部信息,对包含表单数据的
POST
请求来说,用户填写的信息通过
request.form
访问。
尽管
Flask
的请求对象提供的信息足以处理
Web
表单,但有些任务很单调,而且要重复操
作。比如,生成表单的
HTML
代码和验证提交的表单数据。
Flask-WTF
扩展可以把处理
Web
表单的过程变成一种愉悦的体验。这个扩展对独立的
WTForms
包进行了包装,方便集成到
Flask
应用中。
Flask-WTF
及其依赖可使用
pip
安装:
(venv) $
pip install flask-wtf
4.1
 配置
与其他多数扩展不同,
Flask-WTF
无须在应用层初始化,但是它要求应用配置一个
密钥
密钥是一个由随机字符构成的唯一字符串,通过加密或签名以不同的方式提升应用的安全
性。
Flask
使用这个密钥保护用户会话,以防被篡改。每个应用的密钥应该不同,而且不
能让任何人知道。示例
4-1
展示如何在
Flask
应用中配置密钥。
示例
4-1
 
hello.py
:配置
Flask-WTF
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess ...
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