[django]JsonResponse序列化数据

def home(request):
    data = {
        'name': 'maotai',
        'age': 22
    }
    import json
    return HttpResponse(json.dumps(data), content_type='application/json', status=400)


def home2(request):
    data = {'name': 'maotai', 'age': 23}
    return JsonResponse(data, safe=True)

JsonResponse(继承HttpResponse)基于JsonResponse做了一层封装,返回数据浏览器直接json mime显示.
1,data.dumps
2,加上了content_type

3,JsonResponse支持序列化列表,字典.

def home2(request):
    arr = [1,2,3]
    return JsonResponse(data, safe=False) # safe值默认是true, 如果是列表,则safe置为False, 如果是字典为true.
posted @ 2018-04-23 23:03  mmaotai  阅读(327)  评论(0编辑  收藏  举报