crm项目之分页

时间:2020-12-18 00:23:44

分页处理

1.展示所有页码

2.处理左右极值

3.添加上下页和选中

4.封装成类及使用

一.函数功能实现

使用bootstrap搭建,先在视图函数中写出分写的功能,之后将分页功能封装为一个类,放置在项目下的utils/pagination.py文件中

1.函数的标准实现

views.py

# 分页
def user_list(request):
    """
    一页显示20

    第1页  0      20
    第2页  20     40

      n   (n-1)*20   20*n

    :param request:
    :return:
    """
    # 获取页码

    try:
        page_num = int(request.GET.get('page', '1'))
        if page_num <= 0:
            page_num = 1
    except Exception as e:
        page_num = 1

    # 每页显示的数据量
    per_num = 10

    # 总数据量
    all_count = len(users)

    # 总页码数
    page_count, more = divmod(all_count, per_num)
    if more:
        page_count += 1

    # 最大显示页码数
    max_show = 11
    half_show = max_show // 2

    # 总页码数 < 最大显示页码数
    if page_count < max_show:
        page_start = 1
        page_end = page_count
    else:
        # 处理左边极值
        if page_num <= half_show:
            page_start = 1
            page_end = max_show
        elif page_num + half_show >= page_count:
            page_start = page_count - max_show + 1
            page_end = page_count
        else:
            page_start = page_num - half_show  # 2
            page_end = page_num + half_show  # 7 + 5  12

    page_list = []
    if page_num == 1:
        page_list.append('<li class="disabled"><a>上一页</a></li>')
    else:
        page_list.append('<li><a href="?page={}">上一页</a></li>'.format(page_num-1, ))

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

    if page_num == page_count:
        page_list.append('<li class="disabled"><a>下一页</a></li>')
    else:
        page_list.append('<li><a href="?page={}">下一页</a></li>'.format(page_num+1, ))


    page_html = ''.join(page_list)

    # return render(request, 'user_list.html', {'users': users[(page_num - 1) * per_num:per_num * page_num],
    #                                           'page_count': range(page_start, page_end + 1)})
    return render(request, 'user_list.html', {'users': users[(page_num - 1) * per_num:per_num * page_num],
                                              'page_html':page_html})

  前端html

{% extends 'layout.html' %}


{% block content %}

