Django check: Use JsonResponse

Use the built-in JsonResponse class, which is a subclass of HttpResponse.


JsonResonse is a HttpResponse subclass that helps to create a JSON-encoded response. It inherits most behavior from its superclass with a couple differences. Its default Content-Type header is set to application/json, and it comes with a default JSON encoder, so you don't have to serialize the data before returning the response.

This check will alert on these cases, for example:

from django.http import HttpResponse
import json

def foo():
    dump = json.dumps({})
    # use JsonDumps(dump) instead
    return HttpResponse(dump, content_type='application/json')

def foo1():
    # similar to above
    dump = json.dumps({})
    x = HttpResponse(dump, content_type='application/json')


  1. Django documentation