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版)
用户资料
107
10-1:用户资料页面
如果你从
GitHub
上克隆了这个应用的
Git
仓库,那么可以执行
git checkout
10a
检出应用的这个版本。这个版本包含一个数据库迁移,检出代码后记得
执行
flask db upgrade
命令。
10.3
 资料编辑器
用户资料的编辑分两种情况。最显而易见的情况是,用户要进入一个页面,输入自己的资
料,以便显示在自己的资料页面上。还有一种不太明显但也同样重要的情况,那就是要让
管理员能够编辑任意用户的资料——不仅要能编辑用户的个人信息,还要能编辑用户不能
直接访问的 User 模型字段,例如用户角色。这两种编辑需求有本质上的区别,所以我们将
创建两个不同的表单。
10.3.1
 用户级资料编辑器
普通用户的资料编辑表单如示例
10-7
所示。
示例
10-7
 
app/main/forms.py
:资料编辑表单
class EditProfileForm(FlaskForm):
name = StringField('Real name', validators=[Length(0, 64)])
location = StringField('Location', validators=[Length(0, 64)])
about_me = TextAreaField('About me')
submit = SubmitField('Submit')
注意,这个表单中的所有字段都是可选的,因此长度验证函数的最小值为零。显示这个表
单的路由定义如示例
10-8 ...
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