0
0
Djangoframework~20 mins

Logout view in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Logout View Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens after calling Django's logout() function in a view?

Consider a Django view that calls logout(request). What is the state of request.user immediately after this call?

Django
from django.contrib.auth import logout

def logout_view(request):
    logout(request)
    user_after_logout = request.user
    return user_after_logout.is_authenticated
Arequest.user is an AnonymousUser and is_authenticated is False
Brequest.user remains the same as before logout and is_authenticated is True
Crequest.user is None and accessing is_authenticated raises AttributeError
Drequest.user is a User instance but is_authenticated is False
Attempts:
2 left
πŸ’‘ Hint

Think about what Django does to the user session after logout.

πŸ“ Syntax
intermediate
2:00remaining
Which Django logout view code snippet correctly logs out a user and redirects to home?

Choose the code snippet that correctly logs out the user and redirects to the home page ('/') using Django's built-in functions.

A
from django.contrib.auth import logout
from django.shortcuts import redirect

def logout_view(request):
    logout(request)
    return redirect('/')
B
from django.contrib.auth import logout
from django.shortcuts import redirect

def logout_view(request):
    logout(request)
    return redirect(request.path)
C
from django.contrib.auth import logout
from django.shortcuts import redirect

def logout_view(request):
    logout(request)
    return redirect('home')
D
from django.contrib.auth import logout
from django.http import HttpResponseRedirect

def logout_view(request):
    logout()
    return HttpResponseRedirect('/')
Attempts:
2 left
πŸ’‘ Hint

Check the logout function call and the redirect target.

πŸ”§ Debug
advanced
2:00remaining
Why does this logout view raise an error?

Given this Django logout view, why does it raise a TypeError?

Django
from django.contrib.auth import logout
from django.shortcuts import redirect

def logout_view(request):
    logout()
    return redirect('/')
AThe view lacks a return statement, causing a TypeError
Blogout() is called without the required request argument, causing a TypeError
Credirect('/') is invalid because '/' is not a named URL pattern
Dlogout() returns a value that cannot be used with redirect()
Attempts:
2 left
πŸ’‘ Hint

Check the function call signature for logout.

❓ state_output
advanced
2:00remaining
What is the session state after logout in Django?

After calling logout(request) in a Django view, what happens to the session data?

Django
from django.contrib.auth import logout

def logout_view(request):
    request.session['key'] = 'value'
    logout(request)
    session_keys = list(request.session.keys())
    return session_keys
AThe session keys are reset to default Django keys but 'key' remains
BThe session retains all keys including 'key', so session_keys contains ['key']
CThe session raises a KeyError when accessing keys after logout
DThe session is flushed and session_keys is an empty list
Attempts:
2 left
πŸ’‘ Hint

Think about what logout does to the session data.

🧠 Conceptual
expert
3:00remaining
Which statement about Django's logout view behavior is true?

Choose the correct statement about what Django's logout(request) function does internally.

AIt invalidates the user’s password to prevent further logins until reset
BIt only sets request.user to None but keeps session data intact
CIt deletes the user’s session data and sets request.user to AnonymousUser
DIt redirects the user automatically to the login page after logout
Attempts:
2 left
πŸ’‘ Hint

Consider what happens to session and user state after logout.