django paginator

 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/Filtering Alphabetically by zain 4 years, 2 months ago
  2. Faster pagination / model object seeking (10x faster infact :o) for larger datasets (500k +) by sleepycal 2 years, 5 months ago
  3. Complex Formsets, Redux by smagala 3 years, 2 months ago
  4. Pagination Template Tag by ioan1k 3 years, 8 months ago
  5. Custom managers with chainable filters by itavor 5 years, 4 months ago

Comments

rawjam (on November 19, 2009):

Is there a reason why you opted for your own approach over http://code.google.com/p/django-pagination/ ?

#

mhangman (on November 19, 2009):

i just dont want to add more app. to my project. this few code enaugh for me.

#

nsmgr8 (on November 19, 2009):

Just use object_list generic view.

#

(Forgotten your password?)