在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实现多线程网页爬虫
Sep 06 Python
Python用模块pytz来转换时区
Aug 19 Python
浅谈利用numpy对矩阵进行归一化处理的方法
Jul 11 Python
python使用response.read()接收json数据的实例
Dec 19 Python
简单了解python的内存管理机制
Jul 08 Python
Flask框架学习笔记之表单基础介绍与表单提交方式
Aug 12 Python
JupyterNotebook设置Python环境的方法步骤
Dec 03 Python
基于python监控程序是否关闭
Jan 14 Python
Python 如何操作 SQLite 数据库
Aug 17 Python
python3 使用ssh隧道连接mysql的操作
Dec 05 Python
删除pycharm鼠标右键快捷键打开项目的操作
Jan 16 Python
Python常用配置文件ini、json、yaml读写总结
Jul 09 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 工厂模式使用方法
2010/05/18 PHP
PHP文件注释标记及规范小结
2012/04/01 PHP
从零开始学YII2框架(一)通过Composer安装Yii2框架
2014/08/20 PHP
PHP7.1方括号数组符号多值复制及指定键值赋值用法分析
2016/09/26 PHP
laravel框架中间件简单使用方法示例
2020/01/25 PHP
分享几种好用的PHP自定义加密函数(可逆/不可逆)
2020/09/15 PHP
javascript中对Attr(dom中属性)的操作示例讲解
2013/12/02 Javascript
javascript处理a标签超链接默认事件的方法
2015/06/29 Javascript
javascript经典特效分享 手风琴、轮播图、图片滑动
2016/09/14 Javascript
微信小程序实战之自定义抽屉菜单(7)
2017/04/18 Javascript
使用bootstrap实现下拉框搜索功能的实例讲解
2018/08/10 Javascript
菊花转动的jquery加载动画效果
2018/08/19 jQuery
解决vue中修改export default中脚本报一大堆错的问题
2018/08/27 Javascript
js中的闭包实例展示
2018/11/01 Javascript
vue 子组件修改data或调用操作
2020/08/07 Javascript
JavaScript 实现拖拽效果组件功能(兼容移动端)
2020/11/11 Javascript
vue 数据遍历筛选 过滤 排序的应用操作
2020/11/17 Javascript
在Python中操作日期和时间之gmtime()方法的使用
2015/05/22 Python
Python之父谈Python的未来形式
2016/07/01 Python
Python实现列表删除重复元素的三种常用方法分析
2017/11/24 Python
django中静态文件配置static的方法
2018/05/20 Python
在Mac下使用python实现简单的目录树展示方法
2018/11/01 Python
对python 生成拼接xml报文的示例详解
2018/12/28 Python
python自动化之Ansible的安装教程
2019/06/13 Python
pandas 如何分割字符的实现方法
2019/07/29 Python
基于K.image_data_format() == 'channels_first' 的理解
2020/06/29 Python
Python QT组件库qtwidgets的使用
2020/11/02 Python
python 利用opencv实现图像网络传输
2020/11/12 Python
pycharm 关闭search everywhere的解决操作
2021/01/15 Python
大学生毕业求职的自我评价
2013/09/29 职场文书
土木工程毕业生自荐信
2013/11/12 职场文书
领导班子四风对照检查材料范文
2014/09/27 职场文书
青年志愿者服务活动总结
2015/05/06 职场文书
关于远足的感想
2015/08/10 职场文书
详解Html5项目适配系统深色模式方案总结
2021/04/14 HTML / CSS
Java基础——Map集合
2022/04/01 Java/Android