2020-11-04 django分页中,请求页号等于paginator.num_pages时报空页错误

采用django的Paginator来进行分页,当请求页号等于paginator.num_pages时报空页错误,一直弄不明白什么原因,请求大家帮助。

URL配置链接为:comm/comlist/ctype/<int:类目ID>/<int:每页数量>-<int:请求页号>

Views中的 函数如下:

def getTypeCommList(request, distinct_class, class_id,perpage_num,page_number=None): 

dclass = distinct_class

if distinct_class == 'ctype':
    singleCommType = CommodityType.objects.get(pk=class_id)
    showCommList = Commodity.objects.filter(is_selling=True).filter(type=singleCommType )
elif distinct_class == 'cseries':
    singleCommType = Series.objects.get(pk=class_id)
    showCommList = Commodity.objects.filter(is_selling=True).filter(series=singleCommType)
else:
    singleCommType = Level.objects.get(pk=class_id)
    showCommList = Commodity.objects.filter(is_selling=True).filter(level=singleCommType)

print("当前商品总数:",showCommList.count())

print("当前获得的每页显示数",perpage_num)
print("当前页号",page_number)

showcommCount = showCommList.count()
tem_perpage_num = int(perpage_num)
if tem_perpage_num &gt; showcommCount:
    tem_perpage_num = int(showcommCount)

print("调整后的每页数量:",tem_perpage_num)

# page_num = request.GET.get('page', default='1')
page_num = 1
if page_number is not None:
    page_num = int(page_number)

paginator = Paginator(showCommList, tem_perpage_num)
# 由于common中的CustomerPaginator继承了Paginator,所以这里使用CustomerPaginator实例化,传入4个参数:当前页码、每页显示页码个数、全部数据、每页显示数据条数
# paginator = CustomerPaginator(page_num,5,showCommList,tem_perpage_num)
print("当前页号",page_num)
print("paginator的num_pages值为:",paginator.num_pages)

if int(page_num) &gt; paginator.num_pages:
    page_num = paginator.num_pages

# 根据页码从分页器中取出对应页的数据
try:
    pageList = paginator.page(page_num)
    print(pageList.object_list)

    print("当前要返回的数据集成员量:",pageList.number)
except PageNotAnInteger:
    # 不是整数返回第一页数据
    pageList = paginator.page(1)
    # page_num = 1
except EmptyPage:
    # 当参数页码大于或小于页码范围时,会触发该异常
    # print('出为空页错误,EmptyPage:{}'.format(e))
    print("当前请求页号超过最大页,执行到这里了")
    pageList = paginator.page(paginator.num_pages)
    # if int(page_num) &gt; paginator.num_pages:
    #     # 大于 获取最后一页数据返回
    #     print("执行到这里了")
    #     pageList = paginator.page(paginator.num_pages)
    # else:
    #     # 小于 获取第一页
    #     pageList = paginator.page(1)

# 这部分是为了再有大量数据时,仍然保证所显示的页码数量不超过10,
page_num = int(page_num)
if page_num &lt; 6:
    if paginator.num_pages &lt;= 10:
        dis_range = range(1, paginator.num_pages + 1)
    else:
        dis_range = range(1, 11)
elif (page_num &gt;= 6) and (page_num &lt;= paginator.num_pages - 5):
    dis_range = range(page_num - 5, page_num + 5)
else:
    dis_range = range(paginator.num_pages - 9, paginator.num_pages + 1)



context = {
    'basicInfo':basicInfo,
    'current_user':current_user,
    'client_wish_list':client_wish_list,
    'comTypeList':comTypeList,
    'artTypeList':artTypeList,
    'seriesList':seriesList,
    'levelList':levelList,
    'singleCommType':singleCommType,
    'mostViewComList':mostViewComList,
    'dclass':dclass,
    'num_per_page':perpage_num,
    'pageList': pageList,
    'paginator':paginator,
    'dis_range':dis_range,

}


    以上代码想实现 商品分页显示,遇到的问题:
    如果请求页号等于paginator.num_pages的时候,页面就会报错。报错结果如下(url为:comm/comlist/ctype/&lt;int:类目ID&gt;/&lt;int:每页数量&gt;-&lt;int:请求页号&gt;):

    EmptyPage at /comm/comlist/ctype/2/2-2

本页结果为空 Request Method: GET Request URL: http://127.0.0.1:8000/comm/comlist/ctype/2/2-2 Django Version: 3.1.2 Exception Type: EmptyPage Exception Value:
本页结果为空 Exception Location: D:\Programs\Python\Python39\lib\site-packages\django\core\paginator.py, line 55, in validate_number Python Executable: E:\MyPythonSoft\pycharmProject\BusinessOrderSystem\order_venv\Scripts\python.exe Python Version: 3.9.0 Python Path:
['E:\MyPythonSoft\pycharmProject\BusinessOrderSystem', 'E:\MyPythonSoft\pycharmProject\BusinessOrderSystem', 'D:\Program Files\JetBrains\PyCharm ' '2020.1.1\plugins\python\helpers\pycharm_display', 'D:\Programs\Python\Python39\python39.zip', 'D:\Programs\Python\Python39\DLLs', 'D:\Programs\Python\Python39\lib', 'D:\Programs\Python\Python39', 'E:\MyPythonSoft\pycharmProject\BusinessOrderSystem\order_venv', 'E:\MyPythonSoft\pycharmProject\BusinessOrderSystem\order_venv\lib\site-packages', 'D:\Programs\Python\Python39\lib\site-packages', 'D:\Program Files\JetBrains\PyCharm ' '2020.1.1\plugins\python\helpers\pycharm_matplotlib_backend'] Server time: Wed, 04 Nov 2020 11:54:24 +0800

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值