Build User Management System with Flask

Muhammad Fahim
4 min readSep 9, 2021

--

In this tutorial, we are going to build a user management system using the Python web framework Flask.
We will be using SQL Alchemy which is an open-source SQL toolkit and object-relational mapper for Python …

and also we will be using Flask-WTForms which can help us to create and use web forms with simple Python models, turning tedious and boring form validation into a breeze.

after that, we will use Flask-Login which will provide us the user session management. It will handle the common tasks of logging in, logging out, and remembering our users’ sessions over extended periods of time.

And for security purposes, we will be hashing passwords using werzeug library which is a python library that contains a lot of development and debugging tools for the implementation of web application gateway interface(WSGI) applications.

First, we should learn …

*What is Flask?*
Flask is a micro web framework written in Python that allows us to build up web applications. It was developed by Armin Ronacher. Flask’s framework is more explicit than Django’s framework and is also easier to learn because it has less base code to implement a simple web-Application. A Web-Application Framework or Web Framework is a collection of modules and libraries that helps the developer to write applications without writing low-level codes such as protocols, thread management, etc. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine.

Now do something practical coding…

First, let's start with importing libraries that we will be using in this project.

After that, we need to do some configuration.

First, we make a variable in which pass the flask function to the app, and then another variable we are passing the secret key which we imported from secrets library and it has 16 cryptographically strong random numbers.

Then we tried to turn off the warning system of SQL ALCHEMY by explicitly turning it off(you can find more information on this topic link)

After we link our app with the SQL Alchemy database and also give the path of our database file location.

After that, we are going to the Flask login library. First, we created a User class in which we gave a unique id to every user which also will be our primary key. Then we saved the username, email, and password variables in the database.

Then we create another Login form class in which we are trying to check the user whether he has inputted the username or password or not.

In the registration class, we are making sure that while the user trying to create an account whether he/she has inputted the required values in the correct form and length.

Next, we tried to make a route for our application and also declare its functions.

The first function we created is Login which is inheriting from our Login Form class and with IF condition we are checking whether the user entered the correct password and comparing it to our stored hashed password and in case of if the user has entered the wrong password then we are displaying the message of an invalid username or password.

In the next route of registration, we are inheriting from our registration class and also hashing the password with the library Werkzeug and we are using the SHA256 algorithm as a method and passing it our New_use variable.

And then we are passing it to SQL Alchemy database sessions for creating and storing new user data in the database.

The session basically establishes all conversations with the database. Session object’s current database connection, populating result rows into objects that are then stored in the session(you can read more about the session on this link).

In the next route and its function, we are just returning the default dashboard on successfully logging it.

after logging out the user we are just returning it to the index page.

Conclusion: In this tutorial we learned how to user management system. This system not Register new users but also authenticate the username and password. We also learned how to hash passwords and store in our database .

And there we have it. I hope you have found this useful. Thank you for reading.

You can also visit this link to see the entire project code on Github.

Contact me on LinkedIn account. Thank you

--

--

Muhammad Fahim

Junior Data engineer @M3hive. Passionate about Data Engineering and Web Scraping & Automation. Sharing insights