weixin_33717298 2015-02-18 04:27 采纳率: 0%
浏览 40

Ajax + Jquery + Django

I try use ajax in Django, but not work the table fields.

I need return

firstname, lastname

but the most I got was the entire json result

See my complete code https://github.com/rg3915/ajax_test

views.py

def person_list_ajax(request):
    return render(request, 'core/person_list_ajax.html')


def get_person(request):
    persons = Person.objects.all()  # .filter(id=1)
    # , fields=('firstname', 'lastname'))
    items = serializers.serialize('json', persons)
    result = json.dumps(items)
    return HttpResponse(result)  # , content_type="application/json")

template: person_list_ajax.html

<script type="text/javascript">
    $(document).ready(function(){
        $("#btnPersons").click(function(){
            $.ajax({
                type: "GET",
                url: "/get_person/",
                dataType: "json",
                success: function(result){
                    // alert(result);
                    // var result = {"model": "core.person", "fields": {"lastname": "Johnson", "firstname": "Dawn"}, "pk": 1};
                    /* $.each(result,function(i,item){
                        alert(i + ":" + item)
                    }); */

                    /* $.each(result, function(i, item){ */
                        $("#corpoTabela").append('<tr>' +
                        '<td>' +
                        result +
                        '</td>' +
                        '</tr>');
                    // });
                }
            });
        });
    });
</script>

I try for return each values, but not work

                $.each(result, function(i, item){
                    $("#corpoTabela").append('<tr>' +
                    '<td>' +
                    result +
                    '</td>' +
                    '</tr>');
                });

And if try result.firstname return 'undefined'.

Question: how to return each values 'firstname' and 'lastname' of json?

See json result (incomplete - is long)

[{"model": "core.person", "fields": {"lastname": "Griffin", "created_at": "2015-01-04T00:15:15.253Z", "firstname": "Aaron", "phone": "(75) 3184-2917", "blocked": false, "birthdate": "1994-03-15T23:03:31.115Z", "modified_at": "2015-01-04T00:15:15.253Z", "cpf": "43024276069", "email": "a.griffin@email.com"}, "pk": 84}, {"model": "core.person", "fields": {"lastname": "Clary", "created_at": "2015-01-04T00:15:15.248Z", "firstname": "Alexis", "phone": "(76) 1129-3183", "blocked": false, "birthdate": "1993-08-09T06:48:49.647Z", "modified_at": "2015-01-04T00:15:15.248Z", "cpf": "68959775006", "email": "a.clary@email.com"}, "pk": 82}, {"model": "core.person", "fields": {"lastname": "Smith", "created_at": "2015-01-04T00:15:15.246Z", "firstname": "Alfonso", "phone": "(23) 1054-7766", "blocked": false, "birthdate": "1997-06-23T19:15:15.627Z", "modified_at": "2015-01-04T00:15:15.246Z", "cpf": "68527495071", "email": "a.smith@email.com"}, "pk": 79}, {"model": "core.person", "fields": {"lastname": "West", "created_at": "2015-01-04T00:15:14.870Z", "firstname": "Angeline", "phone": "(34) 5708-6947", "blocked": true, "birthdate": "1987-11-01T21:12:06.034Z", "modified_at": "2015-01-04T00:15:14.870Z", "cpf": "86925336118", "email": "a.west@email.com"}, "pk": 48}, {"model": "core.person", "fields": {"lastname": "Bonilla", "created_at": "2015-01-04T00:15:15.089Z", "firstname": "Ann", "phone": "(58) 7408-7824", "blocked": false, "birthdate": "1990-09-15T09:05:56.708Z", "modified_at": "2015-01-04T00:15:15.089Z", "cpf": "95614240530", "email": "a.bonilla@email.com"}, "pk": 70}, {"model": "core.person", "fields": {"lastname": "Kim", "created_at": "2015-01-04T00:15:14.832Z", "firstname": "Antoine", "phone": "(54) 1458-8935", "blocked": false, "birthdate": "2002-03-05T12:53:11.508Z", "modified_at": "2015-01-04T00:15:14.832Z", "cpf": "62622224142", "email": "a.kim@email.com"}, "pk": 46}, {"model": "core.person", "fields": {"lastname": "Chilton", "created_at": "2015-01-04T00:15:15.026Z", "firstname": "Arthur", "phone": "(88) 1659-3588", "blocked": true, "birthdate": "2000-03-22T10:07:24.715Z", "modified_at": "2015-01-04T00:15:15.026Z", "cpf": "28367921465", "email": "a.chilton@email.com"}, "pk": 59}, {"model": "core.person", "fields": {"lastname": "Lee", "created_at": "2015-01-

  • 写回答

2条回答 默认 最新

  • 北城已荒凉 2015-02-18 04:49
    关注

    This is how I do it.

    Add all of this after the success in your ajax load:

    success: function(result){
        // create an empty array
        var items = []
    
        // create table headers
        items.push("<tr><th>Last Name, First Name</th></tr>");
    
        // populate each table row
        $.each( results.fields, function( key, val ) {
            items.push('<tr><td>' + val.lastname + ', ' + val.firstname + '</td></tr>');
    
        // load results into a new table in the #corpoTabela div
        $( "<table>", { html: items.join( "" ) }).appendTo( '#corpoTabela' );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器