User Registration Form Flask Tutorial

In this tutorial, we're going to be talking about the creation of our registration form. While not required, Flask has a built in forms module called WTForms. To get this, run:

pip install flask-wtf

Now, since we're using this module to create the form, what we will wind up doing is creating a form class, which will give us a registration form object, which we then will pass on to our template. It sounds confusing possibly, but it's not so bad!

Within our file, we will add the following class. It is my suggestion that for organization, as well as functionality, that you put all of your classes at the very top of your script if they are used in many places, or you can put them right above the function that they will be used in.

class RegistrationForm(Form):
    username = TextField('Username', [validators.Length(min=4, max=20)])
    email = TextField('Email Address', [validators.Length(min=6, max=50)])
    password = PasswordField('New Password', [
        validators.EqualTo('confirm', message='Passwords must match')
    confirm = PasswordField('Repeat Password')
    accept_tos = BooleanField('I accept the Terms of Service and Privacy Notice (updated Jan 22, 2015)', [validators.Required()])


Form class, containing username, email, password, and confirm password fields. Then we have a check box for the "accept terms" requirement.

Take note about the "validators" parameters here, where we can specify all sorts of things, but we require various length rules, as well as requiring that both passwords match.

Also note the "boolean" field, meaning it can be either on or off, or a 1 or 0. Notice that you also need to have a validator here if you want this box to be a required check box. If you were running a restaurant application here, and people were just picking their ingredients for a sandwich, then you wouldn't require a check for all ingredients. Here, we want to be certain that the users have agreed to our terms.

The next tutorial:

  • Introduction to Practical Flask
  • Basic Flask Website tutorial
  • Flask with Bootstrap and Jinja Templating
  • Starting our Website home page with Flask Tutorial
  • Improving the Home Page Flask Tutorial
  • Finishing the Home Page Flask Tutorial
  • Dynamic User Dashboard Flask Tutorial
  • Content Management Beginnings Flask Tutorial
  • Error Handling with Flask Tutorial
  • Flask Flash function Tutorial
  • Users with Flask intro Tutorial
  • Handling POST and GET Requests with Flask Tutorial
  • Creating MySQL database and table Flask Tutorial
  • Connecting to MySQL database with MySQLdb Flask Tutorial
  • User Registration Form Flask Tutorial
  • Flask Registration Code Tutorial
  • Finishing User Registration Flask Tutorial
  • Password Hashing with Flask Tutorial
  • Flask User Login System Tutorial
  • Decorators - Login_Required pages Flask Tutorial
  • Dynamic user-based content Flask Tutorial
  • More on Content Management Flask Tutorial
  • Flask CMS Concluded Flask Tutorial
  • The Crontab Flask Tutorial
  • Flask SEO Tutorial
  • Flask Includes Tutorial
  • Jinja Templating Tutorial
  • Flask URL Converters Tutorial
  • Flask-Mail Tutorial for email with Flask
  • Return Files with Flask send_file Tutorial
  • Protected Directories with Flask Tutorial
  • jQuery with Flask Tutorial
  • Pygal SVG graphs with Flask Tutorial
  • PayPal with Flask Web Development Tutorial
  • Securing your Flask website with SSL for HTTPS using Lets Encrypt