Django自定义分页完整版带保留搜索字段功能

本文继自定义分页完整版之后的又一个改进版本,带上了保留字段,

上一篇地址:https://www.cnblogs.com/apollo1616/articles/9925787.html

本文只粘贴pager文件替换原文的pager文件,html替换原文的test.html文件

views.py文件内容

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import copy

class Pagination(object):
    # 封装分页相关数据
    def __init__(self, request, current_page, record_count, page_record_limit, page_count_limit):
        """
        :param page_count:          总页数
        :param record_count:         数据总个数
        :param page_range:             页的范围
        :param page_count_limit:    最多显示页面
        :param page_record_limit:    每页显示的行数
        :param page_record_list:    页面数据列表
        :param current_page:        当前页
        :param page_mid:            页面显示页码中间位置页码
        """
        self.params = copy.deepcopy(request.GET)
        # 当前页码
        try:
            current_page = int(current_page)
            if current_page <= 1:
                current_page = 1
        except Exception as e:
            current_page = 1
        self.current_page = current_page
        # 数据总个数
        self.record_count = record_count
        # 每页显示的行数
        self.page_record_limit = page_record_limit
        # 总页数(数据总个数record_count / 每页显示的行数page_record_limit)
        # [数据总个数]对[每页显示的行数]取余,如果余数为0,总页数等于商,否则等于商+1
        page_count, tmp = divmod(record_count, page_record_limit)
        if tmp:
            page_count += 1
       self.page_count = page_count else: self.page_count = page_count # 页面页数显示限制(要奇数,便于对称显示) self.page_count_limit = page_count_limit # 每页第一条数据 @property def start(self): return (self.current_page - 1) * self.page_record_limit # 每页最后一条数据 @property def end(self): return self.current_page * self.page_record_limit def page_range(self): # 如果,总页码[小于]最多显示页面: if self.page_count <= self.page_count_limit: page_start = 1 page_end = self.page_count + 1 # 如果,总页码[大于]最多显示页面: else: # 页面显示页码中间位置页码 self.page_mid = int(self.page_count_limit - 1) // 2 if self.current_page <= self.page_mid: page_start = 1 page_end = self.page_count_limit + 1 else: if (self.current_page + self.page_mid) > self.page_count: page_start = self.page_count - self.page_count_limit + 1 page_end = self.page_count + 1 else: page_start = self.current_page - self.page_mid page_end = self.current_page + self.page_mid + 1 return range(page_start, page_end) # 页面显示页码的控制 def page_html(self): # 准备页面渲染的数据 page_record_list = [] # 首页 first_page = '
  • 首页
  • ' % (1,) page_record_list.append(first_page) # 处理上一页 if self.current_page <= 1: prev_page = '
  • 上一页
  • ' else: prev_page = '
  • 上一页
  • ' % (self.current_page - 1,) page_record_list.append(prev_page) ################ 点击页码保留搜索条件 ################ # 页码显示部分 for i in self.page_range(): self.params["page"] = i if i == self.current_page: temp = '
  • %s
  • ' % (self.params.urlencode(), i) else: temp = '
  • %s
  • ' % (self.params.urlencode(), i,) page_record_list.append(temp) ################ 点击页码保留搜索条件 ################ # 处理下一页 if self.current_page >= self.page_count: next_page = '
  • 下一页
  • ' else: next_page = '
  • 下一页
  • ' % (self.current_page + 1,) page_record_list.append(next_page) # 尾页 last_page = '
  • 尾页
  • ' % (self.page_count,) page_record_list.append(last_page) return ''.join(page_record_list)

     

    test.html文件内容

    
    "en">
    
        "UTF-8">
        Apollo
        
        "stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
              integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        
        
        
        
    
    
    
    class="container">
    class="page-header">

    自定义分页器 阿波罗图书管理系统

    class="row">
    class="col-md-6 col-md-offset-3">
    "" method="get">
    class="form-group col-md-10" style="padding: 0">
    class="input-group">
    class="input-group-addon">class="glyphicon glyphicon-search">
    "text" class="form-control" id="search" name=" " placeholder="请输入关键词搜索">
    "padding-left: 5px" class="col-md-2">
    class="row">
    class="col-md-6 col-md-offset-3"> class="table table-bordered table-hover table-striped"> {% for row in data %} {% endfor %}
    姓名 年龄
    {{ row.name }} {{ row.age }}
    {# 分页样式 #}
    class="col-md-6 col-md-offset-3">
      class="pagination pagination-sm"> {{ page_obj.page_html|safe }}

     

    self.page_count = page_count

    转载于:https://www.cnblogs.com/apollo1616/articles/9926040.html

    你可能感兴趣的:(Django自定义分页完整版带保留搜索字段功能)