在django中使用自定义标签实现分页功能


Posted in Python onJuly 04, 2017

效果演示:

 在django中使用自定义标签实现分页功能

github地址:https://github.com/mncu/django_projects/tree/master/django_projects/pagination_test

在django中使用自定义标签实现分页功能

本例中总页数为30页,显示页数为12页,当前页的前排页数为4,后排页数为5

将分页分为三种情况:

1   当前页为第1页到第7页的时候,无省略页,且12个位置的内容是不变

2  当前页为第8页到第25页时,位置1与位置2内容不变,当前页一直处于位置7,

3  当前页为第25页到第30页时,位置1与位置2内容不变,位置8到位置12的内容不变,当前页在位置8到位置12之中变换

自定义标签代码:

from django import template

register = template.Library()

@register.assignment_tag
def pagination(current_page,paginator,num_of_displaypages=10,num_of_backpages=4):
 # current_page is a django.core.paginator.Page 's instance
 # paginator is a django.core.paginator.Paginator 's instance
 #
 num_of_frontpages = num_of_displaypages - num_of_backpages -3
 html=''

 # 当总页数小于等于 显示页数 时,则将总页数全部显示
 if paginator.num_pages <= num_of_displaypages :
  for i in range(1,paginator.num_pages+1):
   html+= '<li ><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s </a></li>'%(i,i)
  return html
 # 第一种情况 
 elif current_page.number <= num_of_displaypages-num_of_backpages:
  for i in range(1,num_of_displaypages+1):
   html+= '<li ><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s </a></li>'%(i,i)
  return html
 # 第二种情况
 elif num_of_displaypages-num_of_frontpages <= current_page.number <= paginator.num_pages-num_of_backpages :
  html = '''
   <li><a href="?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a></la>
   <li class="disabled"><a href="?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >...</a></la>

  '''
  for i in range(current_page.number-num_of_frontpages,current_page.number+num_of_backpages+1):
   html+='<li><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s</a></la>'%(i,i)
  return html
 # 第三种情况
 else:
  html = '''
   <li><a href="?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a></la>
   <li class="disabled"><a href="?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >...</a></la>

  '''
  for i in range(paginator.num_pages-num_of_backpages-num_of_frontpages,paginator.num_pages+1):
   html+='<li><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s</a></la>'%(i,i)
  return html

来看html代码

{% load mytags %}
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <!-- 新 Bootstrap 核心 CSS 文件 -->
 <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="external nofollow" >

 <!-- 可选的Bootstrap主题文件(一般不用引入) -->
 <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css" rel="external nofollow" >

 <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
 <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>

 <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
 <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>
{{ current_page.object_list }}

 <nav>

  <ul class="pagination">
  {% if current_page.has_previous %}
  <li ><a href="?page={{ current_page.previous_page_number }}" rel="external nofollow" >上一页 <span class="sr-only">(current)</span></a></li>
  {% endif %}

  {% pagination current_page paginator 12 5 as page_list %} <!-- 引用自定义标签,并传入参数 -->

  {{ page_list|safe }} <!-- 显示 -->

  {% if current_page.has_next %}
  <li><a href="?page={{ current_page.next_page_number }}" rel="external nofollow" >下一页 <span class="sr-only">(current)</span></a></li>
  {% endif %}
  </ul>

 </nav>

<script>
 $(document).ready(function(){
  $('.pagination li a').each(function(){

   if ( $(this).html() == {{ current_page.number }} ){
    $(this).parent().addClass('active')
   }
  });

 })


</script>
</body>
</html>

看看view函数:

from django.shortcuts import render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

# Create your views here.


