Django Pagination
Django provides a few classes that help you manage paginated data – that is, data that’s split across several pages, with “Previous/Next” links. These classes live in django/core/paginator.py.
Example
Give Paginator a list of objects, plus the number of items you’d like to have on each page, and it gives you methods for accessing the items for each page:
- >>> from django.core.paginator import Paginator
- >>> objects = ['john', 'paul', 'george', 'ringo']
- >>> p = Paginator(objects, 2)
- >>> p.count
- 4
- >>> p.num_pages
- 2
- >>> p.page_range
- [1, 2]
- >>> page1 = p.page(1)
- >>> page1
- <Page 1 of 2>
- >>> page1.object_list
- ['john', 'paul']
- >>> page2 = p.page(2)
- >>> page2.object_list
- ['george', 'ringo']
- >>> page2.has_next()
- False
- >>> page2.has_previous()
- True
- >>> page2.has_other_pages()
- True
- >>> page2.next_page_number()
- Traceback (most recent call last):
- ...
- EmptyPage: That page contains no results
- >>> page2.previous_page_number()
- 1
- >>> page2.start_index() # The 1-based index of the first item on this page
- 3
- >>> page2.end_index() # The 1-based index of the last item on this page
- 4
- >>> p.page(0)
- Traceback (most recent call last):
- ...
- EmptyPage: That page number is less than 1
- >>> p.page(3)
- Traceback (most recent call last):
- ...
- EmptyPage: That page contains no results
Note
Note that you can give Paginator a list/tuple, a Django QuerySet, or any other object with a count() or __len__()method. When determining the number of objects contained in the passed object, Paginator will first try callingcount(), then fallback to using len() if the passed object has no count() method. This allows objects such as Django’s QuerySet to use a more efficient count() method when available.
Using Paginator in a view
Here’s a slightly more complex example using Paginator in a view to paginate a queryset. We give both the view and the accompanying template to show how you can display the results. This example assumes you have a Contacts model that has already been imported.
The view function looks like this:
- from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
- def listing(request):
- contact_list = Contacts.objects.all()
- paginator = Paginator(contact_list, 25) # Show 25 contacts per page
- page = request.GET.get('page')
- try:
- contacts = paginator.page(page)
- except PageNotAnInteger:
- # If page is not an integer, deliver first page.
- contacts = paginator.page(1)
- except EmptyPage:
- # If page is out of range (e.g. 9999), deliver last page of results.
- contacts = paginator.page(paginator.num_pages)
- return render_to_response('list.html', {"contacts": contacts})
In the template list.html, you’ll want to include navigation between pages along with any interesting information from the objects themselves:
- {% for contact in contacts %}
- {# Each "contact" is a Contact model object. #}
- {{ contact.full_name|upper }}<br />
- ...
- {% endfor %}
- <div class="pagination">
- <span class="step-links">
- {% if contacts.has_previous %}
- <a href="?page={{ contacts.previous_page_number }}">previous</a>
- {% endif %}
- <span class="current">
- Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
- </span>
- {% if contacts.has_next %}
- <a href="?page={{ contacts.next_page_number }}">next</a>
- {% endif %}
- </span>
- </div>
Django Pagination的更多相关文章
- 通过 Django Pagination 实现简单分页
作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 当博客上发布的文章越来越多时,通常需要进行分页显示,以免所有的文章都堆积在一个页面, ...
- python 学习笔记十八 django深入学习三 分页,自定义标签,权限机制
django Pagination(分页) django 自带的分页功能非常强大,我们来看一个简单的练习示例: #导入Paginator>>> from django.core.p ...
- django分页linaro-django-pagination
1.安装linaro-django-pagination settings INSTALLED_APPS = ( # ... 'linaro_django_pagination', ) MIDDLEW ...
- [py][mx]django分页第三方模块django-pure-pagination
前台的这些数据都是从后台取来的 分页模块django-pure-pagination - 一款基于django pagination封装的更好用的分页模块 https://github.com/jam ...
- django学习笔记——搭建博客网站
1. 配置环境,创建django工程 虚拟环境下建立Django工程,即创建一个包含python脚本文件和django配置文件的目录或者文件夹,其中manage.py是django的工程管理助手.(可 ...
- Django 博客开发教程目录索引
Django 博客开发教程目录索引 本项目适合 0 基础的 Django 开发新人. 项目演示地址:Black & White,代码 GitHub 仓库地址:zmrenwu/django-bl ...
- 稳定易用的 Django 分页库,完善分页功能
作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在 通过 Django Pagination 实现简单分页 中,我们实现了一个简单的 ...
- django 添加分页功能的包
Django pagination based upon the core pagination module
- ListView 中 的 分页
Django Pagination 简单分页 当博客上发布的文章越来越多时,通常需要进行分页显示,以免所有的文章都堆积在一个页面,影响用户体验.Django 内置的 Pagination 能够帮助我 ...
随机推荐
- 64_p2
perl-Class-XSAccessor-1.19-10.fc26.x86_64.rpm 12-Feb-2017 15:12 48098 perl-Clipboard-0.13-16.fc26.no ...
- MySQL登录问题1045 (28000)处理步骤【原创】
MySQL登录问题1045 (28000) 俩台服务器主从复制,从的同步账号无法远程登录主服务器.报错ERROR 1045 (28000): Access denied for user 'root ...
- Redis安装和客户端cli常见操作
安装Redis $ wget http://download.redis.io/releases/redis-4.0.6.tar.gz $ tar xzf redis-4.0.6.tar.gz $ c ...
- Java显式锁学习总结之四:ReentrantLock源码分析
概述 ReentrantLock,即重入锁,是一个和synchronized关键字等价的,支持线程重入的互斥锁.只是在synchronized已有功能基础上添加了一些扩展功能. 除了支持可中断获取锁. ...
- Java容器---Arrays & Collections工具类
1.Array & Arrays 与Collection & Collections区别 (1)Collection": 是一个接口,与其子类共同组成一个Collection ...
- MySQL-事务特性
1. 事务概念引入: 现实生活中,我们往往经常会进行转账操作,转账操作可以分为两部分来完成,转入和转出.只有这两部分都完成了才可以认为是转账成功.在数据库中,这个过程是使用两条语句来完成的,如果其中任 ...
- Jmeter-----保存到响应文件
在jmeter中使用保存响应到文件 ------适用于非GUI模式执行脚本时,无法查看报错的信息. 1.添加组件: 2.各个配置项说明: 1.名称:即组件在整个测试计划中的名称显示,建议设置为用意义的 ...
- 【51nod】1227 平均最小公倍数
题解 这个故事告诉们数论函数不要往分式上跑,你推不出来 好久没推式子了这么明显的转化我都忘了= = 首先\(A(n) = \frac{1}{n} \sum_{i = 1}^{n} \frac{i * ...
- bzoj1452 最大流
很明显最大流.. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #defi ...
- 解决win10下git闪退
网上找了很多方法,只有这个是有用的,记录下来. 问题描述 在git官网下载了软件,安装之后,git Bash出现闪退现象,同时在当前文件夹下面会生成一个mintty.exe.stackdump的文件. ...