Open In App

Flask login without database – Python

Last Updated : 17 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will talk about Python-based Flask login without a database in this article. In order to use Flask login without a database in this method basically we are using a python dictionary through which users will be able to log in using their login credentials that we add to the database and also the functionality of logout will be added in this login system in profile page through which user can log out. before that we will understand some important terminology:

  • render_template: In the render_template module we are using for calling one HTML file from the templates folder it is used for displaying the login page first on the display screen for import we are using the following command in the app.py file
  • url_for:  In the url_for module we are using for connecting two HTML files with each other here we are using this module for connecting login to the profile page for import we are using the following command in the app.py file 

Flask Login without Database 

Templates File

First, we make a templates folder for HTML files, In the templates folder, we are making two HTML files one for login and another for the welcome profile page .so first we make login.html for making the login page 

login.html

We are writing code for the login page in which we first write the welcome login page message, add the details message, fill out the username and password inputs, call the info method to call the flashing error/warning message, and make one action and one method for the login system before connecting this file to the HTML file. If we enter the proper username and password, the login page will transfer us to the user profile page; if we enter the wrong information, the login page will display an invalid username or password warning. To submit this we are adding one submit button.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login Page</title>
</head>
<style>
  .gfg{
    color: green;
 
  }
  .strong{
    color: red;
  }
</style>
<body>
   
 <h2 class="gfg" >Welcome To GFG Login ????</h2>
 <h3 class="strong" >{{info}} </h3>
<form action="/form_login" method="post">
  <h2></h2><strong>Username:</strong> <br>
  <input type="text" name='username'><br>
  <strong>Password:</strong><br>
  <input  name='password' ><br><br>
<input  type="submit" value="Login">
</form>
 
 
</body>
</html>


Output:

Login  Page

home.html

When we successfully log in using the correct username and password, one welcome user (username) message will appear on the display screen in this HTML file, and we are also writing for one logout button utilizing which we can redirect to the login page. When a user clicks one of our buttons, we add the URL for the (‘login’) tag, which calls the login function and directs them to the login page.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<style>
    .str{
        color: Red;
        font-weight: bold;
    }
    .btn{
        font-size: large;
        color: blue;
    }
</style>
<body >
<br>
<h1>Welcome  <strong class="str" >{{name}} ???? !</strong></h1>
<a  class="btn" href="{{url_for('hello_world')}}">Logout</a>
 
</body>
</html>


Output:

user Profile Page 

app.py

We are importing several libraries into the app.py file that are required for the login system. We import the render template to call the HTML file, and after initializing the flask function, we create the hello world function that enables us to call the login.html file. We create a login function in which we make a request in Python and call the username and password from the dictionary, and we create an if and else condition in which we are writing some flashing message that will show the wrong password and wrong username. We use a dictionary to add the username and password details of users for login, and we can add and remove users as well as change the password of any user at any time.

Python3




# import all libraries
from flask import Flask, request, render_template
 
# initialize flask function
app = Flask(__name__)
 
 
# Make hello_world function
@app.route('/')
def hello_world():
    return render_template("login.html")
 
 # add database like login credentials,
# username and password
database = {'GeeksForGeeks': '123',
            'Abdul Kalam': 'xyz',
            'Jony': 'abc', 'Tony': 'pqr'}
 
# Make another function for login and we are
# making if and else condition for some
# situation like during wrong password wrong
# user and also for successful login
 
@app.route('/form_login', methods=['POST', 'GET'])
def login():
    name1 = request.form['username']
    pwd = request.form['password']
    if name1 not in database:
        return render_template('login.html',
                               info='Invalid User ????!')
    else:
        if database[name1] != pwd:
            return render_template('login.html',
                                   info='Invalid Password ????!')
        else:
            return render_template('home.html',
                                   name=name1)
 
 
 # Run flask in debug mode
if __name__ == '__main__':
    app.run()


After writing this code we run it in your terminal following the command:

flask run

Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads