在Django中创建UTF-8 JsonResponse [英] Creating UTF-8 JsonResponse in Django

查看:443
本文介绍了在Django中创建UTF-8 JsonResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何简单的方法来覆盖 DjangoJSONEncoder.ensure_ascii 并将其设置为 False 或输出非ascii文本在 django.http.JsonResponse 中以任何其他方式?

解决方案

编辑或者如果您倾向于使用utf-8格式,请使用Django的JsonResponse():



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



或者

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

更多关于 safe = False HERE






旧:



你没有任何改变。



尽管Django以ASCII(从UTF-8)创建JSON数据,但JavaScript将自动将其解码为UTF-8。


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?

解决方案

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.

这篇关于在Django中创建UTF-8 JsonResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