0
0
Djangoframework~10 mins

Session expiry behavior in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Session expiry behavior
User logs in
Session created with expiry time
User makes requests
Check if session expired?
NoContinue session
Yes
Session expired
User redirected to login
This flow shows how Django creates a session with an expiry time, checks it on each request, and expires it when time runs out.
Execution Sample
Django
from django.contrib.auth import logout

# User logs in
request.session.set_expiry(300)  # 5 minutes expiry

# On each request
if request.session.get_expiry_age() <= 0:
    # Session expired
    logout(request)
This code sets a session expiry of 5 minutes and checks on each request if the session has expired to log out the user.
Execution Table
StepActionSession Expiry Time (seconds)Time Passed (seconds)Session Valid?Result
1User logs in, session created3000YesSession active
2User makes request after 100s300100YesSession active
3User makes request after 299s300299YesSession active
4User makes request after 301s300301NoSession expired, user logged out
5User tries request after expiry300350NoRedirect to login
💡 Session expires when time passed exceeds expiry time (301 > 300 seconds)
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
session_expiry_timeNone300300300300300
time_passed00100299301350
session_validFalseTrueTrueTrueFalseFalse
Key Moments - 2 Insights
Why does the session become invalid after 301 seconds but not at 299 seconds?
Because the session expiry time is set to 300 seconds. At 299 seconds, time passed is less than expiry, so session is valid. At 301 seconds, time passed exceeds expiry, so session is invalid (see execution_table rows 3 and 4).
What happens if the user tries to make a request after the session expired?
The session is no longer valid, so Django logs out the user and redirects to login (see execution_table row 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the session_valid value at step 3?
ATrue
BFalse
CNone
DError
💡 Hint
Check the 'Session Valid?' column at step 3 in the execution_table.
At which step does the session expire according to the execution table?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the first step where 'Session Valid?' is 'No' in the execution_table.
If the session expiry time was set to 600 seconds, how would the session_valid value change at step 4?
AIt would be False
BIt would be True
CIt would be None
DIt would cause an error
💡 Hint
Compare the time passed (301s) with the new expiry time (600s) in variable_tracker.
Concept Snapshot
Django sessions store user data with an expiry time.
Set expiry with request.session.set_expiry(seconds).
On each request, check expiry with get_expiry_age().
If expired, session is invalid and user is logged out.
Expired sessions redirect users to login.
This keeps user data secure and session fresh.
Full Transcript
In Django, when a user logs in, a session is created with a set expiry time, for example 300 seconds (5 minutes). Each time the user makes a request, Django checks if the session has expired by comparing the time passed since creation with the expiry time. If the time passed is less than the expiry, the session remains valid and the user continues. If the time passed exceeds the expiry, the session is invalidated, the user is logged out, and redirected to the login page. This process ensures sessions do not last forever and helps keep user data secure.