By Sean Reifschneider Date March 19, 2008
3 pages around the current page.
But no ellipsis if we are towards the beginning or end, or if the
ellipsis would only cover for one page.
And hide next/previous if at the beginning or end.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'photoblog.photos',
)
Which meant I needed to write this to
"photoblog/photos/templatetags/paginator.py".
Note: If you don't already have a "templatetags" directory,
create it with:
mkdir templatetags
touch templatetags/__init__.py
The "__init__.py" file needs to be there so that the directory can be
imported as a module.
from django.views.generic.list_detail import object_list
def index(request, format):
from django.db.models import Q
photo_list = Photos.objects.filter(
Q(album = None) | Q(isalbumleader = True)
).order_by('-uploadedon')
return object_list(request, template_name = 'index.html',
queryset = photo_list, paginate_by = 25)
<div class="pager">
{% if has_previous %}
<span class="page">
<a href="?page={{ previous }}">< Prev</a>
</span>
{% endif %}
{% if show_first %}
<span class="page"><a href="?page=1">1</a></span>
<span class="ellipsis">...</span>
{% endif %}
{% for linkpage in page_numbers %}
{% ifequal linkpage page %}
<span class="current">{{ page }}</span>
{% else %}
<span class="page"><a href="?page={{ linkpage }}"
>{{ linkpage }}</a></span>
{% endifequal %}
{% endfor %}
{% if show_last %}
<span class="ellipsis">...</span>
<span class="page"><a href="?page=last">{{ pages }}</a></span>
{% endif %}
{% if has_next %}
<span class="page"><a href="?page={{ next }}">Next ></a></span>
{% endif %}
</div>
This can basically be used un-modified.
{% if is_paginated %}{% load paginator %}{% paginator 3 %}{% endif %}
The "3" is the number of surrounding pages around the current page to
show.
Note: In your template, the object_list generic view presents
the list of items for the page as "object_list". So a minimal full page
would be something like:
{% if is_paginated %}{% load paginator %}{% paginator 3 %}{% endif %}
{% if object_list %}
<ul>{% for photo in object_list %}
<li />{{ photo.name }}
{% endfor %}</ul>
{% endif %}
{% if is_paginated %}{% load paginator %}{% paginator 3 %}{% endif %}
This example includes pagers both above and below the paged data, and
a simple list of the photo names.
<style type="text/css">
.pager {
padding-top: 20px;
padding-left: 40px;
}
.pager .page a {
border: 3px solid #bbbbbb;
margin-left: 1px;
margin-right: 1px;
padding-left: 4px;
padding-right: 4px;
text-decoration: none;
color: #000000;
}
.pager .current {
border: 3px solid #444444;
margin-left: 2px;
margin-right: 2px;
padding-left: 2px;
}
</style>