django分页

 

个人博客地址

http://www.darkghost.life

 

 

 1 #----------------usage----------------
 2 #arg(request,obj)传入http request和数据库的queryset对象,返回html标签和分页后的queryset对象
 3 class limit:  
 4     def limit(self,res,obj):
 5         limit = int(res.GET.get('limit',1))
 6         pagelimit = int(res.GET.get('pagelimit',30))
 7         startlimit = (limit-1)*pagelimit
 8         endlimit = limit*pagelimit
 9         logdb = obj[startlimit:endlimit]
10         page_count,lastpage_count = divmod(obj.count(),pagelimit) 
11         if lastpage_count:
12             page_count +=1
13         startpage = 1
14         endpage = page_count
15         hrefli=[]
16         pagenumber = [30,50,70,100,200]
17         part1 = '<form style="display: inline;" method="GET" action="%s">每页显示<select name="pagelimit" class = "pagelimit">'%res.path
18         part2li=[]
19         part3 =     '<input type="submit" value = "确定"></form>'
20         for number in pagenumber:
21             if number == pagelimit:
22                 option = '<option selected>%s</option>'%number
23             else:
24                 option = '<option>%s</option>'%number
25             part2li.append(option)
26         part2 = ''.join(part2li)
27         pagesize = part1+part2+part3
28         hrefli.append(pagesize)
29         if limit !=1:
30             hrefli.append('<a class="page" href="%s?limit=%s&pagelimit=%s">%s</a>'%(res.path,limit-1,pagelimit,'上一页'))
31         for x in range(startpage,endpage+1):
32             if limit+3 < x or x <limit -3:
33                 hrefli.append('<a class="page pitch hidden" href="%s?limit=%s&pagelimit=%s">%s</a>'%(res.path,x,pagelimit,x))
34             elif x == limit:
35                 hrefli.append('<a class="page pitch" href="%s?limit=%s&pagelimit=%s">%s</a>'%(res.path,x,pagelimit,x))
36             else:
37                 hrefli.append('<a class="page unpitch" href="%s?limit=%s&pagelimit=%s">%s</a>'%(res.path,x,pagelimit,x))
38         if limit != endpage:
39             hrefli.append('<a class="page" href="%s?limit=%s&pagelimit=%s">%s</a>'%(res.path,limit+1,pagelimit,'下一页'))
40         href=''.join(hrefli)
41         return href,logdb

 

posted @ 2021-09-06 15:14  无限's-blog  阅读(36)  评论(0编辑  收藏  举报