django HttpResponse的用法

一、传json字典

def back_json(rquest):
    #JsonResponse父类是HttpResponse,原码里调用了json.dumps()
    from django.http import JsonResponse
    back_msg = {'name':name,'age':123}
    return JsonResponse(back_msg)

二、传列表

def back_json(rquest):
    #JsonResponse父类是HttpResponse,原码里调用了json.dumps()
    from django.http import JsonResponse
    back_list = [1,2,3]
    #JsonResponse默认传字典,传列表的话需要指定safe=False
    return JsonResponse(back_list,safe=False)

 

转载于:https://www.cnblogs.com/di2wu/p/10068528.html

你可能感兴趣的:(django HttpResponse的用法)