测试版本

代码:

# 测试分页users=[{'name':'alex{}'.format(i),'pwd':'aaa{}'.format(i)}for i in range(1,302)]

def user_list(request):        # 获取当前页码值 并处理异常 输入字母也显示第一页  默认为第一页    try:        current_page=int(request.GET.get('page',1))        # 如果小于o 显示页面为1        if current_page<=0:            current_page=1    except Exception as e:        current_page=1#     最多显示页码数    max_show=11    half_show=max_show//2

#每页显示数量    per_num=10#         总数量    all_count=len(users)#     总页数    total_num,more=divmod(all_count,per_num)#     判断是否有剩余的    if more:        total_num +=1

    #总页面数小于最大显示数: 显示总页码数    if total_num<max_show:        page_start= 1        page_end=total_num    #总页面数大于最大显示数: 显示总页码数    else:        # 当前页面数 小于一半        if current_page<=half_show:            page_start=1            page_end=max_show        # 当前页面数加一半 大于总页面数        elif current_page +half_show >=total_num:            page_end = total_num            page_start=total_num -max_show +1        else:            page_start=current_page - half_show            page_end=current_page +half_show        # 存放li标签的列表    html_list=[]    #     写首页 直接跳转    first_li='<li><a href="/crm/user_list/?page=1">首页</a></li>'    html_list.append(first_li)    # 添加点击到第一页 不能点    if current_page ==1:        prev_li='<li class="disabled"><a><<</a></li>'    #  可以点击上一页    else:        prev_li='<li><a href="/crm/user_list/?page={0}"><<</a></li>'.format(current_page - 1)    html_list.append(prev_li)

    # 循环 分页 在后端添加样式再传到前端    for num in range(page_start,page_end+1):        # 给当前页面加样式        if current_page==num:            li_html='<li class="active"><a href="/crm/user_list/?page={0}">{0}</a></li>'.format(num)        #     否则不加        else:            li_html = '<li ><a href="/crm/user_list/?page={0}">{0}</a></li>'.format(num)        html_list.append(li_html)

    # 添加点击到最后一页 不能点    if current_page==total_num:        next_li='<li class="disabled"><a>>></a></li>'        #  可以点击下一页    else:        next_li = '<li><a href="/crm/user_list/?page={0}">>></a></li>'.format(current_page + 1)    html_list.append(next_li)        #     写尾页 直接跳转    last_li = '<li><a href="/crm/user_list/?page={}">尾页</a></li>'.format(total_num)    html_list.append(last_li)    #在前端显示全部界面\

    html_str=mark_safe(''.join(html_list))

    # 获取切片起始值  current_page为页数,因为是切片取第一页要从零开始 要减一    start=(current_page-1)*per_num    # 获取切片终止值    end=current_page *per_num

    return render(request,'user_list.html',                  {'data':users[start:end],                   # 'total_num':range(page_start,page_end+1)                   'html_str': html_str                   }                  )html代码:(里面有模板与继成)
{% extends 'board/base.html' %}

{% block conten %}

    <table class="table table-bordered">        <thead>        <tr>            <th>序号</th>            <th>用户名</th>            <th>密码</th>        </tr>        </thead>        <tbody>        {% for user in data %}            <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">{#            {% for num in total_num %}#}{#                <li><a href="/crm/user_list/?page={{ num }}">{{ num }}</a></li>{% endfor %}#}

            {#            {% for num in total_num %}#}            {#                <li><a href="/user_list/?page={{ num }}">{{ num }}</a></li>#}            {#            {% endfor %}#}            {#            {{ html_str|safe }}#}            {{ html_str }}        </ul>    </nav>

{% endblock %}

自己定义类 现在项目同级建一个 utils的文件夹  里面创建pagination.pypagination.py代码:
# 分页器from django.utils.safestring import mark_safe

class Pagination:    def  __init__(self,request,all_count,per_num=10, max_show=11):        # 基本的URL        self.base_url=request.path_info        try:            self.current_page=int(request.GET.get('page',1))            # 如果小于o 显示页面为1            if self.current_page<=0:                self.current_page=1        except Exception as e:            self.current_page=1    #     最多显示页码数        self.max_show=max_show        half_show=max_show//2

    #每页显示数量        self.per_num=per_num    #         总数量        self.all_count=all_count    #     总页数

        self.total_num,more=divmod(all_count,per_num)    #     判断是否有剩余的        if more:            self.total_num +=1

        #总页面数小于最大显示数: 显示总页码数        if self.total_num<max_show:            self.page_start=1            self.page_end=self.total_num        #总页面数大于最大显示数: 显示总页码数        else:            # 当前页面数 小于一半            if self.current_page<=half_show:                self.page_start=1                self.page_end=max_show            # 当前页面数加一半 大于总页面数            elif self.current_page +half_show >=self.total_num:                self.page_end = self.total_num                self.page_start=self.total_num -max_show +1            else:                self.page_start=self.current_page - half_show                self.page_end=self.current_page +half_show

    @property    def start(self):        # 获取切片起始值  current_page为页数,因为是切片取第一页要从零开始 要减一

        return (self.current_page - 1) * self.per_num    @property    def end(self):        # 获取切片终止值

        return self.current_page * self.per_num    @property    def show_li(self):        # 存放li标签的列表        html_list = []        #     写首页 直接跳转        first_li = '<li><a href="{}?page=1">首页</a></li>'.format(self.base_url)        html_list.append(first_li)        # 添加点击到第一页 不能点        if self.current_page == 1:            prev_li = '<li class="disabled"><a><<</a></li>'        #  可以点击上一页        else:            prev_li = '<li><a href="{1}?page={0}"><<</a></li>'.format(self.current_page - 1,self.base_url)        html_list.append(prev_li)

        # 循环 分页 在后端添加样式再传到前端        for num in range(self.page_start, self.page_end + 1):            # 给当前页面加样式            if self.current_page == num:                li_html = '<li class="active"><a href="{1}?page={0}">{0}</a></li>'.format(num,self.base_url)            #     否则不加            else:                li_html = '<li ><a href="{1}?page={0}">{0}</a></li>'.format(num,self.base_url)            html_list.append(li_html)

        # 添加点击到最后一页 不能点        if self.current_page == self.total_num:            next_li = '<li class="disabled"><a>>></a></li>'            #  可以点击下一页        else:            next_li = '<li><a href="{1}?page={0}">>></a></li>'.format(self.current_page + 1,self.base_url)        html_list.append(next_li)        #     写尾页 直接跳转        last_li = '<li><a href="{1}?page={0}">尾页</a></li>'.format(self.total_num,self.base_url)        html_list.append(last_li)        # 在前端显示全部界面\

        return mark_safe(''.join(html_list))
views.py代码:
from utils.pagination import Pagination
# 客户展示页面def page(request):    ret_all=models.Customer.objects.all()    # 接收数据的次数    pagee = Pagination(request,ret_all.count())    return render(request,'board/page.html',{'ret':ret_all[pagee.start:pagee.end],'pagination':pagee.show_li                })
# 测试分页users=[{'name':'alex{}'.format(i),'pwd':'aaa{}'.format(i)}for i in range(1,302)]
def user_list(request):    page=Pagination(request,len(users))

    return render(request, 'user_list.html',                  {'data': users[page.start:page.end],                   # 'total_num':range(page_start,page_end+1)                   'html_str': page.show_li                   }                  )
html.py代码:
{% extends 'board/base.html' %}

{% block conten %}      <h2 class="sub-header">客户信息 </h2>          <div class="table-responsive">            <table class="table table-striped table-hover table-bordered">              <thead>                <tr>                    <th>序号</th>                  <th>QQ</th>                  <th>QQ昵称</th>                  <th>姓名</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 i in ret %}               <tr>                  <td>{{ forloop.counter }}</td>                   <th>{{ i.qq }}</th>                  <th>{{ i.qq_name|default:'暂无' }}</th>                  <th>{{ i.name|default:'暂无' }}</th>                  <th>{{ i.get_sex_display }}</th>                    <th>{{ i.phone|default:'暂无' }}</th>                    <th>{{ i.get_source_display }}</th>                   <th>{{ i.course }}</th>                    <th>{{ i.get_class_type_display }}</th>                    <th>{{ i.show_status }}                    </th>                    <th>{{ i.last_consult_date }}</th>                   <th>{{ i.next_date }}</th>                   <th>{{ i.consultant|default:'暂无' }}</th>                  <th>{{ i.show_class }}</th>                </tr>              {% endfor %}

              </tbody>            </table>          <div style="text-align: right">              <nav aria-label="Page navigation">        <ul class="pagination">

            {{ pagination }}        </ul>    </nav>          </div>     </div>{% endblock  %}
 




												

django项目分页的更多相关文章

  1. 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程

    点击了解更多Python课程>>> 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程 适用人群: 即将毕业的大学生,工资低工作重的白领,渴望崭露头角的职场新人, ...

  2. Django项目纪要

    开发流程 公司高层 项目立项 | 市场部门 需求分析-->需求分析说明书, 需求规格说明书 | 产品部门 产品原型-->产品 UI 前端 后端 测试 移动端 | |------------ ...

  3. vue项目和django项目交互补充,drf介绍,restful规范

    目录 一.vue项目与django项目的交互 二.drf(Django-restframework) 1. drf主要知识点 2. drf框架安装 3. web接口(WEB API) 4. restf ...

  4. Django项目-个人网站之投票模块

    Django项目之个人网站 关注公众号"轻松学编程"了解更多. Github地址:https://github.com/liangdongchang/MyWeb.git 感兴趣的可 ...

  5. Django DRF 分页

    Django DRF 分页 分页在DRF当中可以一共有三种,可以通过setttings设置,也可也通过自定义设置 PageNumberPagination 使用URL http://127.0.0.1 ...

  6. 1.-Django项目结构

    一.Django简介 Django是一个开放源代码的Web应用框架,由Python写成.采用了MTV的框架模式,即模型M,视图V和模版T.   Django基本组件: 1.基本配置文件/路由系统 2. ...

  7. centos7 apache httpd安装和配置django项目

    一.安装httpd服务 apache在centos7中是Apache HTTP server.如下对httpd的解释就是Apache HTTP Server.所以想安装apache其实是要安装http ...

  8. 终端指令操作创建Django项目

    需求:通过Django创建一个用户表和权限表. 用户表包括:用户名,邮箱,密码,管理权限. 权限表包括:普通用户,管理用户,超级用户. 权限表和用户表有一对多的关系,即用户表中的每条数据对应权限表中的 ...

  9. mac osx 上面部署Django项目 apache+mysql+mod_wsgi

    1.安装Xcode command line tools 首先,编译mysql和Homebrew需要用到Xcode command line tools,所以首先安装command line tool ...

随机推荐

  1. 第十章 企业项目开发--分布式缓存Redis(2)

    注意:本章代码是在上一章的基础上进行添加修改,上一章链接<第九章 企业项目开发--分布式缓存Redis(1)> 上一章说了ShardedJedisPool的创建过程,以及redis五种数据 ...

  2. 【计算机视觉】如何使用于仕琪老师的libfacedetect人脸检测库

    前言 最近又开始进行人脸检测方向的内容,看到于仕琪老师的多角度检测想试一下,还不清楚原理,先测试效果如何. libfacedetect人脸检测库是深圳大学于仕琪老师发布的开源库,与opencv自带的人 ...

  3. Arpa’s obvious problem and Mehrdad’s terrible solution 思维

    There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious ...

  4. B. Beautiful Paintings

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. test20181005 序列

    题意 考场30分 维护差值,考虑每次移动的变更,当前2-n位置上的差加1,1位置上的差减n-1. 然后要求的是绝对值的和,用吉司机线段树维护最大最小值.次大次小值. 期望复杂度\(O(n \log n ...

  6. 在 Laravel 5 中使用 Repository 模式实现业务逻辑和数据访问的分离

    1.概述 首先需要声明的是设计模式和使用的框架以及语言是无关的,关键是要理解设计模式背后的原则,这样才能不管你用的是什么技术,都能够在实践中实现相应的设计模式. 按照最初提出者的介绍,Reposito ...

  7. JVM监控

    jconsole 说明: 首先JConsole这个是JDK里面自带的工具  在JAVA_HOME/bin目录下,今天主要测试远程监控JVM 第一步:设置好需要远程机器的Tomcat 修改Tomcat下 ...

  8. cookie、localStorage、sessionStorage 的生命周期

    生命周期 存储 生命周期 cookie 没有设置 expires 选项时,cookie 的生命周期仅限于当前会话中,关闭浏览器意味着这次会话的结束,所以会话 cookie 仅存在于浏览器打开状态之下. ...

  9. vue-cli 下的 webpack 优化

    app bundle 可以通过组件懒加载优化 vender 优化方法: 使用 cdn,然后在 webpack 配置中指定 externals. 利用 webpack 的插件 DllPlugin 和 D ...

  10. Spring Cloud 入门 之 Zuul 篇(五)

    原文地址:Spring Cloud 入门 之 Zuul 篇(五) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,微服务会不对增加,相应的其对外开放的 API 接口 ...