Python:Django3x分页分个明白
2021/9/20 20:27:15
本文主要是介绍Python:Django3x分页分个明白,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage, InvalidPage def page_results(contacts, page, page_size): page_size = page_size if type(page_size) is int else int(page_size) paginator = Paginator(contacts, page_size) kwargs = { 'per_page': 0, 'has_previous': 0, 'number': 0, 'num_pages': 0, 'previous_page_number': 0, 'next_page_number': 0, 'count': 0, } try: page = page if type(page) is int else int(page) contacts = paginator.page(page) kwargs['per_page'] = contacts.paginator.per_page kwargs['has_previous'] = contacts.has_previous kwargs['number'] = contacts.number kwargs['num_pages'] = contacts.paginator.num_pages kwargs['previous_page_number'] = contacts.previous_page_number kwargs['next_page_number'] = contacts.next_page_number kwargs['count'] = contacts.paginator.count # TODO: 开始捕获异常 except PageNotAnInteger: contacts = paginator.page(1) except InvalidPage: # 如果请求的页数不存在, 重定向页面 contacts = paginator.page(1) except EmptyPage: # 如果请求的页数不在合法的页数范围内,返回结果的最后一页。 contacts = paginator.page(paginator.num_pages) kwargs['per_page'] = contacts.paginator.per_page kwargs['has_previous'] = contacts.has_previous kwargs['number'] = contacts.number kwargs['num_pages'] = contacts.paginator.num_pages kwargs['previous_page_number'] = contacts.previous_page_number kwargs['next_page_number'] = contacts.next_page_number kwargs['count'] = contacts.paginator.count kwargs['page'] = page kwargs['page_size'] = page_size return contacts, kwargs
这篇关于Python:Django3x分页分个明白的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享