Flask WTF Registration Form with SQLAlchemy

In this Flask tutorial we learn about Flask WTF Registration Form with SQLAlchemy,

in previous tutorial we have learned that how you can work with Flask SQLAlchemy

but we have used terminal for adding data, now we are going to learn that how you can use

Flask SQLAlchemy practically.

 

 

 

For this tutorial you need to download and install Wamp Server. create a database in the

Wamp Server, iam going to give flaskcodeloop for the database name, but you can give the

name according to your choice. right now we don’t have any table in our database. 

 

 

 

 

In here we have created our forms.py, the first one is for login that we have already talked about

this, you can check this article, Flask WTF Forms. and the second one is registration form.

 

 

 

 

 

 

This is our app.py and we have added our SQLAlchmey and database configuration.

 

 

 

 

 

Note : You need to use this command for creating your model table in the database.

 

 

 

 

 

If you check your flaskcodeloop database, you can see that we have our table

with the data.

Flask Mysql Database
Flask Mysql Database

 

 

 

 

 

 

In the above code this is SQLAlchemy configuration for Mysql Database.

 

 

 

 

 

And this is our database model class, it is just a simple model with three fields, id, username

and password.

 

 

 

 

 

In the app.py file this is our registration route. you can see that we have hashed our password,

because we shall not use plain text password, by this reason you need to hash your password

with werkzeug.security library. also we have used Flash Message after successful registration.

 

 

 

 

 

Our base.html file, also we have added two buttons for the login and signup.

 

templates/base.html

 

 

 

 

 

This is our registration.html, and we have simply rendered our form with our Flash Message.

 

templates/registration.html

 

 

 

 

 

In here we have login.html file.

 

templates/login.html

 

 

 

 

 

 

This is our index.html.

 

templates/index.html

 

 

 

 

 

 

Now run your Flask Project and go to http://localhost:5000/register, you can see a 

registration form, add the data and click on Register button, this will be the result.

Flask WTF Registration Form with SQLAlchemy
Flask WTF Registration Form with SQLAlchemy

 

 

 

 

 

And if you check your database, you can see that we have our data with

hashed password.

Flask Mysql
Flask Mysql

 

 

 

 

 

 

 

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
×