Challenge - 5 Problems
APIView Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this APIView GET method?
Consider this Django REST Framework APIView code. What will be the JSON response when a GET request is made?
Django
from rest_framework.views import APIView from rest_framework.response import Response class HelloView(APIView): def get(self, request): return Response({"message": "Hello, world!"})
Attempts:
2 left
💡 Hint
Look at the return value inside the get method.
✗ Incorrect
The get method returns a Response with a dictionary containing the message key. This results in a JSON response with {"message": "Hello, world!"}.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a POST method in APIView?
You want to add a POST method to this APIView that returns the posted data back. Which code snippet is correct?
Django
from rest_framework.views import APIView from rest_framework.response import Response class EchoView(APIView): # Add POST method here
Attempts:
2 left
💡 Hint
Remember the method signature for APIView methods includes self and request.
✗ Incorrect
Option B correctly defines the post method with self and request parameters and returns a Response wrapping request.data. Other options have wrong signatures or missing Response wrapper.
❓ state_output
advanced2:00remaining
What is the response status code of this APIView method?
Given this APIView code, what status code will the response have when a DELETE request is made?
Django
from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status class ItemView(APIView): def delete(self, request): # pretend to delete an item return Response({"detail": "Deleted"}, status=status.HTTP_204_NO_CONTENT)
Attempts:
2 left
💡 Hint
Look at the status parameter in the Response.
✗ Incorrect
The Response explicitly sets status=status.HTTP_204_NO_CONTENT, which corresponds to HTTP status code 204.
🔧 Debug
advanced2:00remaining
Why does this APIView raise an error on GET?
This APIView raises an error when a GET request is made. What is the cause?
Django
from rest_framework.views import APIView from rest_framework.response import Response class FaultyView(APIView): def get(self): return Response({"msg": "Hi"})
Attempts:
2 left
💡 Hint
Check the method signature for APIView methods.
✗ Incorrect
APIView methods must accept self and request parameters. Missing request causes a TypeError when called.
🧠 Conceptual
expert3:00remaining
Which option correctly restricts APIView to only allow GET and POST methods?
You want an APIView that only accepts GET and POST requests and returns 405 Method Not Allowed for others. Which code snippet achieves this?
Attempts:
2 left
💡 Hint
Check the APIView attribute that controls allowed HTTP methods.
✗ Incorrect
Setting http_method_names to ['get', 'post'] restricts the view to only those methods. Other options either use wrong attribute names or manually handle methods less cleanly.