• django+ajax实现简单的查询页面局部更新


    views.py


     1 def selects(request):
     2     if request.method =='POST':
     3         l = request.POST.get('l')
     4         s = NewsInfo.objects.filter(tid_id=l)
     5         list = []
     6         for i in s:
     7             data = {
     8                 'id':i.id,
     9                 'title':i.nTitle,
    10                 'author':i.nAuthor,
    11                 'content':i.nContent,
    12                 'time':i.nPubDateTime,
    13                 'ns':i.NStatus,
    14                 'typp':i.tid.tname
    15             }
    16             list.append(data)
    17             # print(list)
    18             # print(JsonResponse(list,safe=False))
    19 
    20         return JsonResponse(list,safe=False)

    urls.py


        path('selects/', views.selects,name = 'selects'),

    index.html


     1     function b() {
     2         var nums = $('#select').val()
     3         {#console.log(nums);#}
     4         if (nums.length !== 0) {
     5             $.ajax({
     6                 url: '{% url 'selects' %}',
     7                 type: 'post',
     8                 data: ({
     9                     'l': nums
    10                 }),
    11                 success: function (data) {
    12                     if (data == null || data == '') {
    13                         alert("没有该数据");
    14                     } else {
    15 
    16                         var html = ""
    17                         for (var i = 0; i < data.length; i++) {
    18                             html += '<tr><td>' + '<input type="checkbox" name="hh" value="{{ all.id }}">' + '</td>' +
    19                                 '<td>' + data[i].id + '</td>' +
    20                                 '<td>' + data[i].title + '</td>' +
    21                                 '<td>' + data[i].author + '</td>' +
    22                                 '<td>' + data[i].content + '</td>' +
    23                                 '<td>' + data[i].time + '</td>' +
    24                                 '<td>' + '{% if all.NStatus == True %}
    '+
    25                                     '                未审核
    '+
    26                                     '            {% else %}
    '+
    27                                     '                已审核
    '+
    28                                     '            {% endif %}' + '</td>' +
    29                                 '<td>' + data[i].typp + '</td></tr>'
    30 
    31 
    32                         }
    33                         $('#texts').html(html)
    34                     }
    35 
    36 
    37                 },
    38                 error: function () {
    39                     alert('查询失败')
    40                 }
    41             })
    42         } else {
    43 
    44         }
    45     }
    46 
    47 </script>

    详情请访问私人博客:http://skx.pythonanywhere.com/

    个人小站,欢迎访问!http://klvr.xyz
  • 相关阅读:
    HBase 负载均衡
    HBase的写事务,MVCC及新的写线程模型
    HBase RegionServer宕机处理恢复
    分布式事务实现-Percolator
    MVC框架
    06-JS中li移动第二种形式
    05-JS中li移动第一种形式
    04-JS中文档碎片
    03-JS中添加节点
    02-JS中父节点
  • 原文地址:https://www.cnblogs.com/xxks/p/14137051.html
Copyright © 2020-2023  润新知