A. HttpResponse cannot have status_code set manually
B. status_code must be an integer, not a string
C. Missing return statement
D. HttpResponse requires a JSON object
Solution
Step 1: Check the status_code assignment
Status codes must be integers like 404, not strings like '404'.
Step 2: Confirm HttpResponse usage
HttpResponse allows setting status_code manually but it must be int.
Final Answer:
status_code must be an integer, not a string -> Option B
Quick Check:
status_code = int, not string [OK]
Hint: status_code must be int, not string [OK]
Common Mistakes:
Using string for status_code
Thinking HttpResponse forbids status_code
Forgetting to return response
5. You want to create a Django view that processes a POST request with JSON data and returns a JSON response with a message and the original data. Which code snippet correctly handles this?
hard
A. def view(request):
data = request.GET
return JsonResponse({'message': 'Received', 'data': data})
B. def view(request):
data = request.POST
return HttpResponse({'message': 'Received', 'data': data})
C. def view(request):
data = request.body
return JsonResponse(data)
D. def view(request):
data = json.loads(request.body)
return JsonResponse({'message': 'Received', 'data': data})
Solution
Step 1: Parse JSON from POST request body
request.body contains raw bytes; json.loads converts it to a Python dict.
Step 2: Return JsonResponse with message and data
JsonResponse correctly serializes the Python dict to JSON for response.
Step 3: Check other options
def view(request):
data = request.POST
return HttpResponse({'message': 'Received', 'data': data}) wrongly uses HttpResponse with dict, def view(request):
data = request.GET
return JsonResponse({'message': 'Received', 'data': data}) uses GET instead of POST, def view(request):
data = request.body
return JsonResponse(data) returns raw bytes without parsing.
Final Answer:
def view(request):
data = json.loads(request.body)
return JsonResponse({'message': 'Received', 'data': data}) -> Option D
Quick Check:
Parse JSON body, respond with JsonResponse [OK]
Hint: Parse JSON from request.body, respond with JsonResponse [OK]
Common Mistakes:
Using request.POST for JSON body
Returning dict in HttpResponse
Not parsing JSON before response
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.