Junior — Middle
Is it possible to send only a pure JSON response using the Django framework?
sobes.tech AI
Answer from AI
Yes, in Django, you can send a plain JSON response. For this, the JsonResponse class from django.http module is used. It automatically serializes the given dictionary or list into JSON and sets the correct Content-Type: application/json header.
Example:
from django.http import JsonResponse
def my_view(request):
data = {'message': 'Hello, world!'}
return JsonResponse(data)
This response will contain only JSON without HTML or other wrappers.