Flask Session

  • __init__.py
from flask_session import Session, SqlAlchemySessionInterface


app = Flask(__name__)
# Configuration
app.config.from_object('config')
app.secret_key = 'ae25be573f1b6e19a066fe920685137cb5ea4f76e5924173'

# Session
Session(app)
app.session_interface = SqlAlchemySessionInterface(app, db, 'session', '')

Configuration

config.py

SESSION_TYPE = 'sqlalchemy'

The SqlAlchemySessionInterface will create a table to store session info:

Flask session

This is implemented on top of cookies for you and signs the cookies cryptographically. What this means is that the user could look at the contents of your cookie but not modify it, unless they know the secret key used for signing.

from flask import session

# set session key
session['username'] = form.username.data

# get session key
session.get('username')

To get session_id, use request.cookies.get()

session_id = request.cookies.get('session')

results matching ""

    No results matching ""