Django Paginator 引发 TypeError
我正在尝试使用 django 分页模块,包括标准发行版 1.3 中的模块。
当尝试加载当前由分页控制的页面时,如果我不在 uri 上包含 ?page= ,则会抛出 TypeError。我以前从未出现过这种情况,并且看不出发生这种情况的任何原因。
这是我当前的观点:
paginator = Paginator(mails_list, 25) # Shows 25 mails per page
page = request.GET.get('page')
try:
mails = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver the first page.
mails = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results
mails = paginator.page(paginator.num_pages)
TypeError:
int() argument must be a string or a number, not 'NoneType'
The error is displayed from the line 3 of the 上述代码:
mails = paginator.page(page)
有人之前目睹过此错误和/或知道如何纠正它吗?
I'm trying to use the django pagination module including in the standard distribution version 1.3.
When attempting to load a page that is currently controlled by pagination, if I do not include ?page= on the uri, it throws a TypeError. I've never had this situation arise before, and do not see any reason for it occurring.
Here's my current view:
paginator = Paginator(mails_list, 25) # Shows 25 mails per page
page = request.GET.get('page')
try:
mails = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver the first page.
mails = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results
mails = paginator.page(paginator.num_pages)
TypeError:
int() argument must be a string or a number, not 'NoneType'
The error is being presented from line 3 of the above code:
mails = paginator.page(page)
Anyone witnessed this error before and/or know how to correct it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试更改此行:
对此:
问题是您获得的参数不存在。使用
[]
建立索引会导致KeyError
,但如果get
方法不存在,则返回None
。分页器正在调用int(None)
,但失败。get
方法的第二个参数是键不存在时默认返回的参数,而不是None
。我传递了'1'
,int
不应失败。Try changing this line:
To this:
The problem is you're getting a parameter that doesn't exist. Indexing using
[]
would result in aKeyError
, but theget
method returnsNone
if it doesn't exist. The paginator is callingint(None)
, which fails.The second parameter to the
get
method is a default to return if the key doesn't exist rather thanNone
. I passed'1'
whichint
should not fail on.您必须将 string 转换为 int
或者
你可以做到。两人都跑。
you must convert string to int
or
you can do it. Both of them runs.