0
0
Djangoframework~20 mins

Session expiry behavior in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Session Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when SESSION_COOKIE_AGE is set to 0?
In Django settings, if you set SESSION_COOKIE_AGE = 0, what will be the behavior of the session cookie in the browser?
AThe session cookie will expire immediately and the user will be logged out on the next request.
BDjango will raise a configuration error and refuse to start.
CThe session cookie will never expire and remain valid indefinitely.
DThe session cookie will behave like a browser-length cookie and expire when the browser is closed.
Attempts:
2 left
💡 Hint
Think about what a zero value means for cookie expiration in browsers.
state_output
intermediate
2:00remaining
What is the session expiry time after calling set_expiry(300)?
Given a Django view where request.session.set_expiry(300) is called, what is the session expiry behavior?
Django
def my_view(request):
    request.session.set_expiry(300)
    return HttpResponse('Hello')
AThe session will expire after 300 seconds (5 minutes) from the last activity.
BThe session will expire after 300 seconds from when the session was created, ignoring activity.
CThe session will never expire because set_expiry disables expiration.
DThe session will expire immediately because 300 is treated as False.
Attempts:
2 left
💡 Hint
Check Django docs on set_expiry with integer values.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly sets a session to expire at browser close?
Select the Django code snippet that correctly sets the session to expire when the browser closes.
Arequest.session.set_expiry(-1)
Brequest.session.set_expiry('browser')
Crequest.session.set_expiry(None)
Drequest.session.set_expiry(0)
Attempts:
2 left
💡 Hint
Remember the special meaning of 0 in set_expiry.
🔧 Debug
advanced
2:00remaining
Why does session never expire despite set_expiry(10)?
A developer sets request.session.set_expiry(10) in a Django view, but the session never expires after 10 seconds. What is the most likely cause?
AThe session cookie is set with a max-age of 0, overriding set_expiry.
BThe SESSION_SAVE_EVERY_REQUEST setting is True, so expiry resets on every request.
CThe server time is incorrect, causing expiry to fail.
Dset_expiry only works with values greater than 60 seconds.
Attempts:
2 left
💡 Hint
Think about how SESSION_SAVE_EVERY_REQUEST affects session expiry.
🧠 Conceptual
expert
2:00remaining
What error occurs if you call set_expiry with a string other than 'session' or 'never'?
In Django, what happens if you call request.session.set_expiry('invalid_string')?
ARaises a ValueError indicating invalid expiry value.
BSilently ignores the invalid value and keeps previous expiry.
CRaises a TypeError because string is not allowed.
DSets the session to never expire regardless of the string.
Attempts:
2 left
💡 Hint
Check Django source or docs on allowed string values for set_expiry.