Django之自定义分页组件

63 篇文章 0 订阅
7 篇文章 0 订阅

一.分页逻辑

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})

直接拿去用就行…

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Django中,分页是一个常见的需求,为了方便处理分页,可以使用Django内置的分页器Paginator。 Paginator的使用方法很简单,我们可以将一个QuerySet对象或者一个列表作为参数传入Paginator中,然后就可以使用Paginator提供的一些方法来进行分页操作。下面是一个简单的分页器小插件的代码: ```python from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger def paginate(objects, page=1, per_page=10): """ 分页器小插件 """ paginator = Paginator(objects, per_page) try: paginated_objects = paginator.page(page) except PageNotAnInteger: paginated_objects = paginator.page(1) except EmptyPage: paginated_objects = paginator.page(paginator.num_pages) return paginated_objects ``` 上面的代码定义了一个名为paginate的函数,它接受三个参数: - objects:需要进行分页的对象,可以是一个QuerySet对象或者一个列表。 - page:当前页码,默认为1。 - per_page:每页显示的条目数量,默认为10。 函数中首先使用Paginator来对objects进行分页操作,并使用try/except语句来处理异常情况。如果page参数不是一个整数,则将page设置为1;如果page参数超出了可用的页码范围,则将page设置为最大页码数(paginator.num_pages)。 最后,函数返回一个分页后的对象(paginated_objects),可以在Django模板中使用该对象来渲染分页器。 使用该分页器小插件的示例代码如下: ```python from django.shortcuts import render from .models import MyModel from .utils import paginate def my_view(request): my_objects = MyModel.objects.all() paginated_objects = paginate(my_objects, request.GET.get('page')) return render(request, 'my_template.html', {'paginated_objects': paginated_objects}) ``` 在上面的代码中,我们首先获取所有的MyModel对象,然后使用paginate函数将其分页,最后将分页后的对象传递给模板进行渲染。 在模板中,可以使用Django内置的分页器标签({% for obj in paginated_objects %}、{% if paginated_objects.has_previous %}等)来渲染分页器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值