Flask Tutorial
About Lesson

In this Flask Tutorial we are going to learn how to create Flask Forms with Flask-WTF, Flask-WTF is simple integration of Flask and WTForms, including CSRF, file upload, and reCAPTCHA. 

 

Features :

  • Integration with WTForms.
  • Secure Form with CSRF token.
  • Global CSRF protection.
  • reCAPTCHA support.
  • File upload that works with Flask-Uploads.
  • Internationalization using Flask-Babel.

 

 

Installation

First of all you need to install Flask-WTF, you can easily install Flask-WTF using pip command.

 

 

First of all you need to create a new python file at name of form.py, basically in this file we are going to create our form. also you can see that we have used validators for our input fields. a validator simply takes an input, verifies it fulfills some criterion, such as a maximum length for a string and returns. Or, if the validation fails, raises a ValidationError this system is very simple and flexible, and allows you to chain any number of validators on fields.

 

 

Now let’s add a button in our NavBar for login, so open your base.html and add this code before closing the navbar.

 

 

First we need to import our  LoginForm from form.py file.

 

 

When you are working with forms, than you need to add secret key, it will protect you from CSRF attacks.

 

 

Now let’s open our app.py file, because we want to add a login route, in this route first we create the object of our LoginForm and after that we send that to the login.html file that we will create, also we have added our methods in the Login route.

 

 

Let’s create our login.html file in our templates folder, in here we are rendering the fields from the login that we are receiving via context variable.

 

 

Rest of the files.

 

app.py 

 

 

templates/base.html

 

 

templates/index.html

 

 

templates/contact.html

 

 

templates/about.html

 

 

templates/500.html

 

 

templates/404.html

 

 

 

Run your project click on the login button, this will be the result.

Flask Tutorial - Flask Forms with Flask-WTF
Flask Tutorial – Flask Forms with Flask-WTF