Login

django paginator

Author:
mhangman
Posted:
November 19, 2009
Language:
Python
Version:
1.1
Tags:
django paginator
Score:
1 (after 1 ratings)

This a basic pagination example. It shows new 5 pnews items. We added a code to our template so we can view previous or next pages. It also show us how many pages we have.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#add this to views.py

from django.core.paginator import Paginator,InvalidPage, EmptyPage

def index(request, page=1):
 list = pnews.objects.all().order_by('-pdate')
 paginator = Paginator(list, 5)
 
 try:
  results = paginator.page(page)
 except(InvalidPage, EmptyPage):
  results = paginator.page(paginator.num_pages)
  
 return render_to_response('news/homepage.html', {"results":results})

#this to urls.py modify that for your urls
url(r'^news/(?P\d+)/$', 'cms.news.views.index', name='gmsnews'),

#this to your template so you can visit other pages
<div class="paginator-bottom">
    {%if results.has_previous%}
    <a href="{%url gmsnews results.previous_page_number%}">previous</a>
    {%endif%}
    <p>Page {{results.number}} of {{results.paginator.num_pages}}</p>
    {%if results.has_next%}
    <a href="{%url gmsnews results.next_page_number%}">next</a>
    {%endif%}
</div>

#note that you have to call pages like this
{% for pnews in results.object_list %}
#not like
{% for pnews in results %}

More like this

  1. Pagination Template Tag by ioan1k 6 years, 11 months ago
  2. Pagination Django with Boostrap by guilegarcia 10 months, 3 weeks ago
  3. A tip for preserving GET arguments with pagination by achimnol 7 years, 2 months ago
  4. Django Twitter Bootstrap 3 pagination by Ragnar_Lodbrok 2 years, 6 months ago
  5. paginator using url tag by fivethreeo 9 years ago

Comments

mhangman (on November 19, 2009):
<p>i just dont want to add more app. to my project. this few code enaugh for me.</p>

#

nsmgr8 (on November 19, 2009):
<p>Just use object_list generic view.</p>

#

Gauravwagh11 (on August 5, 2014):
<p>I am getting error</p> <p>TemplateSyntaxError: Could not parse the remainder: ' book_get books.previous_pa ge_number' from 'url book_get books.previous_page_number' at this line next</p>

#

Please login first before commenting.