Open In App

How to use Flask-Session in Python Flask ?

Flask Session – 

Uses of Session

This article assumes you are familiar with flask basics. Checkout – Flask – (Creating first simple application) to learn how to make a simple web application in flask.

Installation

Install the extension with the following command



$ easy_install Flask-Session

Alternatively, if you have pip installed

$ pip install Flask-Session

Configuring  Session  in  Flask




from flask import Flask, render_template, redirect, request, session
from flask_session import Session

This is specific to the flask_session library only






app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

Remember User After Login

So we will start making two basic pages and their route called index.html and login.html




@app.route("/")
def index():
    return render_template('index.html')
 
 
@app.route("/login", methods=["POST", "GET"])
def login():
    return render_template("login.html")




@app.route("/login", methods=["POST", "GET"])
def login():
  # if form is submited
    if request.method == "POST":
        # record the user name
        session["name"] = request.form.get("name")
        # redirect to the main page
        return redirect("/")
    return render_template("login.html")




@app.route("/")
def index():
  # check if the users exist or not
    if not session.get("name"):
        # if not there in the session then redirect to the login page
        return redirect("/login")
    return render_template('index.html')




@app.route("/logout")
def logout():
    session["name"] = None
    return redirect("/")

Complete Project –




from flask import Flask, render_template, redirect, request, session
# The Session instance is not used for direct access, you should always use flask.session
from flask_session import Session
 
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
 
 
@app.route("/")
def index():
    if not session.get("name"):
        return redirect("/login")
    return render_template('index.html')
 
 
@app.route("/login", methods=["POST", "GET"])
def login():
    if request.method == "POST":
        session["name"] = request.form.get("name")
        return redirect("/")
    return render_template("login.html")
 
 
@app.route("/logout")
def logout():
    session["name"] = None
    return redirect("/")
 
 
if __name__ == "__main__":
    app.run(debug=True)

index.html




{% extends "layout.html" %}
 
{% block y %}
 
   {% if session.name %}
      You are Register {{ session.name }} <a href="/logout">logout</a>.
   {% else %}
      You are not Register. <a href="/login">login</a>.
   {% endif %}
 
{% endblock %}

login.html




{% extends "layout.html" %}
 
{% block y %}
 
   <h1> REGISTER </h1>
 
   <form action="/login" method="POST">
      <input placeholder="Name" autocomplete="off" type="text" name="name">
      <input type="submit" name="Register">
   </form>
 
{% endblock %}

layout.html




<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta name="viewport" content="initial-scale=1, width=device-width">
        <title> flask </title>
    </head>
    <body>
        {% block y %}{% endblock %}
    </body>
</html>

Output – 

login.html

index.html

You can also see your  generated  session.


Article Tags :