doctorsong

分页函数未封装源码

size = 每页展示的条数。

plus=当前页展示的前几页后几页一起展示。


from django.utils.safestring import mark_safe



def
numlist(request): # for i in range(1,100): # a = "1804539"+str(random.randint(1000,9999)) # models.prettyNum.objects.create(mobile= a,price=998, level=1,status=1) search_dic = {} search = request.GET.get("query",'') if search: search_dic["mo5bile__contains"]= search queryset = models.prettyNum.objects.filter(**search_dic)



"""分页逻辑""" page
= int(request.GET.get("page",1)) size =10 start =(page-1)*size+1 end = page*size +1 queryset = models.prettyNum.objects.all()[start:end]#核心所在 total_count = models.prettyNum.objects.all().count()#计算一共有多少条数据 total_page,div = divmod(total_count,size)#计算一共可以分几页 if div: total_page += 1 plus= 5 if total_page <= 2*plus+1: start_page = 1 end_page=total_page else: if page-plus<=1: start_page = 1 end_page = 2*plus+1 else: if page+plus>=total_page: start_page = total_page-2*plus end_page = total_page else: start_page=page-plus end_page =page+plus page_str_list =[] """首页""" page_str_list.append("<li><a href='?page={}'>首页</a></li>".format(1)) """上一页""" if page-1 <= 0: pre ='''<li> <a href="?page={}" aria-label="Previous"> <span aria-hidden="true">&laquo;</span> </a> </li>'''.format(1) else: pre = '''<li> <a href="?page={}" aria-label="Previous"> <span aria-hidden="true">&laquo;</span> </a> </li>'''.format(page-1) page_str_list.append(pre) for i in range(start_page,end_page+1):#控制显示分页标签的数量 if page == i: ele = '<li class="active"><a href="?page={}">{}</a></li>'.format(i,i) else: ele = '<li ><a href="?page={}">{}</a></li>'.format(i, i) page_str_list.append(ele) """下一页""" if page +1 >=total_page: post = '''<li> <a href="?page={}" aria-label="Next"> <span aria-hidden="true">&raquo;</span> </a> </li>'''.format(total_page) else: post = '''<li> <a href="?page={}" aria-label="Next"> <span aria-hidden="true">&raquo;</span> </a> </li>'''.format(page+1) page_str_list.append(post) """尾页""" page_str_list.append("<li><a href='?page={}'>尾页</a></li>".format(total_page)) page_str = mark_safe("".join(page_str_list)) return render(request,"numlist.html", {"queryset":queryset,"q":search,"page_str":page_str}

前端应用

<div class="text-center">
                <nav aria-label="Page navigation">
                    <ul class="pagination">

                        {{ page_str }}

                    </ul>
                </nav>
</div>

 

posted on 2023-05-08 15:17  songyu666  阅读(6)  评论(0编辑  收藏  举报

导航