0
0
Djangoframework~20 mins

APIView for custom endpoints in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
APIView Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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!"})
A{"message": "Hello, world!"}
B{"error": "Method not allowed"}
CHTTP 404 Not Found
DHTTP 500 Internal Server Error
Attempts:
2 left
💡 Hint
Look at the return value inside the get method.
📝 Syntax
intermediate
2: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
A
def post(self):
    return Response(request.data)
B
def post(self, request):
    return Response(request.data)
C
def post(request):
    return Response(request.data)
D
def post(self, request):
    return request.data
Attempts:
2 left
💡 Hint
Remember the method signature for APIView methods includes self and request.
state_output
advanced
2: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)
A500
B200
C404
D204
Attempts:
2 left
💡 Hint
Look at the status parameter in the Response.
🔧 Debug
advanced
2: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"})
AMissing 'request' parameter in get method
BResponse is not imported
CReturn value is not a dictionary
DAPIView requires a post method
Attempts:
2 left
💡 Hint
Check the method signature for APIView methods.
🧠 Conceptual
expert
3: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?
A
class MyView(APIView):
    def get(self, request):
        return Response({'msg': 'GET'})
    def post(self, request):
        return Response({'msg': 'POST'})
    def delete(self, request):
        return Response(status=405)
B
class MyView(APIView):
    allowed_methods = ['GET', 'POST']
    def get(self, request):
        return Response({'msg': 'GET'})
    def post(self, request):
        return Response({'msg': 'POST'})
C
class MyView(APIView):
    http_method_names = ['get', 'post']
    def get(self, request):
        return Response({'msg': 'GET'})
    def post(self, request):
        return Response({'msg': 'POST'})
D
class MyView(APIView):
    def dispatch(self, request, *args, **kwargs):
        if request.method not in ['GET', 'POST']:
            return Response(status=405)
        return super().dispatch(request, *args, **kwargs)
Attempts:
2 left
💡 Hint
Check the APIView attribute that controls allowed HTTP methods.