0
0
Djangoframework~5 mins

Session expiry behavior in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is session expiry in Django?
Session expiry in Django means the time when a user's session data is deleted or becomes invalid, requiring the user to log in again or start a new session.
Click to reveal answer
beginner
How can you set a session to expire when the user closes the browser in Django?
Set SESSION_EXPIRE_AT_BROWSER_CLOSE = True in Django settings. This makes the session expire as soon as the browser is closed.
Click to reveal answer
beginner
What does the setting SESSION_COOKIE_AGE control in Django?
SESSION_COOKIE_AGE controls how long (in seconds) a session cookie lasts before it expires. The default is 1209600 seconds (2 weeks).
Click to reveal answer
intermediate
How can you programmatically set a custom expiry time for a session in Django?
Use request.session.set_expiry(value) where value can be seconds (int), a datetime or timedelta object, or 0 to expire on browser close.
Click to reveal answer
beginner
What happens if you do not set any session expiry in Django?
If no expiry is set, Django uses the default SESSION_COOKIE_AGE (2 weeks) and the session will expire after that time unless the user logs out or clears cookies.
Click to reveal answer
Which Django setting makes the session expire when the browser closes?
ASESSION_COOKIE_AGE = 0
BSESSION_EXPIRE_AT_BROWSER_CLOSE = True
CSESSION_SAVE_EVERY_REQUEST = True
DSESSION_ENGINE = 'django.contrib.sessions.backends.cache'
What is the default duration of a Django session cookie if not changed?
AUntil browser closes
B1 day
C1 hour
D2 weeks
How do you set a session to expire after 5 minutes programmatically?
Arequest.session.set_expiry(300)
Brequest.session.set_expiry(True)
Crequest.session.set_expiry(0)
Drequest.session.set_expiry('5 minutes')
If SESSION_EXPIRE_AT_BROWSER_CLOSE is False and SESSION_COOKIE_AGE is 0, what happens?
ASession never expires
BSession expires at browser close
CSession expires immediately
DSession uses default expiry of 2 weeks
Which method is used to clear session data in Django?
Arequest.session.clear()
Brequest.session.delete()
Crequest.session.expire()
Drequest.session.remove()
Explain how Django handles session expiry by default and how you can customize it.
Think about settings and methods that control session lifetime.
You got /4 concepts.
    Describe how to make a Django session expire immediately when the user closes their browser.
    Focus on the setting that links session expiry to browser behavior.
    You got /3 concepts.