Complete the code to import the Django JsonResponse class.
from django.http import [1]
The JsonResponse class is used to send JSON data back to the client in Django.
Complete the code to parse JSON data from a POST request in a Django view.
import json def my_view(request): data = json.loads(request.[1].decode('utf-8')) return JsonResponse({'received': data})
The request.body contains the raw bytes of the request payload. We decode it and parse JSON.
Fix the error in the code to return a JSON response with a status code 201.
from django.http import JsonResponse def create_item(request): response = JsonResponse({'message': 'Created'}) response.[1] = 201 return response
The correct attribute to set the HTTP status code on a Django HttpResponse or JsonResponse object is status_code.
Fill both blanks to parse JSON data and return a JsonResponse with a custom status code.
import json from django.http import JsonResponse def update_view(request): data = json.loads(request.[1].decode('utf-8')) return JsonResponse({'updated': data}, status=[2])
Use request.body to get raw data and status=200 to indicate success.
Fill all three blanks to parse JSON from request, extract a field, and return it in JsonResponse.
import json from django.http import JsonResponse def echo_name(request): data = json.loads(request.[1].decode('utf-8')) name = data.get('[2]', 'Guest') return JsonResponse({'name_echo': [3])
We read JSON from request.body, get the 'name' field, and return it in the response.