Bird
0
0

Given this DRF view snippet, what will happen if an anonymous user tries to access it?

medium📝 component behavior Q13 of 15
Django - DRF Advanced Features
Given this DRF view snippet, what will happen if an anonymous user tries to access it?
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView

class MyView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request):
        return Response({'message': 'Hello'})
AThe user will receive a 401 Unauthorized response
BThe user will see the message 'Hello'
CThe user will receive a 403 Forbidden response
DThe server will raise a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand IsAuthenticated permission behavior

    This permission denies access to anonymous users and returns 401 Unauthorized.
  2. Step 2: Analyze the code behavior for anonymous user

    Since the user is not logged in, DRF returns 401, not 403 or success.
  3. Final Answer:

    The user will receive a 401 Unauthorized response -> Option A
  4. Quick Check:

    IsAuthenticated denies anonymous with 401 [OK]
Quick Trick: IsAuthenticated returns 401 for anonymous users [OK]
Common Mistakes:
MISTAKES
  • Confusing 401 Unauthorized with 403 Forbidden
  • Expecting anonymous users to see data
  • Thinking code has syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes