🧠 Tutorial 1: Understanding Flask Session
Objective:
Understand what Flask’s session is, how it stores information, and how to show/hide elements (like a navbar change) when someone is “logged in”.
The Flask session object is a temporary, per-user storage dictionary that keeps data (like login status or username) while the user’s browser session is active. It allows your app to personalize pages and control what users see (e.g., showing “Logout” instead of “Login”).
In Flask, the keys in the session object are just like the keys in a Python dictionary — they represent the names of the data items you want to store for the current user.
🗝️ Example from tutorial:
session['user'] = 'Ben'Here,
- Key:
'user' - Value:
'Ben'
So inside the session, Flask is storing something like:
{'user': 'Ben'}🔹 Common keys you might use:
'user'– to store the username or ID'logged_in'– to track whether the user is logged in (True/False)'role'– to store user roles like “admin” or “student”'cart'– to keep items a user added in a shopping cart'theme'– to remember user preferences like light/dark mode
👉 You don’t need to manually pass the session object to your templates.
Get Icodewithben’s stories in your inbox
Join Medium for free to get updates from this writer.
Flask automatically makes session available inside all Jinja templates.
That’s why in the example you write:
{% if 'user' in session %}
<span>Welcome, {{ session['user'] }}!</span>
{% endif %}💡 Summary:
The keys in the Flask
sessionobject are the labels you choose for storing specific pieces of user-related information, such as'user','logged_in', or'role'.
🧱 Step 1: Setup a mini project
Folder structure:
flask_session_demo/
│
├── app.py
└── templates/
├── base.html
└── home.html📄 app.py
from flask import Flask, render_template, session, redirect, url_for
app = Flask(__name__)
app.secret_key = "mysessiondemo" # Needed for session to work
@app.route('/')
def home():
return render_template('home.html')
@app.route('/login')
def login():
# Imagine a user successfully logged in
session['user'] = 'Ben' # ✅ store username in session
return redirect(url_for('home'))
@app.route('/logout')
def logout():
session.pop('user', None) # ✅ remove user from session
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(debug=True)🧱 base.html
<!DOCTYPE html>
<html>
<head>
<title>Session Demo</title>
</head>
<body>
<nav>
{% if 'user' in session %}
<span>Welcome, {{ session['user'] }}!</span> |
<a href="{{ url_for('logout') }}">Logout</a>
{% else %}
<a href="{{ url_for('login') }}">Login</a>
{% endif %}
</nav>
<hr>
{% block content %}{% endblock %}
</body>
</html>🧱 home.html
{% extends "base.html" %}
{% block content %}
<h2>Flask Session Example</h2>
<p>This page shows how Flask remembers who is logged in using a session.</p>
{% if 'user' in session %}
<p>You are logged in as <b>{{ session['user'] }}</b>.</p>
{% else %}
<p>You are not logged in yet. Click "Login".</p>
{% endif %}
{% endblock %}💬 What you have learned
sessionis a dictionary that remembers info per user.- It lasts while the browser is open (or until you log out).
- It’s perfect for showing a different navbar or storing who is logged in.
🧩 Stretch Task
Add a /profile route that only shows a message if 'user' in session, else redirects home.






