zoukankan      html  css  js  c++  java
  • Web接口开发与自动化测试

    1.导入分页器库
    2.查询表数据列出来
    3.设置分页器每页显示数量
    4.获取当前页码
    5.处理非整数页码页面和超出范围页面
    6.返回当前页面数据

    from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
    # 分页器
    def event_manage(request):
      # 查询Event表所有数据
    event_list = Event.objects.all()
    username = request.session.get('user', '')
    # username = request.COOKIES.get('user', '')
      # 注册分页器,event_list 分页数据,2 每页显示数量
    paginator = Paginator(event_list, 2)
    page = request.GET.get('page')
    try:
         # 返回Page类,返回第page页数据
    contacts = paginator.page(page)
    except PageNotAnInteger:
    # 如果page不是整数,取第一页数据
    contacts = paginator.page(1)
    except EmptyPage:
    # 如果page不在范围内,取最后一页数据
    contacts = paginator.page(paginator.num_pages)
       # 传给前端的参数就是 Page类
    return render(request,'event_manage.html',{'user':username,'events':contacts})


    分页器页面
    <!--分页器-->
    <div class="pagination">
    <span class="step-links">
    {% if events.has_previous %}
    <a href="?page={{events.previous_page_number}}">上一页</a>
    {% endif %}
    {% if guests.paginator.count > 2 %}
    <span class="current">
    {{events.number}} / {{events.paginator.num_pages}}
    </span>
    {% endif %}
    {% if events.has_next %}
    <a href="?page={{events.next_page_number}}">下一页</a>
    {% endif %}
    <br>
    <span class="total">
    合计: {{events.paginator.count}}个发布会
    </span>
    </span>
    </div>


    Page类
    def __init__(self, object_list, number, paginator):
    self.object_list = object_list
    self.number = number
       # 定义分页器,因此也可以通过Page调用分页器
    self.paginator = paginator
    def has_next(self):
    return self.number < self.paginator.num_pages

    def has_previous(self):
    return self.number > 1

    def has_other_pages(self):
    return self.has_previous() or self.has_next()

    def next_page_number(self):
    return self.paginator.validate_number(self.number + 1)

    def previous_page_number(self):
    return self.paginator.validate_number(self.number - 1)

    Paginator类
    @cached_property
    def count(self):
    """Return the total number of objects, across all pages."""
    c = getattr(self.object_list, 'count', None)
    if callable(c) and not inspect.isbuiltin(c) and method_has_no_args(c):
    return c()
    return len(self.object_list)

    @cached_property
    def num_pages(self):
    """Return the total number of pages."""
    if self.count == 0 and not self.allow_empty_first_page:
    return 0
    hits = max(1, self.count - self.orphans)
    return ceil(hits / self.per_page)
  • 相关阅读:
    LNMP状态管理命令
    Gogs基本使用介绍
    初探Asp.net请求机制原理 1
    关于css定位
    JS不同浏览器图片载入处理
    js之队列01
    javascript 快速排序
    JavaScript prototype背后的工作原理
    关于javascrpt if快速判断说明
    js动态加载图片核心代码
  • 原文地址:https://www.cnblogs.com/Tester_Dolores/p/11894940.html
Copyright © 2011-2022 走看看