在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使用multiprocessing模块实现带回调函数的异步调用方法
Apr 18 Python
Python 装饰器使用详解
Jul 29 Python
Python编写Windows Service服务程序
Jan 04 Python
Odoo中如何生成唯一不重复的序列号详解
Feb 10 Python
python远程邮件控制电脑升级版
May 23 Python
Python 的字典(Dict)是如何存储的
Jul 05 Python
Python 实现日志同时输出到屏幕和文件
Feb 19 Python
解决Keras 中加入lambda层无法正常载入模型问题
Jun 16 Python
Python调用系统命令os.system()和os.popen()的实现
Dec 31 Python
Python答题卡识别并给出分数的实现代码
Jun 22 Python
一文搞懂python异常处理、模块与包
Jun 26 Python
python解析照片拍摄时间进行图片整理
Jul 23 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&amp;mysql(一)
2006/10/09 PHP
我的论坛源代码(九)
2006/10/09 PHP
php 抽象类的简单应用
2011/09/06 PHP
php创建sprite
2014/02/11 PHP
Jquery 插件学习实例1 插件制作说明与tableUI优化
2010/04/02 Javascript
Js制作简单弹出层DIV在页面居中 中间显示遮罩的具体方法
2013/08/08 Javascript
动态标签 悬停效果 延迟加载示例代码
2013/11/21 Javascript
jQuery中Dom的基本操作小结
2014/01/23 Javascript
javascript类型转换示例
2014/04/29 Javascript
jquery实现的一个文章自定义分段显示功能
2014/05/23 Javascript
推荐5 个常用的JavaScript调试技巧
2015/01/08 Javascript
jQuery实现页面内锚点平滑跳转特效的方法总结
2015/05/11 Javascript
js实现鼠标划过给div加透明度的方法
2015/05/25 Javascript
详解maxlength属性在textarea里奇怪的表现
2015/12/27 Javascript
jQuery ajax提交Form表单实例(附demo源码)
2016/04/06 Javascript
canvas实现绘制吃豆鱼效果
2017/01/12 Javascript
jQuery插件扩展操作入门示例
2017/01/16 Javascript
bootstrap table单元格新增行并编辑
2017/05/19 Javascript
Angularjs验证用户输入的字符串是否为日期时间
2017/06/01 Javascript
解决JSON.stringify()自动将中文转译成unicode的问题
2018/01/05 Javascript
微信小程序用户信息encryptedData详解
2018/08/24 Javascript
详解vue项目接入微信JSSDK的坑
2018/12/14 Javascript
原生js通过一行代码实现简易轮播图
2019/06/05 Javascript
简谈创建React Component的几种方式
2019/06/15 Javascript
vue.js实现点击图标放大离开时缩小的代码
2021/01/27 Vue.js
[02:54]辉夜杯主赛事第二日败者组 iG.V赛后采访
2015/12/26 DOTA
python中日期和时间格式化输出的方法小结
2015/03/19 Python
Python 3中的yield from语法详解
2017/01/18 Python
python程序 创建多线程过程详解
2019/09/23 Python
如何写出好的Java代码
2014/04/25 面试题
介绍一下你对SOA的认识
2016/04/24 面试题
见习期自我鉴定范文
2014/03/19 职场文书
教师党员学习十八届四中全会思想汇报
2014/11/03 职场文书
优秀团员自我评价
2015/03/10 职场文书
开学第一周值周总结
2015/07/16 职场文书
Golang 字符串的常见操作
2022/04/19 Golang