Bird
0
0

What is wrong with this Django view?

medium📝 Debug Q7 of 15
Django - REST Framework Basics
What is wrong with this Django view?
from django.http import JsonResponse

def view(request):
    data = {'message': 'Hello'}
    return JsonResponse(data, safe=False)
AMissing json.dumps call before JsonResponse
BJsonResponse cannot send dictionaries
Csafe=False is incorrect when sending a dictionary
Dsafe=False is required for dictionaries
Step-by-Step Solution
Solution:
  1. Step 1: Understand safe parameter in JsonResponse

    safe=False allows sending non-dict objects like lists; dicts require safe=True or default.
  2. Step 2: Check usage with dictionary

    Sending a dictionary with safe=False is unnecessary and may cause confusion.
  3. Final Answer:

    safe=False is incorrect when sending a dictionary -> Option C
  4. Quick Check:

    safe=False only for non-dict JSON data [OK]
Quick Trick: Use safe=False only when sending non-dict JSON data [OK]
Common Mistakes:
MISTAKES
  • Using safe=False with dictionaries
  • Thinking JsonResponse needs json.dumps input
  • Confusing safe parameter purpose

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes