首页 > 代码库 > Django之自定义分页
Django之自定义分页
应用于各页面中的分页实现,实现的结果如下图
1.先自定义一个有关分页的PageInfo类
1 class PageInfo(object): 2 3 def __init__(self,current_page,all_count,per_page,base_url,show_page): 4 """ 5 :param current_page:当前页码 6 :param all_count:数据库总行数 7 :param per_page:每页显示行数 8 :param base_url:模板路径 9 :param show_page:显示的页数 10 """ 11 #确保url中page不为数字也可以显示 12 try: 13 self.current_page = int(current_page) 14 except Exception as e: 15 self.current_page = 1 16 self.per_page = per_page 17 #通过divmod函数获取总页码数 18 a,b = divmod(all_count,per_page) 19 if b: 20 a = a +1 21 self.all_pager = a 22 self.show_page = show_page 23 self.base_url = base_url 24 25 #起始页 26 def start(self): 27 return (self.current_page-1) * self.per_page 28 #尾页 29 def end(self): 30 return self.current_page * self.per_page 31 32 33 def pager(self): 34 # v = "<a href=http://www.mamicode.com/‘/custom.html?page=1‘>12" 35 # return v 36 page_list = [] 37 38 half = int((self.show_page-1)/2) 39 40 # 如果数据总页数 < show_page 41 if self.all_pager < self.show_page: 42 begin = 1 43 stop = self.all_pager + 1 44 # 如果数据总页数 > show_page 45 else: 46 # 如果当前页 <=half,永远显示1,show_page 47 if self.current_page <= half: 48 begin = 1 49 stop = self.show_page + 1 50 else: 51 if self.current_page + half > self.all_pager: 52 begin = self.all_pager - self.show_page + 1 53 stop = self.all_pager + 1 54 else: 55 begin = self.current_page - half 56 stop = self.current_page + half + 1 57 #首页 58 first_page= "<li><a href=http://www.mamicode.com/‘%s?page=%s‘>首页" %(self.base_url,1,) 59 page_list.append(first_page) 60 #上一页,如果为第一页,点击上一页将不跳转 61 if self.current_page <= 1: 62 prev = "<li><a href=http://www.mamicode.com/‘#‘>上一页" 63 else: 64 prev = "<li><a href=http://www.mamicode.com/‘%s?page=%s‘>上一页" %(self.base_url,self.current_page-1,) 65 page_list.append(prev) 66 #生成中间页码跳转链接 67 for i in range(begin,stop): 68 if i == self.current_page: 69 temp = "<li class=‘active‘><a href=http://www.mamicode.com/‘%s?page=%s‘>%s" %(self.base_url,i,i,) 70 else: 71 temp = "<li><a href=http://www.mamicode.com/‘%s?page=%s‘>%s" %(self.base_url,i,i,) 72 page_list.append(temp) 73 #下一页,如果为最后一页,点击将不跳转 74 if self.current_page >= self.all_pager: 75 nex = "<li><a href=http://www.mamicode.com/‘#‘>下一页" 76 else: 77 nex = "<li><a href=http://www.mamicode.com/‘%s?page=%s‘>下一页" %(self.base_url,self.current_page+1,) 78 page_list.append(nex) 79 #尾页 80 last_page = "<li><a href=http://www.mamicode.com/‘%s?page=%s‘>首页" % (self.base_url, self.all_pager) 81 page_list.append(last_page) 82 83 return ‘‘.join(page_list)
2.在views中写方法
1 from utils.pager import PageInfo#导入PageInfo类 2 3 def custom(request): 4 all_count = models.UserInfo.objects.all().count()#查询数据总行数 5 page_info = PageInfo(request.GET.get(‘page‘), all_count, 10, ‘/app01/custom.html‘, 11)#实例化类 6 user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()]#页面内需要显示的信息内容 7 return render(request, ‘custom.html‘, {‘user_list‘: user_list, ‘page_info‘: page_info})#传入模板进行渲染
3.需要渲染的‘custom.html‘文件
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 <link rel="stylesheet" href=http://www.mamicode.com/"/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" /> 7 </head> 8 <body> 9 <h1>用户列表</h1> 10 <ul> 11 {% for row in user_list %} 12 <li>{{ row.name }}</li> 13 {% endfor %} 14 </ul> 15 16 <nav aria-label="Page navigation"> 17 <ul class="pagination"> 18 {{ page_info.pager|safe }} 19 </ul> 20 </nav> 21 </body> 22 </html>
4.添加url关系
1 project文件下:
2 from django.conf.urls import url,include 3 from django.contrib import admin 4 5 urlpatterns = [ 6 url(r‘^app01/‘, include(‘app01.urls‘)), 7 ] 8 9 app01文件下: 10 11 from django.conf.urls import url 12 from django.contrib import admin 13 from app01 import views 14 urlpatterns = [ 15 url(r‘^custom.html$‘, views.custom), 16 ]
Django之自定义分页
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。