• Django+Bootstrap实现数据查询分页显示功能


    Django分页实现(Bootstrap)

    请自行在HTML中导入Bootstrap
    page.py 分页组件

    # -*- coding: utf-8 -*-
    # @Time     :2020/5/27 21:33
    # @Author   :12257
    # @SoftWare :PyCharm
    from django.conf import settings
    from django.utils.safestring import mark_safe	# marksafe用来返回安全字符串HTML标签 防止转义
    
    
    class MyPagenation():
    
        def __init__(self, page, total_count, per_page_num, page_num_show, base_url, get_data=None):
            self.base_url = base_url
            self.get_data = get_data
            per_page_num = settings.PER_PAGE_NUM  # 每页显示多少数据
            page_num_show = settings.PAGE_NUM_SHOW  # 显示的页码数
            customer_count = total_count
            div, mod = divmod(customer_count, per_page_num)
            if mod:
                page_num_count = div + 1
            else:
                page_num_count = div
            try:
                self.page = int(page)
            except Exception as e:
                self.page = 1
            if self.page <= 0:
                self.page = 1
            elif self.page > page_num_count:
                self.page = page_num_count
            self.page_num_count = page_num_count
            start_num = (self.page - 1) * per_page_num
            end_num = self.page * per_page_num
            page_num_half_show = page_num_show // 2
            page_num_start_show = self.page - page_num_half_show
            page_num_end_show = self.page + page_num_half_show + 1
            if page_num_start_show <= 0:  # 异常分页判断
                page_num_start_show = 1
                page_num_end_show = page_num_show + 1
            elif page_num_end_show > page_num_count:
                page_num_start_show = page_num_count - page_num_show + 1
                page_num_end_show = page_num_count + 1
            if page_num_count < page_num_end_show:
                page_num_end_show = page_num_count + 1
            self.start_num = start_num
            self.end_num = end_num
            self.page_num_start_show = page_num_start_show
            self.page_num_end_show = page_num_end_show
    
        @property
        def start_num_show(self):
            return self.start_num
    
        @property
        def end_num_show(self):
            return self.end_num
    
        def page_html(self):
            page_num_range = range(self.page_num_start_show, self.page_num_end_show)
            page_html = ''
            first_page_html = f'<nav aria-label="Page navigation"><ul class="pagination"><li><a href="/{self.base_url}/1?{self.get_data}" aria-label="Previous"><span ' 
                              'aria-hidden="true">首页</span></a></li>'
            page_html += first_page_html
            page_pre = ''
            page_html += page_pre
            if self.page <= 1:
                pre_page = f'<li class="disabled"><a href="javascript:void(0)" aria-label="Previous"><span ' 
                           'aria-hidden="true">&laquo;</span></a></li>'
            else:
                pre_page = f'<li><a href="/{self.base_url}/{self.page - 1}?{self.get_data}" aria-label="Previous"><span ' 
                           'aria-hidden="true">&laquo;</span></a></li>'
            page_html += pre_page
            for i in page_num_range:
                if i == self.page:
                    page_html += f'<li class="active"><a href="/{self.base_url}/{i}?{self.get_data}">{i}</a></li>'
                else:
                    page_html += f'<li><a href="/{self.base_url}/{i}?{self.get_data}">{i}</a></li>'
            if self.page >= 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></li> '
            else:
                page_next_html = f'<li><a href="/{self.base_url}/{self.page + 1}?{self.get_data}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'
            page_html += page_next_html
            last_page_html = f'<li><a href="/{self.base_url}/{self.page_num_count}?{self.get_data}" aria-label="Previous"><span ' 
                             'aria-hidden="true">尾页</span></a></li></ul></nav>'
            page_html += last_page_html
            return mark_safe(page_html)
    
    

    settings.py 组件设置

    PER_PAGE_NUM = 10
    PAGE_NUM_SHOW = 5
    

    view.py 使用分页实例

    def customers(request, page=1):
        """
        客户信息查看
        :param request:
        :param page: 页码
        :return:
        """
        kw = request.GET.get('keyword')         # 查询关键字
        sf = request.GET.get('search_field')   # 选择查询的字段
        get_data = request.GET.urlencode()
        # print('get_data=', get_data)
        if sf:
            sf = sf + '__contains'
        if kw:
            customer_list = models.Customer.objects.filter(**{sf: kw})
        else:
            customer_list = models.Customer.objects.all()
        customer_count = customer_list.count()
        per_page_num = 10
        per_num_show = 5
        base_url = 'customers'
        mypage = MyPagenation(page, customer_count, per_page_num, per_num_show, base_url,get_data=get_data)
        page_html = mypage.page_html()
        customer_objs = customer_list.reverse()[mypage.start_num_show:mypage.end_num_show]
        context = {
            'customer_objs': customer_objs,
            'page_html': page_html,
            # 'keyword': kw,
        }
        return render(request, 'salehtml/customers.html', context)
    
  • 相关阅读:
    01 Go 1.1 Release Notes
    Go 1 Release Notes
    go语言版本变化
    npm install的时候出现unexpected end of file错误提示时的解决办法
    Intellij IDEA注册激活破解
    vsCode如何从github拉取项目
    IDEA为了使用方便,需要改的几条配置
    IntelliJ IDEA 下的svn配置及使用
    intellij idea 的全局搜索快捷键方法
    Interllij IDEA中启动web项目
  • 原文地址:https://www.cnblogs.com/pandaa/p/13162459.html
Copyright © 2020-2023  润新知