Bird
0
0

Given this Django view code, what will be the HTTP response content?

medium📝 component behavior Q13 of 15
Django - REST Framework Basics
Given this Django view code, what will be the HTTP response content?
from django.http import JsonResponse
import json

def my_view(request):
    data = json.loads(request.body)
    result = {"message": f"Hello, {data['name']}!"}
    return JsonResponse(result)

And the client sends JSON body: {"name": "Alice"}
AHello, Alice!
B{"message": "Hello, {data['name']}!"}
C{"message": "Hello, Alice!"}
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Parse JSON from request body

    The code uses json.loads(request.body) to get a Python dict with key 'name' and value 'Alice'.
  2. Step 2: Format message and return JsonResponse

    The message string becomes "Hello, Alice!" and is wrapped in a dict, then sent as JSON response.
  3. Final Answer:

    {"message": "Hello, Alice!"} -> Option C
  4. Quick Check:

    JsonResponse sends formatted JSON string [OK]
Quick Trick: JsonResponse returns JSON string with keys and values [OK]
Common Mistakes:
MISTAKES
  • Thinking JsonResponse returns plain text
  • Confusing string interpolation syntax
  • Expecting raw Python dict as response

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes