Complete the code to set Django to use cookie-based sessions.
SESSION_ENGINE = '[1]'
To use cookie-based sessions in Django, set SESSION_ENGINE to django.contrib.sessions.backends.signed_cookies.
Complete the code to set Django to use database-backed sessions.
SESSION_ENGINE = '[1]'
Database sessions store session data in the database. Set SESSION_ENGINE to django.contrib.sessions.backends.db to enable this.
Fix the error in the code to correctly import the session middleware in Django settings.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'[1]',
'django.middleware.common.CommonMiddleware',
]The correct middleware to enable sessions is django.contrib.sessions.middleware.SessionMiddleware.
Fill both blanks to create a session key and store a value in a Django view.
def my_view(request): request.session[1] = [2] return HttpResponse('Session set')
To store a value in session, use request.session['key'] = 'value'. The key must be a string inside brackets.
Fill all three blanks to retrieve a session value with a default in a Django view.
def my_view(request): color = request.session.get([1], [2]) return HttpResponse(f'Favorite color is [3]')
Use request.session.get('key', 'default') to safely get a session value. Then use the variable color in the response.