Complete the code to import the session middleware in Django settings.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
[1],
'django.middleware.common.CommonMiddleware',
]The SessionMiddleware enables session support in Django. It must be included in the MIDDLEWARE list.
Complete the code to set a session variable in a Django view.
def my_view(request): request.session[[1]] = 'blue' return HttpResponse('Color set')
Session variables are stored as key-value pairs. Here, the key is 'favorite_color'.
Fix the error in this code to retrieve a session variable safely.
def get_color(request): color = request.session.get([1], 'red') return HttpResponse(f'Color is {color}')
The key for session.get must be a string, so it needs quotes around it.
Fill both blanks to check if a session key exists and then delete it.
if [1] in request.session: [2] request.session['user_id']
Use 'user_id' to check if the key exists in request.session. Use del request.session['user_id'] to delete it.
Fill all three blanks to create a dictionary comprehension that stores word lengths in session if length is greater than 3.
request.session['word_lengths'] = { [1]: len([2]) for [2] in words if len([2]) [3] 3 }
The comprehension uses 'word' as the key and variable, and filters words with length greater than 3.