0
0
Djangoframework~10 mins

Returning JSON with JsonResponse in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the correct class for returning JSON responses in Django.

Django
from django.http import [1]
Drag options to blanks, or click blank then click option'
AHttpResponse
BJsonResponse
CHttpRequest
DJsonRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using HttpResponse instead of JsonResponse.
Confusing JsonResponse with HttpRequest.
2fill in blank
medium

Complete the code to return a JSON response with a dictionary containing a message.

Django
def my_view(request):
    data = {'message': 'Hello, world!'}
    return [1](data)
Drag options to blanks, or click blank then click option'
Aredirect
BHttpResponse
CJsonResponse
Drender
Attempts:
3 left
💡 Hint
Common Mistakes
Using HttpResponse without setting content type.
Using render which is for HTML templates.
3fill in blank
hard

Fix the error in the code to correctly return JSON with safe=False for non-dict data.

Django
def my_view(request):
    data = ['apple', 'banana', 'cherry']
    return JsonResponse(data, [1]=False)
Drag options to blanks, or click blank then click option'
Asafe
Bjson_safe
Callow_non_dict
Dsafe_mode
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like json_safe or safe_mode.
Not setting safe=False when returning a list.
4fill in blank
hard

Fill both blanks to create a JSON response with a custom status code and headers.

Django
def my_view(request):
    data = {'status': 'ok'}
    return JsonResponse(data, status=[1], headers=[2])
Drag options to blanks, or click blank then click option'
A201
B404
C{'X-Custom-Header': 'Value'}
D{'Content-Type': 'application/json'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using status codes like 404 for success.
Passing headers as a string instead of a dictionary.
5fill in blank
hard

Fill all three blanks to return a JSON response with a nested dictionary, custom status, and safe=False.

Django
def my_view(request):
    data = [1]
    return JsonResponse(data, status=[2], safe=[3])
Drag options to blanks, or click blank then click option'
A{'items': ['a', 'b', 'c']}
B202
CFalse
D['a', 'b', 'c']
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list directly without safe=False.
Using incorrect status codes.
Not matching data type with safe parameter.