Given the following Django REST Framework view using TokenAuthentication, what will be the HTTP status code if a request is made without any token?
from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.authentication import TokenAuthentication from rest_framework.permissions import IsAuthenticated class SampleView(APIView): authentication_classes = [TokenAuthentication] permission_classes = [IsAuthenticated] def get(self, request): return Response({'message': 'Success'})
Think about what happens when authentication credentials are missing.
TokenAuthentication requires a valid token in the request header. Without it, the request is not authenticated, so DRF returns a 401 Unauthorized response.
In Django REST Framework, you want to enable JWT authentication using SimpleJWT. Which of the following REST_FRAMEWORK settings is correct?
Check the full import path for JWTAuthentication in SimpleJWT.
The correct import path for JWTAuthentication in SimpleJWT is rest_framework_simplejwt.authentication.JWTAuthentication. Other options are incorrect or do not exist.
In a DRF view using JWT authentication, after a valid JWT token is provided, what is the type of request.user?
from rest_framework_simplejwt.authentication import JWTAuthentication from rest_framework.views import APIView from rest_framework.response import Response class MyView(APIView): authentication_classes = [JWTAuthentication] def get(self, request): user_type = type(request.user).__name__ return Response({'user_type': user_type})
Think about what Django sets as the user after successful authentication.
After JWT authentication, request.user is set to the Django User model instance representing the authenticated user, typically named 'User'.
Consider this DRF view using TokenAuthentication. It raises an error when a request with a valid token is made. What is the cause?
from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.authentication import TokenAuthentication from rest_framework.permissions import IsAuthenticated class DebugView(APIView): authentication_classes = [TokenAuthentication] permission_classes = [IsAuthenticated] def get(self, request): token_key = request.auth.key return Response({'token': token_key})
Check the type of request.auth when using TokenAuthentication.
In TokenAuthentication, request.auth is the token string, not a Token object. Accessing key attribute on a string causes AttributeError.
In Django REST Framework using SimpleJWT, which statement about the token refresh endpoint is true?
Think about how refresh tokens work in JWT authentication.
In SimpleJWT, the refresh token can be used multiple times until it expires to obtain new access tokens. It does not become invalid after one use.