Sobes.tech
Junior — Middle

Can you explain what is meant by a session in the context of the framework you use?

sobes.tech AI

Answer from AI

In the context of Python web frameworks (such as Django, Flask), a session is a mechanism for storing user state data between HTTP requests, since HTTP itself is stateless.

A session allows storing information (for example, user ID, settings, shopping cart) on the server or encrypted in a cookie, so that subsequent requests can identify the user and restore their state.

Example in Flask:

from flask import Flask, session

app = Flask(__name__)
app.secret_key = 'secret_key'

@app.route('/login')
def login():
    session['user_id'] = 42  # store in session
    return 'Logged in'

@app.route('/profile')
def profile():
    user_id = session.get('user_id')
    if user_id:
        return f'User profile {user_id}'
    return 'Please log in'

Thus, a session is a way to save and retrieve user data between requests.