Bird
Raised Fist0
Djangoframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using APIView in Django REST Framework?
easy
A. To automatically generate database tables.
B. To manage user authentication without coding.
C. To style HTML templates.
D. To create custom endpoints by defining methods like get() and post().

Solution

  1. Step 1: Understand the role of APIView

    APIView allows you to write custom logic for handling HTTP requests by defining methods like get() and post().
  2. Step 2: Compare other options

    Options A, C, and D describe unrelated tasks like database management, styling, or authentication without coding, which APIView does not do automatically.
  3. Final Answer:

    To create custom endpoints by defining methods like get() and post(). -> Option D
  4. Quick Check:

    APIView = custom HTTP methods [OK]
Hint: APIView is for custom HTTP methods like get/post [OK]
Common Mistakes:
  • Thinking APIView auto-generates database tables
  • Confusing APIView with template rendering
  • Assuming APIView manages authentication alone
2. Which of the following is the correct way to import APIView and Response in a Django REST Framework view?
easy
A. from django.views import APIView from django.http import Response
B. from rest_framework.views import APIView from rest_framework.response import Response
C. import APIView from rest_framework import Response from rest_framework
D. from rest_framework.api import APIView from rest_framework.api import Response

Solution

  1. Step 1: Recall correct import paths

    APIView is imported from rest_framework.views and Response from rest_framework.response.
  2. Step 2: Check other options for errors

    from django.views import APIView from django.http import Response uses django.views and django.http which do not provide APIView or DRF Response. import APIView from rest_framework import Response from rest_framework uses invalid import syntax. from rest_framework.api import APIView from rest_framework.api import Response uses wrong module paths.
  3. Final Answer:

    from rest_framework.views import APIView from rest_framework.response import Response -> Option B
  4. Quick Check:

    Correct import paths = from rest_framework.views import APIView from rest_framework.response import Response [OK]
Hint: APIView from views, Response from response module [OK]
Common Mistakes:
  • Importing APIView from django.views
  • Using incorrect import syntax
  • Importing Response from wrong module
3. Given this APIView code, what will be the HTTP status code in the response?
from rest_framework.views import APIView
from rest_framework.response import Response

class HelloView(APIView):
    def get(self, request):
        return Response({"message": "Hello!"}, status=201)
medium
A. 201 Created
B. 404 Not Found
C. 200 OK
D. 500 Internal Server Error

Solution

  1. Step 1: Identify the status code in Response

    The Response is returned with status=201, which means Created.
  2. Step 2: Match status code to HTTP meaning

    201 means resource created successfully, so the response status will be 201 Created.
  3. Final Answer:

    201 Created -> Option A
  4. Quick Check:

    Status=201 means Created [OK]
Hint: Check status argument in Response call [OK]
Common Mistakes:
  • Assuming default 200 OK without checking status
  • Confusing 201 with 404 or 500
  • Ignoring the status parameter in Response
4. Identify the error in this APIView code snippet:
from rest_framework.views import APIView
from rest_framework.response import Response

class MyView(APIView):
    def post(self, request):
        data = request.data
        return Response(data, status=200)

    def get(self):
        return Response({"msg": "Hello"})
medium
A. The get method is missing the request parameter.
B. The post method should not return a Response.
C. The status code 200 is invalid in Response.
D. request.data is not accessible in APIView.

Solution

  1. Step 1: Check method signatures

    In APIView, all HTTP methods must accept self and request parameters. The get method lacks the request parameter.
  2. Step 2: Validate other statements

    Returning Response in post is correct. Status 200 is valid. request.data is accessible in APIView.
  3. Final Answer:

    The get method is missing the request parameter. -> Option A
  4. Quick Check:

    All HTTP methods need request parameter [OK]
Hint: Check method parameters: self and request required [OK]
Common Mistakes:
  • Omitting request parameter in methods
  • Thinking status=200 is invalid
  • Believing request.data is unavailable
5. You want to create a custom APIView that accepts a POST request with JSON data containing a number, doubles it, and returns the result with status 200. Which code snippet correctly implements this?
hard
A. class DoubleView(APIView): def get(self, request): num = request.data.get('number') result = num * 2 return Response({'result': result}, status=200)
B. class DoubleView(APIView): def post(self, request): num = request.data['number'] result = num + num return Response({'result': result}, status=201)
C. class DoubleView(APIView): def post(self, request): num = request.data.get('number') result = num * 2 return Response({'result': result}, status=200)
D. class DoubleView(APIView): def post(self): num = request.data.get('number') result = num * 2 return Response({'result': result}, status=200)

Solution

  1. Step 1: Check method and parameters

    POST method must be defined with self and request parameters. class DoubleView(APIView): def post(self, request): num = request.data.get('number') result = num * 2 return Response({'result': result}, status=200) correctly defines post(self, request).
  2. Step 2: Validate data access and response

    class DoubleView(APIView): def post(self, request): num = request.data.get('number') result = num * 2 return Response({'result': result}, status=200) safely uses request.data.get('number') and multiplies by 2. It returns Response with status=200 as required.
  3. Step 3: Review other options

    class DoubleView(APIView): def post(self, request): num = request.data['number'] result = num + num return Response({'result': result}, status=201) uses status=201 (wrong status). class DoubleView(APIView): def get(self, request): num = request.data.get('number') result = num * 2 return Response({'result': result}, status=200) uses get method instead of post. class DoubleView(APIView): def post(self): num = request.data.get('number') result = num * 2 return Response({'result': result}, status=200) misses request parameter in post method.
  4. Final Answer:

    class DoubleView(APIView): def post(self, request): num = request.data.get('number') result = num * 2 return Response({'result': result}, status=200) -> Option C
  5. Quick Check:

    POST with request param, multiply, status=200 [OK]
Hint: POST method with request param, multiply, return status 200 [OK]
Common Mistakes:
  • Using get method instead of post
  • Missing request parameter in method
  • Returning wrong status code
  • Accessing request.data incorrectly