liveBook · Manning

Flask-WTF

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

Flask-WTF is a Flask extension that integrates Flask applications with WTForms for form construction and validation.

Flask-WTF connects Flask applications with WTForms, turning HTML form handling and validation into a structured part of the application. It is especially useful when login forms must reject missing, malformed, or incorrectly sized input without scattering validation logic through request handlers. section 8.2.2

A Flask-WTF form class centralizes several responsibilities:

  • Declares HTML elements such as email, password, Boolean, and submit fields. section 8.2.2
  • Associates validators such as DataRequired, Length, Email, and EqualTo with individual fields. section 8.2.2
  • Provides labels, HTML names and IDs, optional rendering attributes, and validation errors for templates. section 8.2.2
  • Binds server-side form objects to templates and POST-based request handling. section 8.2.2

A validated login form

Define a LoginForm with an EmailField requiring 4–128 characters and a valid email format, plus a PasswordField. Instantiate it in the login handler, pass it to the template, render its fields and CSRF token, and display any validation errors. After validation, the handler can query the user and verify the submitted password. section 8.2.2

from flask_wtf import FlaskForm                         #1
from wtforms import PasswordField,                      #2
BooleanField, SubmitField                             #2
from wtforms.fields.html5 import EmailField             #2
from wtforms.validators import DataRequired,            #3
Length, Email, EqualTo                                #3
 
class LoginForm(FlaskForm):                             #4
    email = EmailField(                                 #5
        "Email",                                        #5
        validators=[DataRequired(), Length(             #5
            min=4,                                      #5
            max=128,                                    #5
            message="Email must be between 4            #5
            and 128 characters long"                  #5
        ), Email()],                                    #5
        render_kw={"placeholder": " "}                  #5
    )                                                   #5
    password = PasswordField(                           #6
        "Password",                                     #6
        validators=[DataRequired(), Length(             #6
            min=3,                                      #6
            max=64,                                     #6
            message="Password must be between 3         #6
            and 64 characters long"                   #6
        )],                                             #6
        render_kw={"placeholder": “ “}                  #6
    )                                                   #6
    remember_me = BooleanField(" Keep me logged in")    #7
cancel = SubmitField(                                   #8
    label="Cancel",                                     #8
    render_kw={"formnovalidate": True},                 #8
    )                                                   #8
    submit = SubmitField("Log In")                      #9

Inspect the complete LoginForm declaration, including FlaskForm inheritance, field types, validators, and rendering options. section 8.2.2

{% extends "base.html" %}                                                      #1
{% import "macros.jinja" as macros %}                                          #2
 
{% block content %}
<div class="login-wrapper mx-auto mt-3">
    <div class="container login">
        <form method="POST" novalidate>
            <div class="card">
                <h5 class="card-header">
                     User Login
                </h5>
                <div class="card-body">
                    <div class="card-text">
                        {{form.csrf_token}}                                    #3
                        <div                                                   #4
                        class="form-floating mb-3">                          #4
                            {{form.email(                                      #4
                            class_="form-control")}}                         #4
                            {{form.email.label(                                #4
                            class_="form-label")}}                           #4
                            {{macros.                                          #4
                            validation_errors(form.email.errors)}}           #4
                        </div>                                                 #4
                        <div class="mb-3">                                     #5
                            {{form.password(                                   #5
                            class_="form-control")}}                         #5
                            {{form.password.label(                             #5
                            class_="form-label")}}                           #5
                            {{macros.validation_errors(form.password.errors)}} #5
                        </div>                                                 #5
                        <div class="mb-3">                                     #6
                            {{form.remember_me}}                               #6
                            {{form.remember_me.label(                          #6
                            class_="form-check-label")}}                     #6
                        </div>                                                 #6
                    </div>
                </div>
             <div class="card-footer text-end">
                 {{form.cancel(
                 class_="btn btn-warning me-2")}}                            #7
                 {{form.submit(
                 class_="btn btn-primary")}}                                 #8
                </div>
            </div>
    </form>
    </div>
</div>
{% endblock %}
 
{% block styles %}
    {{ super() }}
    <link rel="stylesheet" type="text/css" 
    href="{{ url_for('.static', 
    filename='css/login.css') }}" />
{% endblock %}

Inspect the Jinja template that posts the form, includes the CSRF token, and renders the fields. section 8.2.2

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains field declarations, validators, rendering, templates, and POST handling, but defers stronger security such as two-factor authentication.
Examples
Uses a MyBlog login form with email and password fields, validation messages, a CSRF token, and a generated login screen.
What it assumes
Assumes Flask routes, HTML forms, templates, and HTTP POST requests while teaching field validation and form binding.
What you can do after
Build it from scratch by defining validated FlaskForm classes, rendering them in Jinja, and processing submitted values in handlers.
How it connects
Connects Flask applications with WTForms, Jinja templates, HTTP POST handling, authentication, and password verification.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.