码迷,mamicode.com
首页 > 其他好文 > 详细

自定义分页与保存搜索条件

时间:2020-01-24 09:20:48      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:bsp   自定义分页   %s   django   ext   imp   boot   play   条件   

技术图片
  1 """
  2 分页组件使用示例:
  3 
  4     obj = Pagination(request.GET.get(‘page‘,1),len(USER_LIST),request.path_info)
  5     page_user_list = USER_LIST[obj.start:obj.end]
  6     page_html = obj.page_html()
  7 
  8     return render(request,‘index.html‘,{‘users‘:page_user_list,‘page_html‘:page_html})
  9 
 10 
 11 """
 12 
 13 
 14 class Pagination(object):
 15 
 16     def __init__(self,current_page,all_count,request,per_page_num=2,pager_count=11):
 17         """
 18         封装分页相关数据
 19         :param current_page: 当前页
 20         :param all_count:    数据库中的数据总条数
 21         :param per_page_num: 每页显示的数据条数
 22         :param pager_count:  最多显示的页码个数
 23         """
 24 
 25         try:
 26             current_page = int(current_page)
 27         except Exception as e:
 28             current_page = 1
 29 
 30         if current_page <1:
 31             current_page = 1
 32 
 33         self.current_page = current_page
 34 
 35         self.all_count = all_count
 36         self.per_page_num = per_page_num
 37 
 38 
 39         # 总页码
 40         all_pager, tmp = divmod(all_count, per_page_num)
 41         if tmp:
 42             all_pager += 1
 43         self.all_pager = all_pager
 44 
 45 
 46         self.pager_count = pager_count
 47         self.pager_count_half = int((pager_count - 1) / 2)
 48 
 49         # 保存搜索条件
 50         import copy
 51         self.params = copy.deepcopy(request.GET)
 52 
 53     @property
 54     def start(self):
 55         return (self.current_page - 1) * self.per_page_num
 56 
 57     @property
 58     def end(self):
 59         return self.current_page * self.per_page_num
 60 
 61     def page_html(self):
 62         # 如果总页码 < 11个:
 63         if self.all_pager <= self.pager_count:
 64             pager_start = 1
 65             pager_end = self.all_pager + 1
 66         # 总页码  > 11
 67         else:
 68             # 当前页如果<=页面上最多显示11/2个页码
 69             if self.current_page <= self.pager_count_half:
 70                 pager_start = 1
 71                 pager_end = self.pager_count + 1
 72 
 73             # 当前页大于5
 74             else:
 75                 # 页码翻到最后
 76                 if (self.current_page + self.pager_count_half) > self.all_pager:
 77                     pager_end = self.all_pager + 1
 78                     pager_start = self.all_pager - self.pager_count + 1
 79                 else:
 80                     pager_start = self.current_page - self.pager_count_half
 81                     pager_end = self.current_page + self.pager_count_half + 1
 82 
 83         page_html_list = []
 84 
 85         first_page = <li><a href="?page=%s">首页</a></li> % (1,)
 86         page_html_list.append(first_page)
 87 
 88         if self.current_page <= 1:
 89             prev_page = <li class="disabled"><a href="#">上一页</a></li>
 90         else:
 91             prev_page = <li><a href="?page=%s">上一页</a></li> % (self.current_page - 1,)
 92 
 93         page_html_list.append(prev_page)
 94 
 95         for i in range(pager_start, pager_end):
 96             self.params[page] = i
 97             if i == self.current_page:
 98                 temp = <li class="active"><a href="?%s">%s</a></li> % (self.params.urlencode(), i,)
 99             else:
100                 temp = <li><a href="?%s">%s</a></li> % (self.params.urlencode(), i,)
101             page_html_list.append(temp)
102 
103         if self.current_page >= self.all_pager:
104             next_page = <li class="disabled"><a href="#">下一页</a></li>
105         else:
106             next_page = <li><a href="?page=%s">下一页</a></li> % (self.current_page + 1,)
107         page_html_list.append(next_page)
108 
109         last_page = <li><a href="?page=%s">尾页</a></li> % (self.all_pager,)
110         page_html_list.append(last_page)
111 
112         return ‘‘.join(page_html_list)
page.py
技术图片
 1 from django.shortcuts import render,HttpResponse
 2 from app01 import models
 3 
 4 # Create your views here.
 5 def index(request):
 6     # li = []
 7     # for i in range(100):
 8     #     li.append(models.Book(name="图书%s"%i,price=10+i))
 9     # models.Book.objects.bulk_create(li)
10     # return HttpResponse(‘ok‘)
11     from app01.page import Pagination
12     current_page_num=request.GET.get(page,1)
13     book_list = models.Book.objects.all()
14     pagination=Pagination(current_page_num,book_list.count(),request)
15     book_list=book_list[pagination.start:pagination.end]
16     return render(request,index.html,locals())
views.py
技术图片
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
 7 </head>
 8 <body>
 9 <ul>
10     {% for book in book_list %}
11     <li>{{ book.name }}---{{ book.price }}</li>
12     {% endfor %}
13 </ul>
14 <nav aria-label="Page navigation">
15   <ul class="pagination">
16     {{ pagination.page_html|safe }}
17   </ul>
18 </nav>
19 
20 </body>
21 </html>
index.html

 

自定义分页与保存搜索条件

标签:bsp   自定义分页   %s   django   ext   imp   boot   play   条件   

原文地址:https://www.cnblogs.com/xuqidong/p/12231766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!