int_ua int_ua - 3 months ago 51
JSON Question

Creating UTF-8 JsonResponse in Django

Is there any simple way to override

DjangoJSONEncoder.ensure_ascii
and set it to
False
or output non-ascii text in
django.http.JsonResponse
in any other way?

Answer

EDIT:

Or if you tend to the utf-8 format, use instead of Django's JsonResponse():

return HttpResponse(json.dumps(response_data, ensure_ascii=False), content_type="application/json")

or

return JsonResponse(json.dumps(response_data, ensure_ascii=False), safe=False)

more about the safe=False HERE


OLD:

You don't have to whatever alter.

Although Django creates JSON data in ASCII (from UTF-8), Javascript will automatically decode it back to UTF-8.