Web Technologies/2021-2022/Laboratory 13

Form validation using WTForms
Validating form is going to be one of the most essential steps when building web applications.

Today we will see an approach of using WTFormsand Flask-login in order to implement a register/login mechanism. We will start off with creating a simple User model: Then we will create two classes for our Registration and Login forms. WTForms lets us define fields such as StringField, PasswordField together with validators such as Length, Email, DataRequired: We will have to implement routes for login/register: We will write a Jinja2 macrofor our views. This macro will help us render each individual field of a form, containing our labels and displaying errors if any. So for example this is what our register.html template will look like with the help of render_field macro: Respectively, this will be our login.html form: The following imports and additional configurations steps needed in main.py: We will have to configure our login manager: In the end, you should have a directory structure similar to: