Django中视图总结[urls匹配,HttpRequest对象,HttpResponse对象,对象序列化接受及案例]

技术分享
def get_book(request):
    """获取图书信息的视图"""
    books = BookInfo.objects.all()
    # 对查询集进行序列化
    books = serializers.serialize(json, books)
    return JsonResponse({"books": books})

视图Code
技术分享技术分享
!DOCTYPE html
html lang="en"
head
    meta charset="UTF-8"
    titleTitle/title
    script src="/upload/attach/blank.gif"  data-echo="/static/js/jquery-1.12.4.min.js"/script
    script
        $(function () {
            $.get(‘/get_book1/‘,function (data) {
                //将字符串转成json对象
                books = $.parseJSON(data.books)

                content = ‘‘
                //遍历其中的个对象获取属性值
                content += ‘tr‘

                $.each(books[0].fields, function (key, val) {
                    content += ‘td‘+key+‘/td‘

                })
                content += ‘/tr‘
                //便利打印出每个每个对象
                $.each(books, function (index, data) {
                    content += ‘tr‘
                    //遍历打印每个里面的对象fields的值
                    $.each(data.fields, function (key, val) {
                        content += ‘td‘+val+‘/td‘
                    })
                    content += ‘/tr‘

                })
                $(‘#tab‘).append(content)
            })


        })


    /script
/head
body
table/table
/body
/html

模板code

Django中视图总结[urls匹配,HttpRequest对象,HttpResponse对象,对象序列化接受及案例]

原文地址:http://www.cnblogs.com/kayb/p/7255987.html


最新回复(0)
/jishuJmZQ0VjA6sft3ddxawFbnq0MRl36BNFOjNXqDM3rcd4_3D4719394
8 简首页