在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绘图库Matplotlib的安装
Jul 03 Python
用Python登录Gmail并发送Gmail邮件的教程
Apr 17 Python
Django实现组合搜索的方法示例
Jan 23 Python
1分钟快速生成用于网页内容提取的xslt
Feb 23 Python
浅谈Python中的私有变量
Feb 28 Python
Python处理菜单消息操作示例【基于win32ui模块】
May 09 Python
python中文编码与json中文输出问题详解
Aug 24 Python
eclipse创建python项目步骤详解
May 10 Python
Python 中使用 PyMySQL模块操作数据库的方法
Nov 10 Python
JupyterNotebook 输出窗口的显示效果调整方法
Apr 13 Python
基于tf.shape(tensor)和tensor.shape()的区别说明
Jun 30 Python
python数字图像处理之对比度与亮度调整示例
Jun 28 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中的内存管理问题
2011/08/31 PHP
用php实现百度网盘图片直链的代码分享
2012/11/01 PHP
测试php函数的方法
2013/11/13 PHP
javascript document.images实例
2008/05/27 Javascript
js 禁用只读文本框获得焦点时的退格键
2010/04/25 Javascript
js 返回时间戳所对应的具体时间
2010/07/20 Javascript
jQuery validate 中文API 附validate.js中文api手册
2010/07/31 Javascript
如何在一个页面显示多个百度地图
2013/04/07 Javascript
window.opener用法和用途实例介绍
2013/08/19 Javascript
jQuery对于显示和隐藏等常用状态的判断方法
2014/12/13 Javascript
jQuery图片特效插件Revealing实现拉伸放大
2015/04/22 Javascript
vue.js+boostrap项目实践(案例详解)
2016/09/21 Javascript
jQuery命名空间与闭包用法示例
2017/01/12 Javascript
JS正则表达式判断有效数实例代码
2017/03/13 Javascript
Vue生命周期示例详解
2017/04/12 Javascript
详解Vue CLI3配置解析之css.extract
2018/09/14 Javascript
JavaScript对象的特性与实践应用深入详解
2018/12/30 Javascript
layui table设置某一行的字体颜色方法
2019/09/05 Javascript
详解Webpack抽离第三方类库以及common解决方案
2020/03/30 Javascript
5个你不知道的JavaScript字符串处理库(小结)
2020/06/01 Javascript
Python中urllib2模块的8个使用细节分享
2015/01/01 Python
Python实现的井字棋(Tic Tac Toe)游戏示例
2018/01/31 Python
Python简单实现网页内容抓取功能示例
2018/06/07 Python
浅谈Python 多进程默认不能共享全局变量的问题
2019/01/11 Python
PySide和PyQt加载ui文件的两种方法
2019/02/27 Python
Python read函数按字节(字符)读取文件的实现
2019/07/03 Python
浅谈Python的方法解析顺序(MRO)
2020/03/05 Python
Django import export实现数据库导入导出方式
2020/04/03 Python
python中的unittest框架实例详解
2021/02/05 Python
日本钓鱼渔具和户外用品网上商店:naturum
2016/08/07 全球购物
葡萄牙鞋子品牌:Fair
2016/12/10 全球购物
房地产销售计划书
2014/01/10 职场文书
安全生产大检查方案
2014/05/07 职场文书
党员教师学习党的群众路线教育实践活动心得体会
2014/10/31 职场文书
2016圣诞节贺卡寄语
2015/12/07 职场文书
详解MySQL的主键查询为什么这么快
2022/04/03 MySQL