Consider a Django view that calls logout(request). What is the state of request.user immediately after this call?
from django.contrib.auth import logout def logout_view(request): logout(request) user_after_logout = request.user return user_after_logout.is_authenticated
Think about what Django does to the user session after logout.
After calling logout(request), Django replaces request.user with an AnonymousUser instance. This user is not authenticated, so is_authenticated returns False.
Choose the code snippet that correctly logs out the user and redirects to the home page ('/') using Django's built-in functions.
Check the logout function call and the redirect target.
Option A correctly calls logout(request) with the request argument and redirects to the root URL '/'. Option A misses the request argument in logout. Option A redirects to 'home' which may not be defined. Option A redirects back to the same page, not home.
Given this Django logout view, why does it raise a TypeError?
from django.contrib.auth import logout from django.shortcuts import redirect def logout_view(request): logout() return redirect('/')
Check the function call signature for logout.
The logout function requires the request argument. Calling logout() without arguments raises a TypeError.
After calling logout(request) in a Django view, what happens to the session data?
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
Think about what logout does to the session data.
Django's logout(request) flushes the session data, removing all keys. Thus, request.session.keys() is empty after logout.
Choose the correct statement about what Django's logout(request) function does internally.
Consider what happens to session and user state after logout.
Django's logout(request) clears the session data and replaces request.user with an AnonymousUser. It does not change passwords or perform redirects automatically.