0Pricing
Flask Academy · Lesson

Define a FlaskForm Class

Declare fields and validators in Python.

Define a FlaskForm Class is a free Flask Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Flask-WTF

Handling raw form data by hand gets messy fast. Flask-WTF lets you declare a form once as a Python class and reuse it everywhere. 🎯

Install the Extension

Flask-WTF wraps the WTForms library and adds CSRF protection. Install it with pip before you write a single field.

pip install Flask-WTF

Subclass FlaskForm

Every form you build is a class that inherits from FlaskForm. That base class gives you CSRF and validation for free.

from flask_wtf import FlaskForm

class LoginForm(FlaskForm):
    pass

Add Your First Field

Fields are class attributes set to field objects. A StringField renders as a text input and stores whatever the user types.

from wtforms import StringField

class LoginForm(FlaskForm):
    username = StringField('Username')

Common Field Types

WTForms ships many field types. Reach for PasswordField, BooleanField, or SelectField so each input matches the data you expect.

from wtforms import PasswordField, BooleanField

password = PasswordField('Password')
remember = BooleanField('Remember me')

Give Fields a Label

The first argument to a field is its label. That text is what your template shows next to the input, so make it human-friendly.

email = StringField('Email address')

Attach Validators

Pass a validators list to enforce rules. DataRequired refuses an empty submission before your view ever runs.

from wtforms.validators import DataRequired

username = StringField('Username', validators=[DataRequired()])

Stack Multiple Rules

Validators run in order, so you can stack them. Require a value, then check its length, all in one field.

from wtforms.validators import DataRequired, Length

name = StringField('Name', validators=[DataRequired(), Length(min=2, max=40)])

Add a Submit Button

A SubmitField becomes the form button. Including it in the class keeps the whole form definition in one tidy place.

from wtforms import SubmitField

submit = SubmitField('Log in')

A Complete Form Class

Put the pieces together and you have a reusable, self-validating form class ready to instantiate in any view.

class LoginForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])
    submit = SubmitField('Log in')

Instantiate in a View

Inside a view you simply create an instance. The form object then carries the fields, data, and errors for that request.

@app.route('/login')
def login():
    form = LoginForm()
    return render_template('login.html', form=form)

Quick Check

Which base class should your Flask-WTF forms inherit from?

Recap

You defined a form as a class: subclass FlaskForm, add typed fields with labels and validators, and finish with a submit button. Reusable and clean. ✨

Frequently asked questions

Is the “Define a FlaskForm Class” lesson free?

Yes — the full text of “Define a FlaskForm Class” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Define a FlaskForm Class”?

Declare fields and validators in Python. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Define a FlaskForm Class” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Define a FlaskForm Class
  2. Render and Submit a Form
  3. validate_on_submit and CSRF Tokens
  4. Custom Validators and Field Errors
← Back to Flask Academy