def index(request):

 obj_list = ['page01','page02','page03','page04','page05','page06','page07','page08','page09','page10',
    'page11','page12','page13','page14','page15','page16','page17','page18','page19','page20',
    'page21','page22','page23','page24','page25','page26','page27','page28','page29','page30',]
 #create a paginator instance
 paginator = Paginator(obj_list,1)

 #Get the page_number of current page
 current_page_num = request.GET.get('page')

 try:
  current_page = paginator.page(current_page_num)
 except PageNotAnInteger:
  # If page is not an integer, deliver first page.
  current_page = paginator.page(1)
 except EmptyPage:
  # If page is out of range (e.g. 9999), deliver last page of results.
  current_page = paginator.page(paginator.num_pages)
 return render(request,'index.html',
     {'current_page': current_page,
     'paginator': paginator

     })

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中实现三目运算的方法
Jun 21 Python
Python操作SQLite数据库的方法详解
Jun 16 Python
numpy排序与集合运算用法示例
Dec 15 Python
利用Python实现微信找房机器人实例教程
Mar 10 Python
浅谈Python反射 &amp; 单例模式
Mar 21 Python
pandas对dataFrame中某一个列的数据进行处理的方法
Jul 08 Python
python的命名规则知识点总结
Oct 04 Python
tensorflow 只恢复部分模型参数的实例
Jan 06 Python
在Django中自定义filter并在template中的使用详解
May 19 Python
python实现学生成绩测评系统
Jun 22 Python
python爬虫使用正则爬取网站的实现
Aug 03 Python
基于python的opencv图像处理实现对斑马线的检测示例
Nov 29 Python
详解django中自定义标签和过滤器
Jul 03 #Python
pygame实现弹力球及其变速效果
Jul 03 #Python
[原创]使用豆瓣提供的国内pypi源
Jul 02 #Python
python中安装Scrapy模块依赖包汇总
Jul 02 #Python
Python使用filetype精确判断文件类型
Jul 02 #Python
使用python实现tcp自动重连
Jul 02 #Python
详解python3中socket套接字的编码问题解决
Jul 01 #Python
You might like
php把数组值转换成键的方法
2015/07/13 PHP
php技巧小结【推荐】
2017/01/19 PHP
Yii2.0 RESTful API 基础配置教程详解
2018/12/26 PHP
Yii框架通过请求组件处理get,post请求的方法分析
2019/09/03 PHP
ExtJs3.0中Store添加 baseParams 的Bug
2010/03/10 Javascript
ECMAScript 创建自己的js类库
2012/11/22 Javascript
jquery怎样实现ajax联动框(一)
2013/03/08 Javascript
利用window.name实现windowStorage代码分享
2014/01/02 Javascript
jQuery实现简单二级下拉菜单
2015/04/12 Javascript
Node.js返回JSONP详解
2016/05/18 Javascript
javascript正则表达式中分组详解
2016/07/17 Javascript
JS和jQuery通过this获取html标签中的属性值(实例代码)
2017/09/11 jQuery
iframe高度自适应及隐藏滚动条的实例详解
2017/09/29 Javascript
微信小程序实现手指触摸画板
2018/07/09 Javascript
vue根据值给予不同class的实例
2018/09/29 Javascript
微信小程序如何获取手机验证码
2018/11/04 Javascript
[55:02]2014 DOTA2国际邀请赛中国区预选赛 HGT VS Orenda
2014/05/21 DOTA
用Python编写生成树状结构的文件目录的脚本的教程
2015/05/04 Python
Python实现删除文件但保留指定文件
2015/06/21 Python
python 网络爬虫初级实现代码
2016/02/27 Python
Python搭建HTTP服务器和FTP服务器
2017/03/09 Python
Python如何读取MySQL数据库表数据
2017/03/11 Python
Django中url的反向查询的方法
2018/03/14 Python
Python使用matplotlib绘制多个图形单独显示的方法示例
2018/03/14 Python
python 并发编程 阻塞IO模型原理解析
2019/08/20 Python
python3发送request请求及查看返回结果实例
2020/04/30 Python
浅析CSS3 中的 transition,transform,translate之间区别和作用
2020/03/26 HTML / CSS
德国鞋子网上商店:Omoda.de
2017/03/31 全球购物
Lou & Grey美国官网:主打舒适性面料服饰
2017/12/21 全球购物
澳大利亚最受欢迎的女士度假服装:Kabana Shop
2020/10/10 全球购物
你常见到的runtime exception
2016/09/05 面试题
《匆匆》教学反思
2014/02/22 职场文书
物流管理专业毕业生求职信
2014/03/23 职场文书
大型会议策划方案
2014/05/17 职场文书
霸气押韵的班级口号
2014/06/09 职场文书
oracle DGMGRL ORA-16603报错的解决方法(DG Broker)
2021/04/06 Oracle