Flask Tutorial – Flask Forms with Flask-WTF

In this Flask Tutorial we are going to learn about creating Flask Forms with Flask-WTF

so Flask-WTF is simple integration of Flask and WTForms, including CSRF, file upload, and

reCAPTCHA. and this is our eight article on Flask Web Development.

 

 

 

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.

 

 

If you are interested in Web Development with Django Framework, than you can read the

complete articles in this link,  Django Web Development Tutorials.

 

 

 

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 forms.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.

 

 

 

 

 

OK now we need to add a new view function in our app.py file at name of Login, basically we are going to just create the object of our form in here and we render our form. also you can see we have validated our request for the view handler.

 

 

 

 

 

This is our complete app.py file. also when you are working with the forms you need to configure the secret key at the top.

 

 

 

 

 

 This is our base.html, we have already talked about creating templates in Flask, you can read this article Introduction to Flask Templates. you need to just create templates folder in your working directory. 

 

 

 

templates/base.html

 

 

 

 

In our base.html file we have already added a button for login. and you can see that we have 

linked our button to the Login view function that we have already created in our app.py file. 

 

 

 

 

Now this is our login.html file. we have just simply rendered our form fields in here. you can see that at the top we have added {{form.csrf_token}}, this hidden field is created automatically. Flask-WTF form is already protecting you from CSRF, you don’t have to worry about that. However, you have views that contain no forms, and they still need protection.

 

 

 

So this is the complete file for login.html.

 

 

 

templates/login.html

 

 

 

And this is our index.html file.

 

templates/index.html

 

 

 

 

 

Now if you run the project and click on the login button, you will see your login form.

 

 

 

http://localhost:5000/login

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

 

 

 

 

 

Also you can watch my complete 4 hours training on Flask Web Development

Subscribe and Get Free Video Courses & Articles in your Email

 

Comments are closed.

Share via
Copy link
Powered by Social Snap
×