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"}?
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)
Look at how the JSON body is parsed and how the 'name' key is used.
The view loads the JSON from request.body, extracts the 'name' key, and returns a greeting with that name. Since the POST body contains {"name": "Alice"}, the greeting is "Hello, Alice!".
Which of the following code snippets correctly parses JSON data from a Django request object?
Remember that request.body is a byte string and you need to decode it properly.
In Django, request.body contains the raw bytes of the request body. Using json.loads(request.body.decode('utf-8')) correctly parses the JSON. The other options are invalid because Django's request.POST is for form data, and request has no json() method. Also, json.parse does not exist in Python.
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?
from django.http import JsonResponse def my_view(request): data = {'count': 5, 'items': [1, 2, 3]} return JsonResponse(data, safe=False)
Check the meaning of the safe parameter in JsonResponse.
The safe parameter in JsonResponse must be set to False only when the top-level object is not a dict (e.g., a list). Since data is a dict here, safe=False is unnecessary but does not cause an error. Actually, this code runs fine. The question is tricky because the code does not raise a TypeError. The correct answer is that safe=False is only needed for non-dict objects.
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?
from django.http import JsonResponse def error_view(request): return JsonResponse({'error': 'Not found'}, status=404)
Look at the status parameter in JsonResponse.
The status parameter sets the HTTP status code of the response. Here it is explicitly set to 404, so the response will have status code 404.
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)?
Think about popular Django extensions for APIs.
Django itself does not automatically parse JSON request bodies. However, Django REST Framework (DRF) provides a Request object that parses JSON and other content types automatically, making the data available as request.data. The other options either do not parse JSON automatically or are unrelated.