Bird
0
0

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

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

def my_view(request):
    data = {'name': 'Alice', 'age': 30}
    return JsonResponse(data)
A{"name": "Alice", "age": 30}
Bname=Alice&age=30
C<html>{'name': 'Alice', 'age': 30}</html>
DError: JsonResponse requires a string
Step-by-Step Solution
Solution:
  1. Step 1: Understand JsonResponse behavior

    JsonResponse serializes the dictionary to JSON string and sets content type to application/json.
  2. Step 2: Check the output format

    The output is a JSON string with double quotes, not URL encoded or HTML wrapped.
  3. Final Answer:

    {"name": "Alice", "age": 30} -> Option A
  4. Quick Check:

    JsonResponse outputs JSON string [OK]
Quick Trick: JsonResponse returns JSON string with correct headers [OK]
Common Mistakes:
MISTAKES
  • Expecting URL encoded string instead of JSON
  • Thinking JsonResponse returns HTML
  • Assuming JsonResponse needs string input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes