效果演示:
github地址:https://github.com/mncu/django_projects/tree/master/django_projects/pagination_test
本例中总页数为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="" 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="" 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="" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a></la> <li class="disabled"><a href="" 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="" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s</a></la>'%(i,i) return html # 第三种情况 else: html = ''' <li><a href="" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a></la> <li class="disabled"><a href="" 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="" 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="/UploadFiles/2021-04-08/jquery.min.js">看看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 })以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“在django中使用自定义标签实现分页功能”评论...
更新动态
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]