Sessions help remember who a user is while they browse your site. Session expiry makes sure this memory doesn't last forever, keeping things safe and tidy.
Session expiry behavior in Django
Start learning this pattern below
Jump into concepts and practice - no test required
SESSION_COOKIE_AGE = 1209600 # Time in seconds (default 2 weeks) SESSION_EXPIRE_AT_BROWSER_CLOSE = False # To set session expiry in a view: request.session.set_expiry(value) # value can be: # - an integer (seconds) # - 0 (expire on browser close) # - None (use global settings)
SESSION_COOKIE_AGE sets how long sessions last by default.
SESSION_EXPIRE_AT_BROWSER_CLOSE makes sessions end when the browser closes if set to True.
SESSION_COOKIE_AGE = 3600 # Sessions last 1 hour
SESSION_EXPIRE_AT_BROWSER_CLOSE = Truedef my_view(request): request.session.set_expiry(300) # expire after 5 minutes return HttpResponse('Session set to 5 minutes')
def my_view(request): request.session.set_expiry(0) # expire on browser close return HttpResponse('Session expires on browser close')
This example has two views. One sets a session that expires in 10 seconds and stores a user name. The other checks if the session still has the user.
from django.http import HttpResponse def set_short_session(request): # Set session to expire after 10 seconds request.session.set_expiry(10) request.session['user'] = 'Alice' return HttpResponse('Session set for 10 seconds') def check_session(request): user = request.session.get('user', 'No session') return HttpResponse(f'User in session: {user}')
Session expiry counts from the last user activity, so it resets if the user keeps interacting.
Setting expiry to 0 means the session ends when the browser closes, which is good for sensitive data.
Always test session expiry behavior in your browser's private mode to avoid cached sessions.
Sessions remember users but should not last forever for safety.
You can control session length globally or per user with set_expiry().
Use session expiry to improve security and user experience.
Practice
set_expiry() method do in Django sessions?Solution
Step 1: Understand the purpose of
This method controls the lifetime of a session by setting its expiration time.set_expiry()Step 2: Compare options with the method's function
Only It sets how long a user's session will last before expiring. correctly describes thatset_expiry()sets how long the session lasts before it expires.Final Answer:
It sets how long a user's session will last before expiring. -> Option CQuick Check:
Session expiry time = set_expiry() [OK]
- Confusing set_expiry() with session deletion
- Thinking it creates or encrypts sessions
- Assuming it resets session data
Solution
Step 1: Identify the correct method and object
The methodset_expiry()is called onrequest.sessionto set expiry time.Step 2: Check syntax correctness
request.session.set_expiry(300) uses the correct method and object:request.session.set_expiry(300). Other options use incorrect method names or objects.Final Answer:
request.session.set_expiry(300) -> Option BQuick Check:
Correct method call = request.session.set_expiry(300) [OK]
- Calling set_expiry on request instead of request.session
- Using wrong method names like expire or set_expiry_time
- Missing parentheses or wrong argument
request.session.set_expiry(0)
Solution
Step 1: Understand what passing 0 to set_expiry means
In Django, setting expiry to 0 means the session expires when the browser closes (a browser-length session).Step 2: Compare with other options
The session will expire when the browser is closed. matches this behavior. The session will never expire. is false because 0 does not mean never expire. The session will expire after 0 seconds immediately. is incorrect because it does not expire immediately. The session expiry will use the default global timeout. is incorrect because default timeout is overridden.Final Answer:
The session will expire when the browser is closed. -> Option DQuick Check:
set_expiry(0) = expire on browser close [OK]
- Thinking 0 means no expiry
- Assuming immediate expiry at 0 seconds
- Confusing with default session timeout
request.session.set_expiry = 600
Solution
Step 1: Analyze the code syntax
The code assigns 600 toset_expiryinstead of calling it as a method with parentheses.Step 2: Understand correct usage
The correct usage isrequest.session.set_expiry(600)to call the method and set expiry time.Final Answer:
It assigns a value to the method instead of calling it. -> Option AQuick Check:
Use parentheses to call set_expiry() [OK]
- Assigning value instead of calling method
- Confusing seconds with milliseconds
- Believing 600 causes error due to size
Solution
Step 1: Understand session expiry reset behavior
Callingset_expiry(300)on every request resets the expiry countdown, keeping session alive if user is active.Step 2: Evaluate other options
Setset_expiry(300)once when the session is created only. sets expiry once, so session expires after 5 minutes regardless of activity. Setset_expiry(0)to expire on browser close and ignore activity. expires on browser close, ignoring time. Do not set expiry; rely on default session timeout. uses default timeout, no control.Final Answer:
Setset_expiry(300)on every user request to reset expiry time. -> Option AQuick Check:
Reset expiry on each request = Setset_expiry(300)on every user request to reset expiry time. [OK]
- Setting expiry only once at session creation
- Using 0 expiry which ignores time
- Relying on default timeout without control
