Hello friends, In this blog, I will give you a full guide on how to create a login and registration form using Flask and Postgresql.
If you are new to web development then it becomes really very difficult to understand sessions and user authentication. So we will understand the basics of the flask session and how it works.
Follow the following steps to create a login and registration system using Flask and Postgresql:
If you are new to web development then it becomes really very difficult to understand sessions and user authentication. So we will understand the basics of the flask session and how it works.
Follow the following steps to create a login and registration system using Flask and Postgresql:
Step - 1:
First of all, we will create a registration and a login page using HTML and CSS. Here we will use Bootstrap to create a registration and login page.
<div class="container">
<div class="card">
<div class="card-header">
User Registration
</div>
<div class="card-body">
<form action="{{url_for('registration')}}" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">Enter your name</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="name" required>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="email" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
Here we have designed a simple registration page and now we will design a login page.
<div class="container">
<div class="card">
<div class="card-header">
User Registration
</div>
<div class="card-body">
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="email" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
Step - 2:
Now we will set up a database to store the information of the user. After creating a database we will create a function registration so as to store the information of the user to the database.
Look at the code below:
Look at the code below:
from flask import Flask, render_template, redirect, request, session, flash
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
app = Flask(__name__)
engine = create_engine("postgres://manoj:password@localhost/users")
db = scoped_session(sessionmaker(bind = engine))
@app.route('/')
def index():
return render_template('registration.html')
@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
users = db.execute("SELECT * FROM user WHERE email = :email and password = :password",
{"email": email, "password": password}).fetchone()
if users:
session['user'] = email
return redirect('home')
else:
return redirect('login')
@app.route('/registration', methods=['POST', 'GET'])
def registration():
if request.method == 'POST':
name = request.form.get('name')
email = request.form.get('email')
password = request.form.get('password')
#Check if the user is already exist or not
users = db.execute("SELECT * FROM user WHERE email = :email",
{"email": email}).fetchall()
if users:
flash("User already exist.")
return redirect('login')
db.execute("INSERT INTO user (name, email, password) VALUES (:name, :email, :password)",
{"name": name, "email": email, "password": password})
db.commit()
session['user'] = email
return redirect('home')
else:
return redirect('index')
@app.route('/home')
def home():
if 'user' in session:
users = db.execute("SELECT * FROM user WHERE email = :email",
{"email": session['user']})
return render_template('home', users = users)
else:
return redirect('/login')
if __name__ == '__main__':
app.run(debug = True)