Book Image

Learning Flask Framework

Book Image

Learning Flask Framework

Overview of this book

Table of Contents (17 chapters)
Learning Flask Framework
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Login and logout views


Users will log into our blogging site using their email and password; so, before we begin building our actual login view, let's start with the LoginForm. This form will accept the username, password, and will also present a checkbox to indicate whether the site should remember me. Create a forms.py module in the app directory and add the following code:

import wtforms
from wtforms import validators
from models import User

class LoginForm(wtforms.Form):
    email = wtforms.StringField("Email",
        validators=[validators.DataRequired()])
    password = wtforms.PasswordField("Password",
        validators=[validators.DataRequired()])
    remember_me = wtforms.BooleanField("Remember me?",
        default=True)

Tip

Note that WTForms also provides an e-mail validator. However, as the documentation for this validator tells us, it is very primitive and may not capture all edge cases as full e-mail validation is actually extremely difficult.

In order to validate the user's...