0
0
Djangoframework~20 mins

Request parsing and response rendering in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Request Parsing and Response Rendering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django view when receiving a POST request with JSON data?

Consider this Django view that parses JSON data from a POST request and returns a JSON response:

from django.http import JsonResponse
import json

def my_view(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        name = data.get('name', 'Guest')
        return JsonResponse({'greeting': f'Hello, {name}!'})
    return JsonResponse({'error': 'Only POST allowed'}, status=405)

What will be the JSON response body if the POST request body is {"name": "Alice"}?

Django
from django.http import JsonResponse
import json

def my_view(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        name = data.get('name', 'Guest')
        return JsonResponse({'greeting': f'Hello, {name}!'})
    return JsonResponse({'error': 'Only POST allowed'}, status=405)
A{"greeting": "Hello, Alice!"}
B{"greeting": "Hello, Guest!"}
C{"error": "Only POST allowed"}
DSyntaxError
Attempts:
2 left
💡 Hint

Look at how the JSON body is parsed and how the 'name' key is used.

📝 Syntax
intermediate
1:30remaining
Which option correctly parses JSON data in a Django view?

Which of the following code snippets correctly parses JSON data from a Django request object?

Adata = request.json()
Bdata = request.POST.json()
Cdata = json.loads(request.body.decode('utf-8'))
Ddata = json.parse(request.body)
Attempts:
2 left
💡 Hint

Remember that request.body is a byte string and you need to decode it properly.

🔧 Debug
advanced
2:00remaining
Why does this Django view raise a TypeError when returning JsonResponse?

Examine this Django view:

from django.http import JsonResponse

def my_view(request):
    data = {'count': 5, 'items': [1, 2, 3]}
    return JsonResponse(data, safe=False)

Why does this code raise a TypeError?

Django
from django.http import JsonResponse

def my_view(request):
    data = {'count': 5, 'items': [1, 2, 3]}
    return JsonResponse(data, safe=False)
ABecause safe=False is required only when returning a list, not a dict
BBecause safe=False is used with a dict, which is not allowed
CBecause JsonResponse cannot serialize lists inside dicts
DBecause the data dict is missing a status code
Attempts:
2 left
💡 Hint

Check the meaning of the safe parameter in JsonResponse.

state_output
advanced
1:00remaining
What is the HTTP status code of this Django JsonResponse?

Given this Django view:

from django.http import JsonResponse

def error_view(request):
    return JsonResponse({'error': 'Not found'}, status=404)

What HTTP status code will the response have?

Django
from django.http import JsonResponse

def error_view(request):
    return JsonResponse({'error': 'Not found'}, status=404)
A500
B404
C200
D400
Attempts:
2 left
💡 Hint

Look at the status parameter in JsonResponse.

🧠 Conceptual
expert
2:30remaining
Which Django feature automatically parses JSON request bodies in views?

In Django, which feature or tool automatically parses JSON request bodies and provides the data as a Python dictionary without manually calling json.loads(request.body)?

AUsing Django's HttpRequest.POST attribute
BUsing Django's middleware for JSON parsing
CUsing Django's built-in JsonResponse
DUsing Django REST Framework's Request object
Attempts:
2 left
💡 Hint

Think about popular Django extensions for APIs.