Django的序列化_通过ajax方式

Django序列化方式
  a 对象
  b 字典
  c 元祖




代码是元祖的形式序列化


from
django.shortcuts import render,HttpResponse from app01 import models # Create your views here. import json def xuliehua(request): return render(request,'xuliehua.html') def get_data(request): ret={"status":True,"msg":None} try: obj = models.UserInFo.objects.values('id','username','email') ret['msg']=list(obj) print(ret) except Exception as e: ret['status':False] return HttpResponse(json.dumps(ret))
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>用户列表</h1>

<p  class="ab"></p>


<script src="/static/jquery-3.3.1.js"></script>
<script>
    $(function () {
        ajaxData()
    })

    function ajaxData() {
        $.ajax({
            url:'/get_data/',
            type:'post',
            dataType:'JSON',
            success:function (arg) {
              if (arg.status == true){
                    $('.ab').text(arg.msg)
                    $.each(arg.msg,function (i,j) {
                    console.log(j)
                    })

                    console.log('得到数据')
                }
                else {

                    console.log("未能得到数据")
                }

            }

        })

    }

</script>

</body>
</html>

猜你喜欢

转载自www.cnblogs.com/lhqlhq/p/9184575.html