Book Image

Building Web Applications with Flask

By : Italo M Campelo Maia, Jack Stouffer, Gareth Dwyer, Italo Maia
Book Image

Building Web Applications with Flask

By: Italo M Campelo Maia, Jack Stouffer, Gareth Dwyer, Italo Maia

Overview of this book

Table of Contents (17 chapters)
Building Web Applications with Flask
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

WTForms and you


WTForms (https://github.com/wtforms/wtforms) is a standalone robust form handling library that allows you to generate HTML forms from form-like classes, implement fields and form validation, and include cross-source forgery protection (a nasty vulnerability that crackers may try to exploit in your Web applications). We certainly don't want that!

First, to install WTForms library, use the following:

pip install wtforms

Now let's write some forms. A WTForms form is a class that extends the Form class. As plain as that! Let's create a login form that could be used with our previous login example:

from wtforms import Form, StringField, PasswordField
class LoginForm(Form):
    username = StringField(u'Username:')
    passwd = PasswordField(u'Password:')

In the preceding code, we have a form with two fields, username and passwd, with no validation. It is just enough to build a form in a template, like this:

<form method='post'>
{% for field in form %}
    {{ field.label }}
 ...