from django.http import JsonResponse
def test_json1(request):
user_dict = {'user': '王', 'password': 123456}
# JsonResponse 的传值方式 将json.dumps()中的参数用字典方式传送
return JsonResponse(user_dict, json_dumps_params={'ensure_ascii': False})
def test_json2(request):
ls = ["a","b","c"]
# JsonResponse 的传值方式 将json.dumps()中的参数用字典方式传送
# 非字典序列必须设置参数 safe=False
return JsonResponse(ls, json_dumps_params={'ensure_ascii': False}, safe=False)
关于为什么将list
对象传入JsonResponse
时要加上safe=False
,django4.1的官网说明如下:
Warning
Before the 5th edition of ECMAScript it was possible to poison the JavaScript Array constructor. For this reason, Django does not allow passing non-dict objects to the JsonResponse constructor by default. However, most modern browsers implement ECMAScript 5 which removes this attack vector. Therefore it is possible to disable this security precaution.