Question

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?

Était-ce utile?

La solution

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top