import json from django.http import HttpResponse MIMEANY = '*/*' MIMEJSON = 'application/json' MIMETEXT = 'text/plain' def response_mimetype(request): can_json = MIMEJSON in request.META['HTTP_ACCEPT'] can_json |= MIMEANY in request.META['HTTP_ACCEPT'] return MIMEJSON if can_json else MIMETEXT class JSONResponse(HttpResponse): def __init__(self, obj='', json_opts=None, mimetype=MIMEJSON, *args, **kwargs): json_opts = json_opts if isinstance(json_opts, dict) else {} content = json.dumps(obj, **json_opts) super().__init__(content, mimetype, *args, **kwargs)