ProjectsPlaza

Php Projects, Django Projects, Laravel Projects, Codeigniter Projects

Skip to content
  • My account
  • Cart
  • Forums
  • Sell
  • LICENSES

How to login logout in flask

Hello World, Welcome to my website. Today I am going to discuss how to login logout in the flask. This is a very simple tutorial. I have divided this tutorial into the following parts.

  • Install Flask
  • Create view templates to showing the login form
  • Create login and logout functionality with session
  • Run the application


Install Flask

Please read my previous tutorial to install flask and create a hello world app with flask.


Create view templates to showing the login form

In this step, we will create the following view templates.

login_home.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>How to login logout with flask</title>
</head>
<body>
    <h1>Login Logout with flask</h1>
    {% if login %}
    You are logged in as <strong>{{ session['username'] }}</strong> <br />
    <a href={{ url_for('logout') }}>logout</a>
    {%else%}
    Please login here {{ session['username'] }} <br />
    <a href={{ url_for('login') }}>login</a>
    {%endif %}
</body>
</html>

In this template, we will show login status and if user logout or login then redirect to this page.

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>How to login logout with flask</title>
</head>
<body>
    <h1>Login Logout with flask</h1>
    <form method="POST">
        Username <input type="text" name="username" />
        <input type="submit" />
    </form>
</body>
</html>

In this template, we will create a login form.

All the view templates will be placed in templates directory.


Create login and logout functionality with session

In this step, we will perform login, logout and redirect functionality. Create login.py and add the following code in it.

from flask import Flask,session,render_template,request,redirect,url_for

app=Flask(__name__)

app.secret_key='asdsdfsdfs13sdf_df%&'

@app.route('/login',methods=['GET','POST'])
def login():
    if request.method=='POST':
        session['username']=request.form['username']
        return redirect(url_for('index'))
    return render_template('login.html')

@app.route('/logout')
def logout():
    session.pop('username',None)
    return redirect(url_for('index'))

@app.route('/')
def index():
    login=False
    if 'username' in session:
        login=True
    return render_template('login_home.html',login=login)

if __name__=='__main__':
    app.run(debug=True)

Run the application

  • Open the command prompt.
  • Run the following command and open the given url in browser.
python login.py

When you open this url in the browser, you will see the following screens.


how-to-login-logout-with-flask


I hope you are enjoying my tutorial. Please give your feedback in comment section. Thank you 🙂 🙂

This entry was posted in Flask, Programming, Python, Python Web Framework, Web Development and tagged flask framework, flask tutorials, how to implement login logout in flask, learn flask from scratch, learning flask, login logout in flask, python flask learning, python web development on September 20, 2019 by Projects Plaza.

Post navigation

← How to create view templates in flask How to use database migration in laravel →

More posts from this category

  • Create hello world app with fl...
  • Submit Form in Flask
  • Submit form and save data in d...
  • Implement pagination with code...
  • Update delete data from databa...
  • Upload image with codeigniter
  • Learn laravel with example
  • Create Hello World app with la...
  • Create todo app with laravel
  • Create Todo app with laravel &...
  • Upload Image in Laravel 5
  • Generate Dummy data in laravel...
  • Form validation in laravel
  • Add data in database in larave...
  • Create project from scratch wi...
  • Setup laravel 5 on local syste...
  • Create custom admin login with...
  • Create Read Update and delete...
  • Create a hello world app with...
  • Create a single page applicati...

Post Categories

  • Ajax
  • AngularJs
  • APIs
  • Codeigniter
  • Codeigniter Tutorial Series
  • Django
  • Django 3
  • Django Database
  • Django Frontend
  • Express
  • Flask
  • Handlebars
  • Jade
  • Javascript
  • Javascript Frameworks
  • Jquery
  • Laravel
  • Laravel 5
  • Laravel 7
  • Learn python with example
  • Matplotlib
  • Mobile Development
  • MongoDB
  • Mongoose
  • Nodejs
  • npm
  • Php
  • Php Framework
  • Programming
  • Python
  • Python Exercie
  • Python Web Framework
  • React Native
  • ReactJs
  • Uncategorized
  • Web Development
  • WordPress

Post Tags

  • codeigniter 3
  • codeigniter 3 from scratch
  • codeigniter 3 tutorials
  • Codeigniter Example Code
  • Codeigniter Tutorial
  • codeigniter tutorials
  • create project from scratch with django 2
  • django learning blog
  • django tutorials
  • how to create blog app with django
  • laravel 5 tutorials
  • laravel database
  • laravel practise example
  • laravel scripts
  • laravel turotials
  • Laravel tutorial
  • laravel tutorial blog
  • laravel tutorials
  • Laravel tutorial series
  • Learn Codeigniter
  • Learn Codeigniter From scratch
  • Learn Codeigniter with example
  • learn django
  • learn django 2
  • learn django framework
  • learn django from scratch
  • learn django with example
  • learn django with example code
  • learning codeigniter from scratch
  • learning codeigniterphp framework learning
  • learning laravel
  • learning php framework
  • learning php frameworks
  • learning python
  • Learn laravel
  • learn laravel 5
  • Learn laravel from scratch
  • Learn laravel with example
  • learn php
  • learn python 3
  • learn python with example
  • php framework tutorials
  • php scripts
  • projectsplaza
  • web development
Proudly powered by WordPress
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT
  • USD $
    USD dollar
  • INR ₹
    Indian Rupee
  • Projects
    • Ideas
    • Django
    • Laravel
    • Codeigniter
    • Core php
    • Bootstrap 4
  • Learn Php
    • Php
    • Laravel
    • Codeigniter
  • Learn Python
    • Python
    • Django
    • Flask
  • Javascript
    • Jquery
    • ReactJs
    • Ajax
    • AngularJs
    • React Native
  • Nodejs
    • Express
    • Jade
    • Handlebars
    • MongoDB