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 %}\n'+
25                                     '                未审核\n'+
26                                     '            {% else %}\n'+
27                                     '                已审核\n'+
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/

 

posted @ 2020-12-15 10:14  史可轩  阅读(643)  评论(0编辑  收藏  举报
3 4