Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Paginator raising TypeError

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?

like image 324
Llanilek Avatar asked Apr 01 '26 02:04

Llanilek


1 Answers

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.

like image 151
icktoofay Avatar answered Apr 02 '26 19:04

icktoofay