Django之自定义分页组件

一.分页逻辑

from django.utils.safestring import mark_safe  #mark_safe:安全字符串

class MyPage:

    def __init__(self, page_num, page_num_count, req_path, per_page_num, page_num_show):

        self.page_num = page_num
        self.page_num_count = page_num_count
        self.req_path = req_path
        self.per_page_num = per_page_num  # 每页显示多少条数据
        self.page_num_show = page_num_show  # 显示的页码数

        # 如果有异常就默认当前页
        try:
            self.page_num = int(self.page_num)
        except Exception:
            self.page_num = 1

        # 计算有多少页 如果商不为0页数就加一 总的页码数
        a, b = divmod(self.page_num_count, self.per_page_num)
        if b:
            self.page_num_count = a + 1
        else:
            self.page_num_count = a

        # 如果小于1就让他去当前页,大于总页数就让他去最后一页
        if self.page_num <= 0:
            self.page_num = 1
        elif self.page_num >= self.page_num_count:
            self.page_num = self.page_num_count

        # 每页展示的页码数
        half_show = self.page_num_show // 2  # 2
        if self.page_num - half_show <= 0:
            start_page_num = 1
            end_page_num = self.page_num_show + 1
        elif self.page_num + half_show >= self.page_num_count:
            start_page_num = self.page_num_count - self.page_num_show + 1
            end_page_num = self.page_num_count + 1
        else:
            start_page_num = self.page_num - half_show  # 3
            end_page_num = self.page_num + half_show + 1  # 7

        self.start_page_num = start_page_num
        self.end_page_num = end_page_num

    # 每页显示多少条数据  开始
    @property
    def start_data_num(self):
        return (self.page_num - 1) * self.per_page_num

    # 每页显示多少条数据  结束
    @property
    def end_data_num(self):
        return self.page_num * self.per_page_num

    # 前端页码
    def page_html(self):

        page_num_range = range(self.start_page_num, self.end_page_num)
        page_html = ''

        # 上一页
        if self.page_num <= 1:
            page_pre_html = f'''
                    <nav aria-label="Page navigation">
                    <ul class="pagination">
                    <li class="disabled">
                    <a href="{self.req_path}?page={1}" aria-label="Previous">
                        <span aria-hidden="true">首页</span>
                      </a>
                      <a href="javascript:void(0)" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                      </a>
                    </li>
                '''
        else:
            page_pre_html = f'''
                    <nav aria-label="Page navigation">
                    <ul class="pagination">
                    <li>
                    <a href="{self.req_path}?page={1}" aria-label="Previous">
                        <span aria-hidden="true">首页</span>
                      </a>
                      <a href="{self.req_path}?page={self.page_num - 1}" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                      </a>
                    </li>
                    '''
        page_html += page_pre_html

        # 页码标签 并且有选中状态
        for i in page_num_range:
            if i == self.page_num:
                page_html += f'<li class="active"><a href="{self.req_path}?page={i}">{i}</a></li>'
            else:
                page_html += f'<li><a href="{self.req_path}?page={i}">{i}</a></li>'

        # 下一页
        if self.page_num >= self.page_num_count:
            page_next_html = f'''
                        <li class="disabled">
                          <a href="javascript:void(0)" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                          </a>
                            <a href="{self.req_path}?page={self.page_num_count}" aria-label="Next">
                            <span aria-hidden="true">尾页</span>
                          </a>
                        </li>
                      </ul>
                    </nav>
                '''
        else:
            page_next_html = f'''
                        <li>
                          <a href="{self.req_path}?page={self.page_num + 1}" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                          </a>
                            <a href="{self.req_path}?page={self.page_num_count}" aria-label="Next">
                            <span aria-hidden="true">尾页</span>
                          </a>
                        </li>
                      </ul>
                    </nav>
                        '''

        page_html += page_next_html

        return mark_safe(page_html)

二.首页逻辑

def index(request):

    # 当前页
    page_num = request.GET.get('page')
    # 获取路径
    req_path = request.path_info
    # 计算有多少条数据
    page_num_count = app04.models.Customer.objects.all().count()
    #settings中封装的页码数和每页显示多少条数据
    per_page_num = settings.PER_PAGE_NUM  # 每页显示多少条数据
    page_num_show = settings.PAGE_NUM_SHOW  # 显示的页码数
    #封装的分页组件
    page_obj = MyPage(page_num,page_num_count,req_path,per_page_num,page_num_show)
    page_html = page_obj.page_html()

    # 获取所有的数据
    index_obj = app04.models.Index.objects.all()[page_obj.start_data_num:page_obj.end_data_num]
    return render(request, 'crm/index.html', {'index_obj': index_obj,'page_html':page_html})

直接拿去用就行…

猜你喜欢

转载自blog.csdn.net/qq_39253370/article/details/106064424