• 自定义分页器


    1.前端

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
         <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <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">
    </head>
    <body>
    <ul>
        {% for book in book_list %}
            <li>{{ book.title }}------{{ book.price }}</li>
        {% endfor %}
        
    </ul>
    <nav aria-label="Page navigation">
      <ul class="pagination">
          {{ pagination.page_html|safe }}
      </ul>
    </nav>
    </body>
    </html>

    2.views

    def index(request):
        from app01.page import Pagination
        book_list = Book.objects.all()
        current_page_num = request.GET.get("page")
        pagination = Pagination(current_page_num,book_list.count(),request)
        book_list = book_list[pagination.start:pagination.end]
    
        return render(request,"index.html",locals())

    3.page

    class Pagination(object):
    
        def __init__(self,current_page_num,all_count,request,per_page_num=2,parger_count=11):
            '''
            封装分页相关数据
            :param current_page_num: 当前访问页的数字
            :param all_count:       分页数据的总数
            :param request:         request请求数据
            :param per_page_num:    每页显示的数据条数
            :param parger_count:    最多显示的页码个数
            '''
            try:
                current_page_num = int(current_page_num)
            except Exception as e:
                current_page_num = 1
    
            if current_page_num < 1:
                current_page_num = 1
    
            self.current_page_num = current_page_num
            self.all_count = all_count
            self.per_page_num = per_page_num
    
            # 实际总页码
            all_pager,tmp = divmod(all_count,per_page_num)
            if tmp:
                all_pager += 1
            self.all_pager = all_pager
    
            self.pager_count = parger_count
            self.pager_count_half = int((parger_count - 1) / 2)
    
            # 保存搜索条件
            import copy
            self.params = copy.deepcopy(request.GET)
    
        @property
        def start(self):
            return (self.current_page_num - 1) * self.per_page_num
    
        @property
        def end(self):
            return self.current_page_num * self.per_page_num
    
        def page_html(self):
            # 如果总页码<11个
            if self.all_pager <= self.pager_count:
                pager_start = 1
                pager_end = self.all_pager + 1
            # 总页码>11
            else:
                # 当前页没有前5页
                if self.current_page_num <= self.pager_count_half:
                    pager_start = 1
                    pager_end = self.pager_count + 1
                # 当前页大于5
                else:
                    # 当前页没有后5页
                    if (self.current_page_num + self.pager_count_half) > self.all_pager:
                        pager_start = self.all_pager - self.pager_count +1
                        pager_end = self.all_pager + 1
                    else:
                        pager_start = self.current_page_num - self.pager_count_half
                        pager_end = self.current_page_num + self.pager_count_half +1
    
            page_html_list = []
            self.params["page"] = 1
            first_page = '<li><a href="?%s">首页</a></li>' % (self.params.urlencode())
            page_html_list.append(first_page)
    
            if self.current_page_num <= 1:
                prev_page = '<li class="disabled"><a href="#">上一页</a></li>'
            else:
                self.params["page"] = self.current_page_num - 1
                prev_page = '<li><a href="?%s">上一页</a></li>' % (self.params.urlencode(),)
    
            page_html_list.append(prev_page)
    
            for i in range(pager_start,pager_end):
                self.params["page"] = i
                if i == self.current_page_num:
                    temp = '<li class="active"><a href="?%s">%s</a></li>' %(self.params.urlencode(),i)
                else:
                    temp = '<li><a href="?%s">%s</a></li>' % (self.params.urlencode(),i,)
                page_html_list.append(temp)
    
            if self.current_page_num >= self.all_pager:
                next_page = '<li class="disabled"><a href="#">下一页</a></li>'
            else:
                self.params["page"] = self.current_page_num + 1
                next_page = '<li><a href="?%s">下一页</a></li>' % (self.params.urlencode(),)
            page_html_list.append(next_page)
            self.params["page"] = self.all_pager
            last_page = '<li><a href="?%s">尾页</a></li>' % (self.params.urlencode())
            page_html_list.append(last_page)
    
            return "".join(page_html_list)

     CBV

    FBV-----function based view    
    CBV-----class  based view
            
        path('index/', views.index),
        path('login/', views.LoginView.as_view()),
        path('login/', View.as_view.view)
        # 用户访问get请求/login/-----------view(request)
        
        def view(request):
            self = cls(**initkwargs)
            return self.dispatch(request, *args, **kwargs)
            
            def dispatch(request, *args, **kwargs):
                    if request.method.lower() in self.http_method_names:
                        handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
                    else:
                        handler = self.http_method_not_allowed
                    return handler(request, *args, **kwargs)
  • 相关阅读:
    Codeforces 385 D Bear and Floodlight
    Linux经常使用的命令(两)
    hadoop编程技巧(6)---处理大量的小型数据文件CombineFileInputFormat申请书
    android Notification分析—— 您可能会遇到各种问题
    人类探索地外文明的显著取得的进展
    腰带“兄弟”事实上,投资
    C++机器学习古典材料
    [Recompose] Render Nothing in Place of a Component using Recompose
    [Recompose] Replace a Component with Non-Optimal States using Recompose
    [Recompose] Show a Spinner While a Component is Loading using Recompose
  • 原文地址:https://www.cnblogs.com/qq849784670/p/9923764.html
Copyright © 2020-2023  润新知