0
0
Djangoframework~20 mins

Setting and getting session data in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Session Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django view when session key 'count' is not set?

Consider this Django view that tries to read a session key 'count' and increments it:

def my_view(request):
    count = request.session.get('count', 0)
    count += 1
    request.session['count'] = count
    return HttpResponse(f"Count is {count}")

What will the response content be on the first visit?

Django
def my_view(request):
    count = request.session.get('count', 0)
    count += 1
    request.session['count'] = count
    return HttpResponse(f"Count is {count}")
ACount is None
BCount is 0
CCount is 1
DKeyError: 'count'
Attempts:
2 left
💡 Hint

Remember the default value used in get when the key is missing.

📝 Syntax
intermediate
1:30remaining
Which option correctly sets a session variable 'user' to 'alice' in Django?

Choose the correct code snippet to set the session key 'user' to the string 'alice' inside a Django view.

Arequest.session.add('user', 'alice')
Brequest.session['user'] = 'alice'
Crequest.session.set('user', 'alice')
Drequest.session.user = 'alice'
Attempts:
2 left
💡 Hint

Think about how Python dictionaries are used to set session data.

🔧 Debug
advanced
2:30remaining
Why does this Django view fail to save session data?

Look at this Django view:

def view(request):
    request.session['visits'] = request.session.get('visits', 0) + 1
    return HttpResponse(f"Visits: {request.session['visits']}")

After calling this view multiple times, the visit count does not increase. What is the most likely reason?

Django
def view(request):
    request.session['visits'] = request.session.get('visits', 0) + 1
    return HttpResponse(f"Visits: {request.session['visits']}")
ASession middleware is not enabled in settings
BThe view does not call request.session.save() explicitly
CHttpResponse does not support f-strings
DThe session key 'visits' is read-only
Attempts:
2 left
💡 Hint

Check if the session system is properly activated in Django.

state_output
advanced
2:00remaining
What is the session data after this sequence of requests?

Assume a user makes these requests to a Django view that manages session data:

def counter(request):
    if 'count' not in request.session:
        request.session['count'] = 0
    request.session['count'] += 2
    return HttpResponse(str(request.session['count']))

What will be the session value for 'count' after the third request?

Django
def counter(request):
    if 'count' not in request.session:
        request.session['count'] = 0
    request.session['count'] += 2
    return HttpResponse(str(request.session['count']))
A6
B2
C3
D0
Attempts:
2 left
💡 Hint

Count how much the value increases each request.

🧠 Conceptual
expert
2:30remaining
Which statement about Django session data is true?

Choose the correct statement about how Django handles session data.

ASession data is stored client-side in cookies by default
BSession data requires explicit saving by calling request.session.save() after modification
CSession data is automatically encrypted and cannot be read by the server
DSession data is stored server-side and linked to the client via a session cookie
Attempts:
2 left
💡 Hint

Think about where Django keeps session data and how it identifies clients.