Django Pagination总是返回“页面数小于1”

时间:2017-07-09 10:35:34

标签: python django pagination django-views

我是Django的新手,并试图让Pagination系统发挥作用。无论我做什么,我都会在页面尝试渲染时收到错误。返回渲染行会引发view.py代码中的错误。

异常类型:EmptyPage

异常值:该页码小于1

我已经检查了传递给渲染器的分页对象。它说“< Page 1 of 26>”。 scoresPaginator.object_list显示正确的数据对象。代码中的打印语句都返回预期值。

我已从模板中删除了所有代码,因此应该没有问题。一切都是现在打印标题。我已将分页模块更新为最新版本。

POST数据收集的原因是当Pagination正在工作时我打算使用jQuery post来引用它。所有脚本也已在模板中注释掉。

我在views.py中的代码是:

if request.method == 'POST':
    limit = int(request.POST.get('numberPP'))
    arrange = request.POST.get('arrangeBy')
    pageNumber = request.POST.get('pageNumber')
else:   
    limit = 10
    arrange = 'id'
    pageNumber = 1
rawScores = score.objects.filter(userID=request.user).order_by('-'+arrange)
scoresPaginator = Paginator(rawScores,limit)
try:
    pScores = scoresPaginator.page(pageNumber)
except PageNotAnInteger:
    pScores = scoresPaginator.page(1)
except EmptyPage:
    pScores = scoresPaginator(scoresPaginator.num_pages)
return render(request,'scores/scoresview.html',{'scores':pScores,'arrange':arrange,'numberPP':limit})

我做错了什么?谢谢

EDIT 第一次加载页面时,它是一个GET。数据在if语句的else部分设置。首次加载页面时会抛出该错误。以下是错误的完整回溯。

Internal Server Error: /scores/scoresview
Traceback (most recent call last):
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:\OneDrive\OU\OU Year 4\TM470\Django\project\tm470\scores\views.py", line 67, in scoresview
    return render(request,'scores/scoresview.html',{'scores':pScores,'arrange':arrange,'numberPP':limit})
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\backends\django.py", line 66, in render
    return self.template.render(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 207, in render
    return self._render(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 199, in _render
    return self.nodelist.render(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 990, in render
    bit = node.render_annotated(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 957, in render_annotated
    return self.render(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\loader_tags.py", line 177, in render
    return compiled_parent._render(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 199, in _render
    return self.nodelist.render(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 990, in render
    bit = node.render_annotated(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 957, in render_annotated
    return self.render(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\loader_tags.py", line 72, in render
    result = block.nodelist.render(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 990, in render
    bit = node.render_annotated(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 957, in render_annotated
    return self.render(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 1040, in render
    output = self.filter_expression.resolve(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 708, in resolve
    obj = self.var.resolve(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 849, in resolve
    value = self._resolve_lookup(context)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\template\base.py", line 911, in _resolve_lookup
    current = current()
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\core\paginator.py", line 155, in previous_page_number
    return self.paginator.validate_number(self.number - 1)
  File "E:\OneDrive\OU\OUYEAR~4\TM470\Django\project\pjaenv\lib\site-packages\django\core\paginator.py", line 45, in validate_number
    raise EmptyPage(_('That page number is less than 1'))
django.core.paginator.EmptyPage: That page number is less than 1
[10/Jul/2017 09:17:35] "GET /scores/scoresview HTTP/1.1" 500 172512

该错误似乎来自第67行的view.py.这是一行:

return render(request,'scores/scoresview.html',{'scores':pScores,'arrange':arrange,'numberPP':limit})

我希望这些额外的信息有所帮助。

1 个答案:

答案 0 :(得分:0)

最后在这个问题上取得了进展。我没有意识到的是,当Django通过模板页面时,它不会注意到任何评论标签。模板页面上的java脚本中存在某种错误。我已经评论了所有的脚本代码。但是Django仍然试图通过剧本。当脚本删除页面时加载了正确的数据就可以了。