Django基础009--Paginator分页
2024-09-03 23:54:03
1.引入
from django.core.paginator import Paginator
2.Paginator对象提供的方法
articles = models.Article.objects.all()
page_obj = Paginator(articles,5)
print('总条数',page_obj.count)
print('有多少页',page_obj.num_pages)
print('页码列表',page_obj.page_range)
3.数据集提供的方法
page_data = page_obj.get_page(page_num)#number代表获取第几页的数据
print(page_data.has_other_pages())#是否有上一页或者下一页
print(page_data.has_next())#是否有下一页
print(page_data.has_previous())#是否有上一页
print(page_data.next_page_number())#下一页页码
print(page_data.previous_page_number())#上一页页码
print(page_data.number)#当前页的页码
4.实例
def index(request):
page_num = request.GET.get('page')
print('页面传递过来的参数',page_num)
categories = models.Category.objects.all()
articles = models.Article.objects.all()
page_obj = Paginator(articles,5)
# print('总条数',page_obj.count)
# print('有多少页',page_obj.num_pages)
# print('页码列表',page_obj.page_range)
page_data = page_obj.get_page(page_num)#number代表获取第几页的数据
# print(page_data.has_other_pages())#是否有上一页或者下一页
# print(page_data.has_next())#是否有下一页
# print(page_data.has_previous())#是否有上一页
# print(page_data.next_page_number())#下一页页码
# print(page_data.previous_page_number())#上一页页码
print(page_data.number)#当前页的页码
return render(request,'index.html',{'articles':page_data,'page_obj':page_obj})
5.前端代码
{% if articles.has_other_pages %}
<div class="text-center mt-2 mt-sm-1 mt-md-0 mb-3 f-16"> {% if articles.has_previous %}
<a class="text-success" href="/articles?page={{ articles.previous_page_number }}">上一页</a>
{% endif %} {% for num in page_obj.page_range %}
{% if num == articles.number %}
<span class="mx-2" style="background-color: #0D7377"><a href="/articles?page={{ num }}">{{num}}</a></span>
{% else %}
<span class="mx-2"><a href="/articles?page={{ num }}">{{num}}</a></span>
{% endif %}
{% endfor %} {% if articles.has_next %}
<a class="text-success" href="/articles?page={{ articles.next_page_number }}">下一页</a>
{% endif %} </div>
{% endif %}
最新文章
- spring-mvc注解(mvc:annotation-driver,JSON,配置详解)
- Beta--项目冲刺第七天
- Java学习第三天160818 表单 框架 下拉列表等
- JS 之性能优化(1)
- C语言知识总结(3)
- GDI+基础(3)
- Swift自定义Class实现Hashable
- ThinkPHP框架下基于RBAC的权限控制模式详解
- Javascript-正则表达式-开发中的使用.
- python第二天3.1
- JS中encodeURI()、decodeURI()、encodeURIComponent()和decodeURIComponent()编码与解码
- cocos2d-x JS 富文本
- 关于EXCEL if、countif 在查找数据的用法
- 鼠标滑过元素,div显示,并根据scrollTop向下移动
- 您无法登陆系统。原因可能是您的用户记录或所属的业务部门在Microoft Dynamics CRM中已被禁用
- (原+译)使用numpy.savez保存字典后读取的问题
- 灯光探测器LightProbe[Unity]
- 使用dbms_output.put_line打印异常所在的行
- JAVA中神奇的双刃剑--Unsafe
- [转]QList内存释放
热门文章
- [论文阅读笔记] Unsupervised Attributed Network Embedding via Cross Fusion
- MySQL 三万字精华总结
- GO语言的JSON01---序列化
- Python+Selenium学习笔记1 - pip命令
- 教你三种jQuery框架实现元素显示及隐藏动画方式
- 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数
- 干货 | LuatOS BSP移植教程,简单到复制粘贴!!!
- 【题解】斐波拉契 luogu3938
- css--flex弹性布局详解和使用
- Arduino库和STM32的寄存器、标准库、HAL库、LL库开发比较之GPIO