Recall & Review
beginner
What is a session in Django?
A session in Django is a way to store data on the server side for a particular user across multiple requests. It helps keep user-specific information like login status or preferences.
Click to reveal answer
beginner
How do you set a session variable in a Django view?
You set a session variable by assigning a value to a key in the request.session dictionary, like
request.session['key'] = value.Click to reveal answer
beginner
How do you retrieve a session variable safely in Django?
Use
request.session.get('key') to get the value. It returns None if the key does not exist, avoiding errors.Click to reveal answer
intermediate
What happens if you try to access a session key that does not exist using
request.session['key']?Django raises a
KeyError because the key is missing. To avoid this, use request.session.get('key').Click to reveal answer
intermediate
How can you delete a session variable in Django?
Use
del request.session['key'] to remove a session variable. This deletes the key and its value from the session data.Click to reveal answer
Which of these is the correct way to set a session variable in Django?
✗ Incorrect
In Django, session data is set by assigning a value to a key in request.session like a dictionary.
How do you safely get a session value that might not exist?
✗ Incorrect
Using request.session.get('key') returns None if the key is missing, preventing errors.
What error occurs if you access a missing session key with request.session['key']?
✗ Incorrect
Accessing a missing key in a dictionary-like object raises a KeyError.
How do you delete a session variable named 'cart'?
✗ Incorrect
Deleting a session key is done with del request.session['cart'].
Where is session data stored in Django by default?
✗ Incorrect
By default, Django stores session data in the database, keeping it secure on the server.
Explain how to set, get, and delete session data in a Django view.
Think of request.session as a dictionary to store user data.
You got /3 concepts.
Describe what happens if you try to access a session key that does not exist using request.session['key'] and how to avoid errors.
Consider how dictionary access works in Python.
You got /3 concepts.