Django Paginator 引发 TypeError

发布于 2024-12-15 01:43:58 字数 818 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆流年 2024-12-22 01:43:58

尝试更改此行:

page = request.GET.get('page')

对此:

page = request.GET.get('page', '1')

问题是您获得的参数不存在。使用 [] 建立索引会导致 KeyError,但如果 get 方法不存在,则返回 None 。分页器正在调用 int(None),但失败。

get 方法的第二个参数是键不存在时默认返回的参数,而不是 None。我传递了 '1'int 不应失败。

Try changing this line:

page = request.GET.get('page')

To this:

page = request.GET.get('page', '1')

The problem is you're getting a parameter that doesn't exist. Indexing using [] would result in a KeyError, but the get method returns None if it doesn't exist. The paginator is calling int(None), which fails.

The second parameter to the get method is a default to return if the key doesn't exist rather than None. I passed '1' which int should not fail on.

故事未完 2024-12-22 01:43:58
get = self.request.GET
page = int(get.get('page'))

您必须将 string 转换为 int
或者

 page = int(request.GET.get('page'))

你可以做到。两人都跑。

get = self.request.GET
page = int(get.get('page'))

you must convert string to int
or

 page = int(request.GET.get('page'))

you can do it. Both of them runs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文