Bird
0
0

You wrote this code to enable TokenAuthentication but your API always returns 403 Forbidden. What is the likely mistake?

medium📝 Debug Q14 of 15
Django - DRF Advanced Features
You wrote this code to enable TokenAuthentication but your API always returns 403 Forbidden. What is the likely mistake?
from rest_framework.authentication import TokenAuthentication

class MyView(APIView):
    authentication_classes = [TokenAuthentication]

    def get(self, request):
        return Response({"message": "Hello"})
AForgot to add parentheses after TokenAuthentication in authentication_classes.
BTokenAuthentication is not imported correctly.
CThe get method should be named post.
Dauthentication_classes should be a tuple, not a list.
Step-by-Step Solution
Solution:
  1. Step 1: Check authentication_classes syntax

    authentication_classes must contain instances, so TokenAuthentication() with parentheses, not the class itself.
  2. Step 2: Effect of missing parentheses

    Without parentheses, DRF does not recognize the authentication class properly, causing 403 Forbidden errors.
  3. Final Answer:

    Forgot to add parentheses after TokenAuthentication in authentication_classes. -> Option A
  4. Quick Check:

    Use TokenAuthentication() not TokenAuthentication = C [OK]
Quick Trick: Always instantiate authentication classes with () [OK]
Common Mistakes:
MISTAKES
  • Using class name without parentheses
  • Confusing 403 with 401 errors
  • Changing method name unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes