Il framework delle sessioni
Memorizzare dati associati a un singolo visitatore
Il framework delle sessioni è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Why Sessions Exist
HTTP forgets you between requests. The session framework lets Django remember each visitor across page loads, so a login or a cart can survive. 🍪
What a Session Stores
A session is a small bag of data tied to one visitor. Django keeps it on the server and gives the browser only a key to find it again.
The Session Cookie
Django sets a cookie named sessionid in the browser. That cookie holds just the session key, never your private data, which stays safe on the server.
It Is Already On
The SessionMiddleware ships enabled in every new project, so sessions work out of the box without any extra setup from you.
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
]The Sessions App
The django.contrib.sessions app lives in INSTALLED_APPS and creates a table to hold session data after you run migrate.
Where Data Lives
By default Django stores sessions in the database. Each row holds the key, the encoded data, and an expiry date for that visitor.
Choosing a Backend
You pick storage with SESSION_ENGINE. Cache or cached_db backends keep sessions in memory for far faster reads on busy sites.
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'Accessing the Session
In any view, the session hangs off the request as request.session. It behaves like a normal Python dictionary you can read and write.
def home(request):
request.session['theme'] = 'dark'
return render(request, 'home.html')Sessions Expire
Sessions do not last forever. SESSION_COOKIE_AGE sets the lifetime in seconds, defaulting to two weeks before a visitor is forgotten.
SESSION_COOKIE_AGE = 1209600 # two weeksClearing Old Sessions
Expired rows linger in the database. Run clearsessions on a schedule to delete stale data and keep the table tidy.
python manage.py clearsessionsAnonymous and Logged In
Sessions track both anonymous and logged-in visitors. That is how a guest can fill a cart before they ever create an account.
Quick Check
Let us check where session data actually lives.
Recap
You met the session framework: it remembers visitors across requests, stores data server-side, and works automatically through SessionMiddleware. Next you will read and write it. ✅
Domande Frequenti
La lezione «Il framework delle sessioni» è gratuita?
Sì — il testo completo di «Il framework delle sessioni» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.
Cosa imparerò in «Il framework delle sessioni»?
Memorizzare dati associati a un singolo visitatore Eserciti Django Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Django Academy?
Non è richiesta alcuna esperienza precedente. Django Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Il framework delle sessioni»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Django Academy?
Sì. Ogni lezione Django Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Il framework delle sessioni
- Leggere e scrivere request.session
- Il framework dei messaggi
- Cookie o sessioni