在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 25 Python
基于python的汉字转GBK码实现代码
Feb 19 Python
Python中对元组和列表按条件进行排序的方法示例
Nov 10 Python
python实现12306抢票及自动邮件发送提醒付款功能
Mar 08 Python
python图形开发GUI库pyqt5的基本使用方法详解
Feb 14 Python
matplotlib 对坐标的控制,加图例注释的操作
Apr 17 Python
解决python 执行sql语句时所传参数含有单引号的问题
Jun 06 Python
Virtualenv 搭建 Py项目运行环境的教程详解
Jun 22 Python
python利用google翻译方法实例(翻译字幕文件)
Sep 21 Python
如何通过安装HomeBrew来安装Python3
Dec 23 Python
Django后端按照日期查询的方法教程
Feb 28 Python
PyTorch 如何设置随机数种子使结果可复现
May 12 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/07/17 PHP
PHP 获取 ping 时间的实现方法
2017/09/29 PHP
Javascript无阻塞加载具体方式
2013/06/28 Javascript
json数据的列循环示例
2013/09/06 Javascript
Jquery attr(&quot;checked&quot;) 返回checked或undefined 获取选中失效
2013/10/10 Javascript
jQuery设置和获取HTML、文本和值示例
2014/07/08 Javascript
jQuery实现表单步骤流程导航代码分享
2015/08/28 Javascript
jQuery之动画效果大全
2016/11/09 Javascript
JavaScript构建自己的对象示例
2016/11/29 Javascript
jQuery EasyUI Panel面板组件使用详解
2017/02/28 Javascript
JS中Safari浏览器中的Date
2017/07/17 Javascript
Angular4学习笔记之准备和环境搭建项目
2017/08/01 Javascript
Array数组对象中的forEach、map、filter及reduce详析
2018/08/02 Javascript
layui框架table 数据表格的方法级渲染详解
2018/08/19 Javascript
ng-events类似ionic中Events的angular全局事件
2018/09/05 Javascript
webpack + vue 打包生成公共配置文件(域名) 方便动态修改
2019/08/29 Javascript
JS实现拖动模糊框特效
2020/08/25 Javascript
[01:14:10]2014 DOTA2国际邀请赛中国区预选赛 SPD-GAMING VS Orenda
2014/05/22 DOTA
[02:17]《辉夜杯》TRG战队巡礼
2015/10/26 DOTA
详解Python多线程Selenium跨浏览器测试
2017/04/01 Python
Python 多核并行计算的示例代码
2017/11/07 Python
scrapy爬虫完整实例
2018/01/25 Python
Python实现按照指定要求逆序输出一个数字的方法
2018/04/19 Python
Python获取统计自己的qq群成员信息的方法
2019/11/15 Python
Django用户认证系统如何实现自定义
2020/11/12 Python
Schutz鞋官方网站:Schutz Shoes
2017/12/13 全球购物
NFL Game Pass欧洲:在线观看NFL比赛直播和点播,以高清质量播放
2018/08/30 全球购物
泰国网上购物:Shopee泰国
2018/09/14 全球购物
Carolina工作鞋官网:Carolina Footwear
2019/03/14 全球购物
德国大型箱包和皮具商店:Koffer
2019/10/01 全球购物
信息部岗位职责
2013/11/12 职场文书
车间班组长岗位职责
2013/11/13 职场文书
新驾驶员个人自我评价
2014/01/03 职场文书
爱护公共设施的标语
2014/06/24 职场文书
汉语专业毕业生自荐信
2014/07/06 职场文书
OpenCV-Python实现人脸美白算法的实例
2021/06/11 Python