Bird
0
0

Identify the error in this APIView code snippet:

medium📝 Debug Q14 of 15
Django - REST Framework Basics
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"})
AThe get method is missing the request parameter.
BThe post method should not return a Response.
CThe status code 200 is invalid in Response.
Drequest.data is not accessible in APIView.
Step-by-Step Solution
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]
Quick Trick: Check method parameters: self and request required [OK]
Common Mistakes:
MISTAKES
  • Omitting request parameter in methods
  • Thinking status=200 is invalid
  • Believing request.data is unavailable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes