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版)
Web
表单
35
app.config 字典可用于存储
Flask
、扩展和应用自身的配置变量。使用标准的字典句法就
能把配置添加到 app.config 对象中。这个对象还提供了一些方法,可以从文件或环境中导
入配置。第
7
章将介绍管理大型应用配置的合理方式。
Flask-WTF
之所以要求应用配置一个密钥,是为了防止表单遭到跨站请求伪造(
CSRF
cross-site request forgery
)攻击。恶意网站把请求发送到被攻击者已登录的其他网站时,就
会引发
CSRF
攻击。
Flask-WTF
为所有表单生成安全令牌,存储在用户会话中。令牌是一
种加密签名,根据密钥生成。
为了增强安全性,密钥不应该直接写入源码,而要保存在环境变量中。这一
技术在第
7
章介绍。
4.2
 表单类
使用
Flask-WTF
时,在服务器端,每个
Web
表单都由一个继承自 FlaskForm 的类表示。这
个类定义表单中的一组字段,每个字段都用对象表示。字段对象可附属一个或多个
验证函
。验证函数用于验证用户提交的数据是否有效。
示例
4-2
是一个简单的
Web
表单,包含一个文本字段和一个提交按钮。
示例
4-2
 
hello.py
:定义表单类
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
class NameForm(FlaskForm):
name ...
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