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 __init__.py 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.Required(), 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.