Bird
Raised Fist0
Djangoframework~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main difference between cookie-based sessions and database sessions in Django?
easy
A. Both store data only on the server but in different database tables.
B. Cookie-based sessions store data on the client browser, while database sessions store data on the server.
C. Both store data only on the client browser but use different encryption methods.
D. Cookie-based sessions store data on the server, while database sessions store data on the client browser.

Solution

  1. Step 1: Understand where session data is stored

    Cookie-based sessions keep the session data inside the user's browser cookies. Database sessions keep the data on the server side in a database.
  2. Step 2: Compare storage locations

    Since cookie sessions store data client-side and database sessions store data server-side, this is the key difference.
  3. Final Answer:

    Cookie-based sessions store data on the client browser, while database sessions store data on the server. -> Option B
  4. Quick Check:

    Storage location = client vs server [OK]
Hint: Remember: cookies = browser, database = server [OK]
Common Mistakes:
  • Confusing client and server storage
  • Thinking both store data only on server
  • Assuming cookie sessions use server database
2. Which setting in Django controls whether sessions use cookies or the database?
easy
A. SESSION_ENGINE
B. SESSION_COOKIE_NAME
C. SESSION_SAVE_EVERY_REQUEST
D. SESSION_EXPIRE_AT_BROWSER_CLOSE

Solution

  1. Step 1: Identify session-related settings

    Django uses several settings for sessions, but the one that controls the backend storage is SESSION_ENGINE.
  2. Step 2: Understand SESSION_ENGINE role

    Changing SESSION_ENGINE switches between cookie-based sessions and database sessions.
  3. Final Answer:

    SESSION_ENGINE -> Option A
  4. Quick Check:

    Session backend = SESSION_ENGINE [OK]
Hint: SESSION_ENGINE sets session storage type [OK]
Common Mistakes:
  • Choosing cookie name instead of engine
  • Confusing save frequency with storage
  • Mixing expiration with storage setting
3. Given this Django setting: SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies', what happens when you store a large amount of data in the session?
medium
A. The data is stored securely on the server database.
B. The data is split between cookie and database automatically.
C. The session will raise an error and not save any data.
D. The data is stored in the user's browser cookie, which may cause size issues.

Solution

  1. Step 1: Identify the session backend

    The setting 'signed_cookies' means session data is stored in browser cookies, signed for security.
  2. Step 2: Consider cookie size limits

    Browser cookies have size limits (usually around 4KB). Storing large data can cause issues or truncation.
  3. Final Answer:

    The data is stored in the user's browser cookie, which may cause size issues. -> Option D
  4. Quick Check:

    Signed cookies = client storage, watch size limits [OK]
Hint: Signed cookies store data client-side, watch size limits [OK]
Common Mistakes:
  • Assuming data is stored server-side
  • Thinking Django splits data automatically
  • Expecting error instead of silent truncation
4. You switched SESSION_ENGINE to use database sessions but your session data is not saving. Which is the most likely cause?
medium
A. You set SESSION_COOKIE_NAME incorrectly.
B. You did not clear browser cookies before testing.
C. You forgot to run migrations to create the session table.
D. You used the wrong database engine in settings.

Solution

  1. Step 1: Understand database session requirements

    Database sessions require a database table to store session data, created by migrations.
  2. Step 2: Identify missing migration impact

    If migrations are not run, the session table does not exist, so session data cannot be saved.
  3. Final Answer:

    You forgot to run migrations to create the session table. -> Option C
  4. Quick Check:

    Database sessions need session table migration [OK]
Hint: Run migrations after switching to database sessions [OK]
Common Mistakes:
  • Blaming cookies instead of database setup
  • Changing cookie name unrelated to saving
  • Confusing database engine with session table
5. You want to store sensitive user data in sessions and ensure it is not exposed to the client. Which session backend should you choose and why?
hard
A. Use database sessions because data is stored server-side and not exposed to the client.
B. Use cookie-based sessions because they are encrypted and secure.
C. Use cookie-based sessions because they are faster and store data locally.
D. Use database sessions but also store data in cookies for backup.

Solution

  1. Step 1: Consider data sensitivity and storage location

    Sensitive data should not be stored in client-accessible places like cookies, even if encrypted.
  2. Step 2: Choose backend that keeps data server-side

    Database sessions store data on the server, protecting it from client access and tampering.
  3. Step 3: Evaluate options

    Use database sessions because data is stored server-side and not exposed to the client. correctly chooses database sessions for sensitive data security.
  4. Final Answer:

    Use database sessions because data is stored server-side and not exposed to the client. -> Option A
  5. Quick Check:

    Sensitive data = server storage [OK]
Hint: Sensitive data? Store server-side with database sessions [OK]
Common Mistakes:
  • Assuming cookie encryption is enough
  • Mixing speed with security needs
  • Trying to duplicate data in cookies and DB