• 自定义分页


     1 class Pagination:
     2     def __init__(self, current_page_num, all_count, request, per_page_num=10, pager_count=5):
     3         """
     4 
     5         :param current_page_num: 当前页码数
     6         :param all_count: 分页数据总条数
     7         :param request:
     8         :param per_page_num: 每页显示的数据条数
     9         :param pager_count: 最多显示的页码个数
    10         """
    11         try:
    12             current_page_num = int(current_page_num)
    13         except Exception as e:
    14             current_page_num = 1
    15         if current_page_num < 1:
    16             current_page_num = 1
    17         self.current_page_num = current_page_num
    18         self.all_count = all_count
    19         self.per_page_num = per_page_num
    20 
    21         # 实际总页码
    22         all_pager, tmp = divmod(all_count, per_page_num)
    23         if tmp:
    24             all_pager += 1
    25         self.all_pager = all_pager
    26 
    27         self.pager_count = pager_count
    28         self.pager_count_half = int((pager_count - 1) / 2)
    29         import copy
    30         self.params=copy.deepcopy(request.GET)
    31 
    32 
    33     @property
    34     def start(self):
    35         return (self.current_page_num - 1) * self.per_page_num
    36 
    37     @property
    38     def end(self):
    39         return self.current_page_num * self.per_page_num
    40 
    41     def page_html(self):
    42         # 如果总页数<11:
    43         if self.all_pager < self.pager_count:
    44             pager_start = 1
    45             pager_end = self.all_pager + 1
    46         # 总页码>11
    47         else:
    48             # 当前页码<=页面上最多显示11/2个页码
    49             if self.current_page_num <= self.pager_count_half:
    50                 pager_start = 1
    51                 pager_end = self.pager_count + 1
    52 
    53             else:
    54                 # 当前页>总页数
    55                 if (self.current_page_num + self.pager_count_half) > self.all_pager:
    56                     pager_start = self.all_pager - self.pager_count + 1
    57                     pager_end = self.all_pager + 1
    58                 # # 当前页<总页数
    59                 else:
    60                     pager_start = self.current_page_num - self.pager_count_half
    61                     pager_end = self.current_page_num + self.pager_count_half + 1
    62         page_html_list = []
    63         first_page = '<li><a href="?page=%s">首页</a></li>' % (1,)
    64         page_html_list.append(first_page)
    65         # 当前页码是否小于1
    66         if self.current_page_num <= 1:
    67             prev_page = '<li class="disabled"><a href="#">上一页</a></li>'
    68         else:
    69             prev_page = '<li><a href="?page=%s">上一页</a></li>' % (self.current_page_num - 1,)
    70         page_html_list.append(prev_page)
    71         # 循环添加a标签
    72         for i in range(pager_start, pager_end):  #
    73             self.params['page']=i
     1 from django.shortcuts import render, HttpResponse
     2 from app01.models import Book
     3 
     4 
     5 # Create your views here.
     6 def index(request):
     7     from app01.page import Pagination
     8     # 取到当前页码
     9     current_page_num = request.GET.get('page')
    10     # 取到所有书籍
    11     book_list = Book.objects.all()
    12     # 实例化一个对象
    13     pagination = Pagination(current_page_num, book_list.count(), request, )
    14     # 切片选中
    15     book_list=book_list[pagination.start:pagination.end]
    16 
    17 
    18     return render(request,'INDEX.html',locals())
    
    
     1 <!DOCTYPE html>
     2 <html lang="zh-CN">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6     <meta name="viewport" content="width=device-width, initial-scale=1">
     7     <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
     8 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
     9 </head>
    10 <body>
    11 
    12 <ul>
    13     {% for book in book_list %}
    14     <li>{{ book.title }} ---- {{ book.price }}</li>
    15     {% endfor %}
    16 </ul>
    17 
    18 <nav aria-label="Page navigation">
    19   <ul class="pagination">
    20    {{ pagination.page_html|safe }}
    21   </ul>
    22 </nav>
    23 
    24 
    25 </body>
    26 </html>
    74             # 如果当前循环元素是当前页码给当前页码加css
    75             if i == self.current_page_num:
    76                 temp = '<li class="active"><a href="?%s">%s</a></li>' % (self.params.urlencode(), i)
    77             else:
    78                 temp = '<li><a href="?%s">%s</a></li>' % (self.params.urlencode(), i)
    79             page_html_list.append(temp)
    80         if self.current_page_num >= self.all_pager:
    81             next_page = '<li class="disabled"><a href="#">下一页</a></li>'
    82         else:
    83             next_page = '<li><a href="?page=%s">下一页</a></li>' % (self.current_page_num + 1,)
    84         page_html_list.append(next_page)
    85         last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.all_pager,)
    86         page_html_list.append(last_page)
    87 
    88         return ''.join(page_html_list)
  • 相关阅读:
    vsftpd的主动模式与被动模式
    Linux环境下vsftpd参数配置
    CentOS下的网络配置文件说明
    第一篇博客,随笔留念
    asp.net xml 增删改操作
    asp.net json 与xml 的基础事例
    linq 之 Distinct的使用
    【P2015】二叉苹果树(树状DP)
    【P2016】战略游戏(贪心||树状DP)
    【P2774】方格取数问题(贪心+最大流,洛谷)
  • 原文地址:https://www.cnblogs.com/y122988/p/9925488.html
Copyright © 2020-2023  润新知