0
0
Djangoframework~10 mins

Cookie-based sessions vs database sessions in Django - Interactive Practice

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

Complete the code to set Django to use cookie-based sessions.

Django
SESSION_ENGINE = '[1]'
Drag options to blanks, or click blank then click option'
Adjango.contrib.sessions.backends.file
Bdjango.contrib.sessions.backends.db
Cdjango.contrib.sessions.backends.signed_cookies
Ddjango.contrib.sessions.backends.cache
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'db' backend instead of 'signed_cookies' for cookie sessions.
Forgetting to set SESSION_ENGINE in settings.
2fill in blank
medium

Complete the code to set Django to use database-backed sessions.

Django
SESSION_ENGINE = '[1]'
Drag options to blanks, or click blank then click option'
Adjango.contrib.sessions.backends.db
Bdjango.contrib.sessions.backends.cache
Cdjango.contrib.sessions.backends.signed_cookies
Ddjango.contrib.sessions.backends.file
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'signed_cookies' backend instead of 'db' for database sessions.
Not running migrations after enabling database sessions.
3fill in blank
hard

Fix the error in the code to correctly import the session middleware in Django settings.

Django
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '[1]',
    'django.middleware.common.CommonMiddleware',
]
Drag options to blanks, or click blank then click option'
A'django.contrib.sessions.middleware.SessionStore'
B'django.contrib.sessions.middleware.SessionMiddleware'
C'django.middleware.sessions.SessionMiddleware'
D'django.contrib.sessions.middleware.SessionEngine'
Attempts:
3 left
💡 Hint
Common Mistakes
Using SessionStore instead of SessionMiddleware.
Incorrect middleware path or name.
4fill in blank
hard

Fill both blanks to create a session key and store a value in a Django view.

Django
def my_view(request):
    request.session[1] = [2]
    return HttpResponse('Session set')
Drag options to blanks, or click blank then click option'
A['favorite_color']
B'favorite_color'
C'blue'
Dblue
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation instead of brackets for session keys.
Not quoting the session key string.
5fill in blank
hard

Fill all three blanks to retrieve a session value with a default in a Django view.

Django
def my_view(request):
    color = request.session.get([1], [2])
    return HttpResponse(f'Favorite color is [3]')
Drag options to blanks, or click blank then click option'
A'favorite_color'
B'unknown'
Ccolor
D'color'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the session key.
Using the string 'color' instead of the variable color in the response.