Login & Registration system with Flask & MySql
This project is on Login & Registration system with Flask & MySql. In this,
I have used Flask, which is a Python web framework built with a small core and easy-to-extend philosophy. It is classified as a
microframework because it does not require particular tools or libraries.
Users should create new registration to enter the home page if already a member of the site then
put login details to go to the dashboard of the home page.
Code on : Login & Registration system with Flask & MySql
Before jumping into a project, in this website I have focused on mostly Backend with flask. Like how Flask works and all, not on the Front end part.
Just how pages are rendered on browser and Login and Registration in the site.
Steps to create this System:
- First, we have to create a folder for our project, eg - FlaskWeb.
- In our FlaskWeb folder we have to create one python file for our Backend purpose, eg- route demo.
- After that, we have to create our front end part, in this, I have used HTML5, CSS3, and JAVASCRIPT.
- In Flask for front-end code we have to store it in a separate folder. For the HTML file, it should be in folder name 'templates' only in that we should create our HTML files
& for CSS file we should create the folder name 'static'.
*You can't give any other name to the templates and static folder.
Front-end part:
In the templates folder, I created 3 files of HTML-
1. home.html:
This is the Dashboard of our main page. After the user will log in or register to our website then only this home page will be rendered or else not.
2. login.html:
In this, if the user is already a member of our website and they put the right login information on the login page then only the user will be logged in to the dashboard of the website.
3. register.html:
Any new user visits our site then after filling the right data in the registration page, the user will be redirected to Dashboard. If the new user is trying to register with the existing details like some other user has registered with the same details then it will display an error that the user already exists.
In the static folder, I created 1 CSS file, style.css
In this project, I have used Bootstrap 4 for the design purpose as I have focused more on the Backend part. in this CSS file, I have given color and all to the website. You can put your CSS instead of using Bootstrap.
Back-end part
For Back-end create python file , eg- routedemo.py
This is a code for routedemo.py:
Things to import in python
First we have to install the required library :
pip install Flask
pip install mysql-connector-python
###############-----------------Import packages--------------------#################################
from flask import Flask, render_template, request, flash, redirect, session
import mysql.connector
import os
from flask import flash
app = Flask(__name__)
###############-----------------sessions--------------------#################################
app = Flask(__name__)
app.secret_key = os.urandom(24)
###############------------------mysql---------------------##############################
conn = mysql.connector.connect(host="localhost", user="root", password="", database="login_validation")
cursor = conn.cursor()
###############-------------------HOME-------------------################################
@app.route('/home')
def home():
if 'user_id' in session:
return render_template('home.html')
else:
return redirect('/')
#############----------------REGISTER--------------------###################
@app.route('/register')
def register():
return render_template('register.html')
###############-----------REGISTRATION--------------###############################
@app.route('/register_validation', methods=['POST'])
def register_validation():
name = request.form.get('namereg')
email = request.form.get('emailreg')
password = request.form.get('passwordreg')
if not len(password) >= 5:
flash('Password must be at least 5 characters in length')
return render_template('register.html')
else:
cursor.execute("INSERT INTO user (id,name,email,password) VALUES (NULL,%s,%s,%s)",
(name, email, password))
conn.commit()
conn.close()
return render_template('home.html')
##########----------------LOGIN-------------------##############################
@app.route('/')
def login():
return render_template('login.html')
############----------------LOGIN_VALIDATION--------------------#######################
@app.route('/login_validation', methods=['POST'])
def login_validation():
email = request.form.get('email')
password = request.form.get('password')
cursor.execute("SELECT * FROM user WHERE email = %s AND password = %s ",
(email, password))
users = cursor.fetchall()
if len(users) > 0:
session['user_id'] = users[0][0]
flash('Logged in Successfully!')
return redirect('/home')
else:
flash('Entered Details already exists or are invalid ')
return redirect('/')
###############------------------LOGOUT---------------#################################
@app.route('/logout')
def logout():
session.pop('user_id')
return redirect('/')
###################------MAIN-----------############################
if __name__ == "__main__":
app.run(debug=True)