0
0
Djangoframework~10 mins

Session expiry behavior in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a session variable in Django.

Django
request.session[[1]] = 'user123'
Drag options to blanks, or click blank then click option'
A'user_id'
Buser_id
C'session_key'
Dsession_key
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes around the session key name.
Using variable names without quotes which causes errors.
2fill in blank
medium

Complete the code to set the session expiry time to 5 minutes.

Django
request.session.set_expiry([1])
Drag options to blanks, or click blank then click option'
A'5 minutes'
B5
C300
D60
Attempts:
3 left
💡 Hint
Common Mistakes
Passing minutes directly instead of seconds.
Using a string instead of an integer.
3fill in blank
hard

Fix the error in the code to clear the session data properly.

Django
request.session.[1]()
Drag options to blanks, or click blank then click option'
Aflush
Bclear
Cdelete
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using clear() which only removes data but keeps the session cookie.
Using non-existent methods like delete() or remove().
4fill in blank
hard

Fill both blanks to check if the session has expired and then set a new expiry.

Django
if request.session.get_expiry_age() [1] 0:
    request.session.set_expiry([2])
Drag options to blanks, or click blank then click option'
A>
B<
C300
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator causing logic errors.
Setting expiry time to zero which means session expires immediately.
5fill in blank
hard

Fill all three blanks to create a session dictionary comprehension that stores user IDs with expiry times greater than 10 minutes.

Django
active_sessions = {user: request.session.get_expiry_age() [1] 600 for user in users if request.session.get_expiry_age() [2] 600 and request.session.exists([3])}
Drag options to blanks, or click blank then click option'
A>
B<
C'session_key'
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up comparison operators causing wrong filtering.
Using a string literal instead of the user variable for session key.