[Solved]-Django Pagination

22👍

You make a false assumption. Django does not retrieve all objects when paginating: it slices the queryset appropriately, which uses LIMIT and COUNT on the SQL.

1👍

A QuerySet is a lazy object. When you assign contact_list = Contacts.objects.all(), Django will NOT hit the database. Until you call the methods such as count(), filter(), first(),…, or contact_list[1:5], Django will really retrieve data from the database. SQL statements that Django generate will correlate to each method and these SQL statments will be sent to the DB.

E.g: contact_list[1:5] generate a SQL statement have LIMIT and OFFSET clauses.

In Paginator class, the QuerySet will passed to the its constructor

paginator = Paginator(contact_list, 25)

When you call paginator.page(page), the statement is called:

return self._get_page(self.object_list[bottom:top], number, self)

0👍

Look inside Paginator class (django/core/paginator.py), it fetches only required pages. There is only one problem on big tables: if you want to show total page numbers you must make count(*) on entire table which can took a long time in some databases (i.e. postgresql, mysql with innodb).

BTW, try to use generic views in django, ListView would be fine here.

0👍

Here we using get_page() to get page wise data(1 page contain 25 data).I would suggest for this like :

def listing(request):
    contact_list = Contacts.objects.all()
    paginator = Paginator(contact_list, 25) # Show 25 contacts per page
    page = request.GET.get('page')
    contacts = paginator.get_page(page)

    return render_to_response('list.html', {"contacts": contacts})

Leave a comment