Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Django's Generic Class Views, the method for sending a JSON-formatted HTTP response is by using the JsonResponse class.

Here is an example of how to use it in a view:

from django.http import JsonResponse
from django.views.generic import View

class MyView(View):
    def get(self, request, *args, **kwargs):
        data = {'message': 'Hello, World!'}
        return JsonResponse(data)

In this example, we have defined a simple view called MyView that returns a JSON response containing a message. The JsonResponse class takes a Python dictionary and returns a JSON-encoded HTTP response with the specified content-type.