python自定义翻页配置

1、创建pager.py文件,针对翻页进行函数书写

class PageInfo(object):
    # current_page  当前页数
    # all_count  所有行
    # per_page   每页的行数
    # show_page  控制只显示11个按钮
    def __init__(self, current_page, all_count, per_page, base_url, show_page=5):
        try:
            self.current_page = int(current_page)
        except Exception as e:
            self.current_page = 1
        self.per_page = per_page

        a, b = divmod(all_count, per_page)

        if b:
            a = a + 1
        self.all_pager = a
        self.base_url = base_url
        self.show_page = show_page
        print(a, b,per_page)
    def start(self):
        return (self.current_page - 1) * self.per_page

    def end(self):
        return self.current_page * self.per_page

    def pager(self):
        page_list = []

        # 如果总页数小于11
        if self.all_pager < self.show_page:
            begin = 1
            stop = self.all_pager + 1
        # 如果总页数大于11
        else:
            # 如果当前页小于等于5,一直保持1,11
            if self.current_page <= 2:
                begin = 1
                stop = self.show_page + 1
            else:
                # 如果当前页大于所有页
                if self.current_page + 2 > self.all_pager:
                    begin = self.all_pager - self.show_page + 1
                    stop = self.all_pager + 1
                else:
                    begin = self.current_page - 2
                    stop = self.current_page + 2 + 1

        if self.current_page > 1:
            prve = "<li><a href='%s/?page=%s'>上一页 </a></li> " % (self.base_url,self.current_page - 1,)
        else:
            prve = "<li><a href='#'>上一页 </a> </li>"
        page_list.append(prve)
        fist = "<li><a href='%s/?page=1'>首页 </a></li> " % (self.base_url,)
        page_list.append(fist)
        for i in range(begin, stop):
            if i == self.current_page:
                temp = "<li class='active' ><a href='%s/?page=%s'> %s </a></li> " % (self.base_url, i, i)
            else:
                temp = "<li><a href='%s/?page=%s'> %s </a></li> " % (self.base_url, i, i)
            page_list.append(temp)
        end = "<li><a href='%s/?page=%s'>尾页 </a> </li>" % (self.base_url,self.all_pager,)
        page_list.append(end)

        if self.current_page < self.all_pager:
            nex = "<li><a href='%s/?page=%s'>下一页 </a> </li>" % (self.base_url,self.current_page + 1,)
        else:
            nex = "<li><a href='#'>下一页 </a></li>"
        page_list.append(nex)
        return ''.join(page_list)
View Code

2、创建函数,调用翻页函数

def userInfo(request):
    all_count = models.userInfo.objects.all().count()
    print(all_count)
    page_info = PageInfo(request.GET.get('page'), all_count, 10, '/userInfo')
    user_list = models.userInfo.objects.all()[page_info.start():page_info.end()]

    return render(request, 'User/userInfo.html', {'user_list': user_list, 'page_info': page_info})
View Code

3、前端html展示

{% extends 'layout.html' %}

{% block css %}
    <style>

    </style>
{% endblock %}

{% block table %}
        <div style="margin: 10px 15px;">
            <a class="btn btn-primary" href="/add_userInfo/">添加</a>
        </div>
    <table class="table table-condensed table-hover table-bordered">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>用户名</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>生日</th>
                    <th>邮箱</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for row in user_list %}
                    <tr>
                        <td>{{ row.id }}</td>
                        <td>{{ row.user }}</td>
                        <td >{{ row.name }}</td>
                        <td >{{ row.sex }}</td>
                        <td >{{ row.birthday }}</td>
                        <td >{{ row.email }}</td>
                        <td>
                            <a href="/edit_userInfo/?tid={{ row.id }}">编辑</a>
                            <a href="/del_userInfo/?tid={{ row.id }}">删除</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

    <nav aria-label="Page navigation">
      <ul class="pagination">
        <li>
          <a href="#" aria-label="Previous">
            <span aria-hidden="true">«</span>
          </a>
        </li>
            {{ page_info.pager|safe}}
        <li>
          <a href="#" aria-label="Next">
            <span aria-hidden="true">»</span>
          </a>
        </li>
      </ul>
    </nav>

{% endblock %}

{% block js %}
    <script src="/static/jquery.min.js"></script>
    <script></script>

{% endblock %}
View Code

4、页面展示

 

posted on 2021-02-23 15:29  !!雪莲花!!  阅读(158)  评论(0)    收藏  举报