Flask Tutorial – Flask-WTF Form Error Messages

In this Flask Tutorial we are going to learn about Flask-WTF Form Error Messages, so in the

previous article we have learned that how you can use Flask-WTF Form, but we have not done 

validation on our Forms. this article is basically for the form validation on Flask-WTF Forms.

you can Check the article on this link Flask Forms with Flask-WTF

 

 

 

 

So this is our forms.py file, and in this file we have created our simple login form and you

can see that we have added some ready validators from Flask-WTF that is InputRequired(). 

For more information about Flask-WTF you can read this article Flask Forms with Flask-WTF.

 

 

 

 

And this is our app.py file with our required view functions and you can see that we have also

added our login view function in here. in this view function just by submitting of the form

we are going to redirect the users to the index using redirect function.

 

 

 

 

 

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. 

 

 

 

 

 

So now this is our login.html file , basically in here we need to put our Flask-WTF validation

errors. and you can use this code for adding validation errors. this is for username, you can add

the same for password, just change the username to the password.

 

 

 

 

This is the complete login.html file.

 

 

templates/login.html

 

 

 

 

 

And this is our index.html file.

 

templates/index.html

 

 

 

 

Now run the project and go to http://localhost:5000/login, if you leave blank the fields and

click on login, you will see this error message.

Flask Tutorial - Flask-WTF Form Error Messages
Flask Tutorial – Flask-WTF Form Error Messages

 

 

 

 

And if you give a random password and email you will redirected to the index page. right

now we have not used any database functionality for our application, we will do this in the

next tutorials.

Flask WTF Error Messages
Flask WTF Error Messages

 

 

 

 

 

 

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

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×