<table class="table table-bordered table-hover">

    <thead>
    <tr>
        <th>序号</th>
        <th>QQ</th>
        <th>姓名</th>
        <th>性别</th>
        <th>出生日期</th>
{#        <th>电话</th>#}
        <th>客户来源</th>
        <th>咨询课程</th>
        <th>状态</th>
        <th>最后跟进</th>
        <th>销售</th>
        <th>已报班级</th>
    </tr>
    </thead>
    <tbody>

    {% for customer in all_customer %}

        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ customer.qq }}</td>
            <td>{{ customer.name|default:'未填写' }}</td>
            <td>{{ customer.get_sex_display }}</td>
            <td>{{ customer.birthday|default:'未填写' }}</td>
{#            <td>{{ customer.phone }}</td>#}
            <td>{{ customer.get_source_display }}</td>
            <td>{{ customer.course }}</td>
            <td>
                {{ customer.show_status }}
            </td>
            <td>{{ customer.last_consult_date }}</td>
            <td>{{ customer.consultant }}</td>
{#            <td>{{ customer.class_list.all }}</td>#}
            <td>{{ customer.show_class }}</td>
        </tr>

    {% endfor %}


    </tbody>
</table>

{% endblock %}

2.自我实现

views.py

user_lst = [ {"name":f"用户{i}","pwd":'123'} for i in range(1,302)]
def userlist(request):
    # 获得请求的页码
    try:
        page_num = request.GET.get('page', '1')
        page_num = int(page_num)
        if page_num <= 0:
            page_num = 1
    except Exception as e:
        page_num = 1

    # 每页显示的数据量
    data_count = 10

    # 总数据量
    data_sum = len(user_lst)

    # 总页数,余下的数据量
    page_all, more = divmod(data_sum,data_count)
    if more:
        page_all += 1

    # 显示页码的总个数
    max_page = 11

    # 显示的页码
    half_page = max_page//2
    left = page_num - half_page
    right = page_num + half_page

    if page_all < max_page:
        left = 1
        right = page_all
    elif left <=0:
        left = 1
        right = left + max_page -1
    elif right >=page_all:
        right = page_all
        left = right - max_page +1

    page_list = []

    if page_num <= 1:
        page_list.append('<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>')
    else:
        page_list.append(f'<li><a href="?page={page_num-1}" aria-label="上一页"><span aria-hidden="true">&laquo;</span></a></li>')

    for i in range(left,right+1):
        if i == page_num:
            page_list.append(f'<li><a href="?page={i}" class="active">{i}</a></li>')
        else:
            page_list.append(f'<li><a href="?page={ i }">{ i }</a></li>')


    if page_num == page_all:
        page_list.append('<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>')
    else:
        page_list.append(f'<li><a href="?page={page_num+1}" aria-label="下一页"><span aria-hidden="true">&raquo;</span></a></li>')

    page_html = "".join(page_list)

    return render(request, "page.html", {"user_lst":user_lst[(page_num-1)*data_count:page_num*data_count],
                                         "page_all":range(left,right+1),
                                         "page_html":page_html},)

前端页面

{% extends 'layout.html' %}

{% block content %}
    <table class="table table-bordered table-hover">
        {% for user in user_lst %}
            <tr>
                <td>{{ user.name }}</td>
                <td>{{ user.pwd }}</td>
            </tr>
        {% endfor %}
    </table>
    <nav aria-label="Page navigation">
        <ul class="pagination">
            {{ page_html|safe }}
{
# {% for page in page_all %}#} {# <li><a href="?page={{ page }}">{{ page }}</a></li>#} {# {% endfor %}#} </ul> </nav> {% endblock %}

二.封装成类的功能实现

1.标准实现

crm项目之分页

utils/pagination.py

class Pagination:

    def __init__(self, page_num, all_count, per_num=10, max_show=11):
        # 获取页码
        try:
            self.page_num = int(page_num)
            if self.page_num <= 0:
                self.page_num = 1
        except Exception as e:
            self.page_num = 1

        # 每页显示的数据量
        self.per_num = per_num

        # 总数据量
        all_count = all_count

        # 总页码数
        self.page_count, more = divmod(all_count, per_num)
        if more:
            self.page_count += 1

        # 最大显示页码数
        self.max_show = max_show
        self.half_show = max_show // 2

    @property
    def page_html(self):
        # 总页码数 < 最大显示页码数
        if self.page_count < self.max_show:
            page_start = 1
            page_end = self.page_count
        else:
            # 处理左边极值
            if self.page_num <= self.half_show:
                page_start = 1
                page_end = self.max_show
            elif self.page_num + self.half_show >= self.page_count:
                page_start = self.page_count - self.max_show + 1
                page_end = self.page_count
            else:
                page_start = self.page_num - self.half_show  # 2
                page_end = self.page_num + self.half_show  # 7 + 5  12

        page_list = []
        if self.page_num == 1:
            page_list.append('<li class="disabled"><a>上一页</a></li>')
        else:
            page_list.append('<li><a href="?page={}">上一页</a></li>'.format(self.page_num - 1, ))

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

        if self.page_num == self.page_count:
            page_list.append('<li class="disabled"><a>下一页</a></li>')
        else:
            page_list.append('<li><a href="?page={}">下一页</a></li>'.format(self.page_num + 1, ))

        return ''.join(page_list)

    @property
    def start(self):
        """
        切片的起始值
        :return:
        """
        return (self.page_num - 1) * self.per_num

    @property
    def end(self):
        """
        切片的终止值
        :return:
        """
        return self.page_num * self.per_num

 views.py

users = [{'name': '哈哈{}'.format(i), 'pwd': '123'} for i in range(1, 302)]
# 分页
def user_list(request):
    """
    一页显示20

    第1页  0      20
    第2页  20     40

      n   (n-1)*20   20*n

    :param request:
    :return:
    """
    page = Pagination(request.GET.get('page', '1'), len(users), )

    return render(request, 'user_list.html', {'users': users[page.start:page.end],
                                              'page_html': page.page_html})

 

2.自我实现

utils/pagination.py

class Pagination:

    def __init__(self, page_num, data_sum, data_count=10, max_page=11):

        # 获得请求的页码
        try:
            self.page_num = page_num
            self.page_num = int(self.page_num)
            if self.page_num <= 0:
                self.page_num = 1
        except Exception as e:
            self.page_num = 1

        # 每页显示的数据量
        self.data_count = data_count

        # 总数据量
        self.data_sum = data_sum

        # 总页数,余下的数据量
        self.page_all, more = divmod(self.data_sum, self.data_count)
        if more:
            self.page_all += 1

        # 显示页码的总个数
        self.max_page = 11

        # 显示的页码
        self.half_page = max_page // 2

    @property
    def page_html(self):
        left = self.page_num - self.half_page
        right = self.page_num + self.half_page

        if self.page_all < self.max_page:
            left = 1
            right = self.page_all
        elif left <= 0:
            left = 1
            right = left + self.max_page - 1
        elif right >= self.page_all:
            right = self.page_all
            left = right - self.max_page + 1

        page_list = []

        if self.page_num <= 1:
            page_list.append(
                '<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>')
        else:
            page_list.append(
                f'<li><a href="?page={self.page_num - 1}" aria-label="上一页"><span aria-hidden="true">«</span></a></li>')

        for i in range(left, right + 1):
            if i == self.page_num:
                page_list.append(f'<li><a href="?page={i}" class="active">{i}</a></li>')
            else:
                page_list.append(f'<li><a href="?page={i}">{i}</a></li>')

        if self.page_num == self.page_all:
            page_list.append(
                '<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>')
        else:
            page_list.append(
                f'<li><a href="?page={self.page_num + 1}" aria-label="下一页"><span aria-hidden="true">»</span></a></li>')

        return  "".join(page_list)

    @property
    def start(self):
        print((self.page_num-1)*self.data_count)
        return (self.page_num-1)*self.data_count
    @property
    def end(self):
        print(self.page_num,self.data_count)
        return self.page_num*self.data_count

 views.py

user_lst = [ {"name":f"用户{i}","pwd":'123'} for i in range(1,302)]
from utils.pagination import Pagination

def userlist(request):

    pag = Pagination(request.GET.get("page","1"),len(user_lst),)

    return render(request, "page.html", {"user_lst":user_lst[pag.start:pag.end],
                                         # "page_all":range(left,right+1),
                                         "page_html":pag.page_html},)

# return render(request, "page.html", {"user_lst":user_lst[(page_num-1)*data_count:page_num*data_count],
#                                          "page_all":range(left,right+1),
#                                          "page_html":pag.page_html},)

  crm项目之分页