0
0
Djangoframework~10 mins

Setting and getting session data 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 named 'user_id' to 42.

Django
request.session[[1]] = 42
Drag options to blanks, or click blank then click option'
A'session_id'
Buser_id
C'id_user'
D'user_id'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the session key.
Using a variable name without quotes.
2fill in blank
medium

Complete the code to get the session variable 'user_id' safely with a default of None.

Django
user_id = request.session.[1]('user_id', None)
Drag options to blanks, or click blank then click option'
Afetch
Bget
Csetdefault
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' which removes the key.
Using 'setdefault' which sets a default if missing.
3fill in blank
hard

Fix the error in the code to delete the session variable 'user_id'.

Django
del request.session[1]
Drag options to blanks, or click blank then click option'
Auser_id
B('user_id')
C['user_id']
D.user_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of brackets.
Using dot notation which is invalid for session keys.
4fill in blank
hard

Fill both blanks to check if 'user_id' exists in session and then get it.

Django
if [1] in request.session:
    user_id = request.session.[2]('user_id')
Drag options to blanks, or click blank then click option'
A'user_id'
Bget
Cpop
D'session_id'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key names or missing quotes.
Using 'pop' which removes the key.
5fill in blank
hard

Fill all three blanks to set a session variable 'cart' to an empty list if not present, then append 'item1'.

Django
if [1] not in request.session:
    request.session[[2]] = []
request.session[[3]].append('item1')
Drag options to blanks, or click blank then click option'
A'cart'
B'items'
D'basket'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for checking and setting.
Forgetting quotes around the key.