码迷,mamicode.com
首页 > 其他好文 > 详细

crm 系统项目(三) 自动分页

时间:2018-12-18 19:44:00      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:over   lse   app1   项目   tar   超过   hid   imp   ret   

crm 系统项目(三) 自动分页

需求:

  1. 做一个自动分页, 每15条数据1页

  2. 让当前页数在中间显示

  3. 上一页, 下一页

注意情况:

  1.总页数 小于 规定显示的页数

  2. 左右两边极值情况

  3. 前后端传参,交互 

  

user_list.py

技术分享图片
{% extends layout.html %}

{% block content %}


    <table class="table table-bordered table-hover">
        <thead>
        <tr>
            <th>序号</th>
            <th>用户名</th>
            <th>密码</th>

        </tr>
        </thead>
        <tbody>
        {% for user in all_user %}
            <tr>
                <td>{{ forloop.counter }}</td>
                <td>{{ user.name }}</td>
                <td>{{ user.pwd }}</td>
            </tr>
        {% endfor %}

        </tbody>
    </table>
    <nav aria-label="Page navigation">
  <ul class="pagination">
    {{ page_html|safe }}
  </ul>
</nav>
{% endblock %}
urls.py

 

views.py

技术分享图片
userlist= [{name:傻傻的兆满{}号.format(i),pwd:帅帅的灰灰{}号.format(i)} for i in range(1,667)]

def user_list(request):
    try:
        page = int(request.GET.get(page, 1))    #获取当前页码数
        if page <= 0:
            page = 1
    except Exception as e:
        page = 1
    print(page)

    per_num = 15    # 每页显示的数据
    all_count = len(userlist)   #总的数据量
    page_num, more = divmod(all_count, per_num) #商和余数
    if more:
        page_num += 1
    max_show = 11   #最多显示的分页数
    half_show = max_show // 2   #当前页码左面和右面各多少个,

    if page_num < max_show:
        page_start = 1
        page_end = page_num
    else:
        if page <= half_show:
            page_start = 1
            page_end = max_show
        elif page + half_show > page_num:
            page_start = page_num - max_show + 1
            page_end = page_num
        else:
            page_start = page - half_show
            page_end = page + half_show

        start = (page - 1) * per_num
        end = page * per_num

    li_list = []    # 将分配好的页码,放入列表
    if page == 1:
        li_list.append(<li class="disabled" ><a> << </a></li>)
    else:
        li_list.append(<li ><a href="?page={}"> << </a></li>.format(page - 1))

    for i in range(page_start, page_end + 1):
        if page == i:
            li_list.append(<li class="active"><a href="?page={}">{}</a></li>.format(i, i))
        else:
            li_list.append(<li><a href="?page={}">{}</a></li>.format(i, i))

    if page == page_num:
        li_list.append(<li class="disabled" ><a> >> </a></li>)
    else:
        li_list.append(<li ><a href="?page={}"> >> </a></li>.format(page + 1))

    page_html = ‘‘.join(li_list)    #列表变成字符串
    return render(request, user_list.html, {all_user: userlist[start:end],
                                              page_num: range(page_start, page_end + 1),
                                              page_html: page_html},)
views.py

为了方便实用可以把整个分页封装起来, 

  在app下新建文件 paginationg.py

  封装分页功能

技术分享图片
# 封装分页
class Pagination:

    def __init__(self, page, all_count, per_num=15, max_show=11):
        try:
            page = int(page)
            if page <= 0:
                page = 1
        except Exception as e:
            page = 1
        self.page = page
        self.all_count = all_count
        self.per_num = per_num
        self.max_show = max_show
        # 总页码数
        self.page_num, more = divmod(all_count, per_num)
        if more:
            self.page_num += 1
        # 最多显示页码数
        half_show = max_show // 2

        if self.page_num < max_show:
            # 总页码数不够 最大显示页码数
            self.page_start = 1
            self.page_end = self.page_num
        else:
            # 能显示超过最大显示页码数
            if page <= half_show:
                # 处理左边的极值
                self.page_start = 1
                self.page_end = max_show
            elif page + half_show > self.page_num:
                # 处理右边的极值
                self.page_start = self.page_num - max_show + 1
                self.page_end = self.page_num
            else:
                # 正常的情况
                self.page_start = page - half_show
                self.page_end = page + half_show

    @property
    def start(self):
        return (self.page - 1) * self.per_num

    @property
    def end(self):
        return self.page * self.per_num

    @property
    def page_html(self):
        li_list = []
        if self.page == 1:
            li_list.append(<li class="disabled" ><a> << </a></li>)
        else:
            li_list.append(<li ><a href="?page={}"> << </a></li>.format(self.page - 1))

        for i in range(self.page_start, self.page_end + 1):
            if self.page == i:
                li_list.append(<li class="active"><a href="?page={}">{}</a></li>.format(i, i))
            else:
                li_list.append(<li><a href="?page={}">{}</a></li>.format(i, i))

        if self.page == self.page_num:
            li_list.append(<li class="disabled" ><a> >> </a></li>)
        else:
            li_list.append(<li ><a href="?page={}"> >> </a></li>.format(self.page + 1))

        return ‘‘.join(li_list)
paginationg.py

viewa.py下直接传参调用

技术分享图片
# 调用封装好的分页
from app1.utils.paginationg import Pagination
def user_list(request):
    pager = Pagination(request.GET.get(page,1), len(user_list), per_num=10, max_show=15)
    return render(request, user_list.html,
                  {"all_user": userlist[pager.start:pager.end],
                   page_html: pager.page_html
                   }, )
View Code

 

 

 

 

crm 系统项目(三) 自动分页

标签:over   lse   app1   项目   tar   超过   hid   imp   ret   

原文地址:https://www.cnblogs.com/konghui/p/10138687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!