在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中os.path用法分析
Jan 15 Python
用 Python 爬了爬自己的微信朋友(实例讲解)
Aug 25 Python
Python使用ConfigParser模块操作配置文件的方法
Jun 29 Python
python 检查文件mime类型的方法
Dec 08 Python
基于Python新建用户并产生随机密码过程解析
Oct 08 Python
Python高阶函数、常用内置函数用法实例分析
Dec 26 Python
python使用SQLAlchemy操作MySQL
Jan 02 Python
在pycharm中为项目导入anacodna环境的操作方法
Feb 12 Python
python 截取XML中bndbox的坐标中的图像,另存为jpg的实例
Mar 10 Python
python 比较字典value的最大值的几种方法
Apr 17 Python
Django def clean()函数对表单中的数据进行验证操作
Jul 09 Python
怎么解决pycharm license Acti的方法
Oct 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 adodb操作mysql数据库
2009/03/19 PHP
window+nginx+php环境配置 附配置搭配说明
2010/12/29 PHP
解析用PHP读写音频文件信息的详解(支持WMA和MP3)
2013/05/10 PHP
thinkphp模板继承实例简述
2014/11/26 PHP
Laravel使用PHPQRCODE实现生成带有LOGO的二维码图片功能示例
2017/07/07 PHP
浅谈javascript对象模型和function对象
2014/12/26 Javascript
JavaScript实现常用二级省市级联下拉列表的方法
2015/03/25 Javascript
AngularJs html compiler详解及示例代码
2016/09/01 Javascript
AngularJS指令用法详解
2016/11/02 Javascript
nodejs创建简易web服务器与文件读写的实例
2017/09/07 NodeJs
Windows下Node.js安装及环境配置方法
2017/09/18 Javascript
js计算两个时间差 天 时 分 秒 毫秒的代码
2019/05/21 Javascript
js实现前端界面导航栏下拉列表
2020/08/27 Javascript
小程序实现录音功能
2020/09/22 Javascript
[01:22:19]EG vs TNC Supermajor小组赛B组败者组第一轮 BO3 第二场 6.2
2018/06/03 DOTA
[01:03:18]DOTA2-DPC中国联赛 正赛 RNG vs Dynasty BO3 第一场 1月29日
2021/03/11 DOTA
Python开发编码规范
2006/09/08 Python
python 获取文件列表(或是目录例表)
2009/03/25 Python
Python自动连接ssh的方法
2015/03/07 Python
详解Django缓存处理中Vary头部的使用
2015/07/24 Python
详解Python3操作Mongodb简明易懂教程
2017/05/25 Python
python实现输入数字的连续加减方法
2018/06/22 Python
pyqt5 实现工具栏文字图片同时显示
2019/06/13 Python
用python爬取历史天气数据的方法示例
2019/12/30 Python
详解python itertools功能
2020/02/07 Python
在python3中实现查找数组中最接近与某值的元素操作
2020/02/29 Python
Python matplotlib修改默认字体的操作
2020/03/05 Python
Python求两个字符串最长公共子序列代码实例
2020/03/05 Python
python如何判断IP地址合法性
2020/04/05 Python
投标邀请书范文
2014/01/31 职场文书
八一建军节演讲稿
2014/09/10 职场文书
派出所副所长四风问题个人整改措施思想汇报
2014/10/13 职场文书
大学生暑期社会实践证明范本
2014/10/24 职场文书
三八妇女节致辞
2015/07/31 职场文书
python3.9之你应该知道的新特性详解
2021/04/29 Python
浅谈Laravel中使用Slack进行异常通知
2021/05/29 PHP