首页 > Web开发 > 详细

[django]django 3种返回json方法

时间:2018-08-24 00:09:55      阅读:206      评论:0      收藏:0      [点我收藏+]

django 3种返回json方法

from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from app01.models import Book


# Create your views here.

def get_book(request):
    all_book = Book.objects.all()
    d = []
    for i in all_book:
        d.append({'name': i.name})
    return JsonResponse(d, safe=False)


def get_book2(request):
    from django.forms.models import model_to_dict
    all_book = Book.objects.all()
    d = []
    for i in all_book:
        d.append(model_to_dict(i))
    return JsonResponse(d, safe=False)


def get_book3(request):
    from django.core.serializers import serialize
    d = serialize('json', Book.objects.all())

    # return HttpResponse(d)
    return HttpResponse(d)

[django]django 3种返回json方法

原文:https://www.cnblogs.com/iiiiiher/p/9527083.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!