Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

Pagination in Django

Posted by Praveen Kumar on January 30, 2009

To use Paginator in Django

we need to import

from django.core.paginator import Paginator

Before going to real example letssee all the classes and attributes of Paginator Module

class Paginator(__builtin__.object)

Methods defined here:|

|  __init__(self, object_list, per_page, orphans=0, allow_empty_first_page=True)|

|  page(self, number)|      Returns a Page object for the given 1-based page number.|

|  validate_number(self, number)|     Validates the given 1-based page number.|
|  ———————————————————————-
Properties defined here:|

|  count

|      <get> = _get_count(self)|          Returns the total number of objects, across all pages.|

|  num_pages

|      <get> = _get_num_pages(self)|          Returns the total number of pages.|

|  page_range

|      <get> = _get_page_range(self)|          Returns a 1-based range of pages for iterating through within
|          a template for loop.

|
|  ———————————————————————-
Data and other attributes defined here:
|
|  __dict__ = <dictproxy object>|      dictionary for instance variables (if defined)
|
|  __weakref__ = <attribute ‘__weakref__’ of ‘Paginator’ objects>|      list of weak references to the object (if defined)

Real example

Lets assume you have event class in models

eventobj=events.objects.all() // list you objects of events class

paginator = Paginator(eventobj, 2)

try:
page = int(request.GET.get(‘page’,’1′))
except ValueError:
page = 1
try:
eventobj = paginator.page(page)
except(EmptyPage, InvalidPage):
eventobj = paginator.page(paginator.num_pages)
return render_to_response(“event.html”,{‘eventobj’:eventobj,’today’:today.strftime(“%B %d %Y %A”)})

event.html

<div >
{% if eventobj %}
<div class=”event”>
<div style=”color:red”>{{ today }} </div>
{% for name in eventobj.object_list %}
<div class=”eventname”><b>{{ name.title }}</b></div>
<div class=”eventaddress”>{{ name.address }}</div>
<div class=”eventdesc”>{{ name.description }}</div>
{% endfor %}
</div>
{% endif %}
</div>
<div class=”paging”>
<table align=”center”>
<tr>
<td>
{% if eventobj.has_previous %}
<a href=”?page={{ eventobj.previous_page_number}}” style=”text-decoration:none;color:#000000;” ><img src=”/static/images/article_previous.gif” border=”0″/>&nbsp;&nbsp;prev&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;</a>
{% endif %}
</td>
<td>
Page {{ eventobj.number }} of {{ eventobj.paginator.num_pages }}
</td>
<td>
{% if eventobj.has_next %}
<a href=”?page={{ eventobj.next_page_number }}” style=”text-decoration:none;color:#000000;”>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;next&nbsp;&nbsp;<img src=”/static/images/article_next.gif” border=”0″/></a>{% endif %}
</tr>
</table></div>


Advertisements

6 Responses to “Pagination in Django”

  1. Habasch said

    Interesting article, what about implementing a vaginator in a future project? Would be interesting as well, wouldn’t it?

  2. Thanks.
    Sorry Habasch, I never heard about vaginator even i googled it but could not find. if you find then send me link. it would be very curious for me to know about this.

  3. Mike said

    Just passing by.Btw, you website have great content!

    _________________________________
    Making Money $150 An Hour

  4. Nitin said

    Hello
    I used this code but “next” link is not working. Can you help me out of this.

    Thanks

  5. Hi Nitin, could you be more specific. what “next” link is not working and what it does when you click on next?

  6. Nitin said

    Hello Praveen
    Its now done. was because of my silly mistake. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: