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
表单
37
4.3
 把表单渲染成
HTML
表单字段是可调用的,在模板中调用后会渲染成
HTML
。假设视图函数通过 form 参数把
一个 NameForm 实例传入模板,在模板中可以生成一个简单的
HTML
表单,如下所示:
<form method="POST">
{{ form.hidden_tag() }}
{{ form.name.label }} {{ form.name() }}
{{ form.submit() }}
</form>
注意,除了 name submit 字段,这个表单还有个 form.hidden_tag() 元素。这个元素生成
一个隐藏的字段,供
Flask-WTF
CSRF
防护机制使用。
当然,这种方式渲染出的表单还很简陋。调用字段时传入的任何关键字参数都将转换成字
段的
HTML
属性。例如,可以为字段指定 id class 属性,然后为其定义
CSS
样式:
<form method="POST">
{{ form.hidden_tag() }}
{{ form.name.label }} {{ form.name(id='my-text-field') }}
{{ form.submit() }}
</form>
即便能指定
HTML
属性,但按照这种方式渲染及美化表单的工作量还是很大,所以在条件
允许的情况下,最好使用
Bootstrap
的表单样式。
Flask-Bootstrap
扩展提供了一个高层级的
辅助函数,可以使用
Bootstrap
预定义的表单样式渲染整个 ...
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