| 1 | = Paginator Tag = |
| 2 | |
| 3 | This is a very basic inclusion tag which builds on the variables already set on the context when paginating with the [http://www.djangoproject.com/documentation/generic_views/#django-views-generic-list-detail-object-list generic object_list view] to allow you to create pagination controls which display first/last page links and links for a number of pages adjacent to the current page. |
| 4 | |
| 5 | The following additional context variables are created: |
| 6 | * '''page_numbers''': A list of page numbers (in ascending order) which should be displayed, including the current page number. |
| 7 | * '''show_first''': A boolean representing whether a first page link should be shown - this will be '''True''' if the first page number doesn't appear in '''page_numbers'''. |
| 8 | * '''show_last''': A boolean representing whether a last page link should be shown - this will be '''True''' if the last page number doesn't appear in '''page_numbers'''. |
| 9 | |
| 10 | == Usage == |
| 11 | |
| 12 | The tag can take a single argument, which specifies the number of adjacent page links. The default is 2. |
| 13 | |
| 14 | {{{ |
| 15 | {% if is_paginated %}<div class="paginator">{% paginator %}</div>{% endif %} |
| 16 | {% if is_paginated %}<div class="paginator">{% paginator 4 %}</div>{% endif %} |
| 17 | }}} |
| 18 | |
| 19 | == Code == |
| 20 | |
| 21 | {{{ |
| 22 | from django import template |
| 23 | |
| 24 | register = template.Library() |
| 25 | |
| 26 | def paginator(context, adjacent_pages=2): |
| 27 | """Adds pagination context variables for first, adjacent and next page links |
| 28 | in addition to those already populated by the object_detail generic view.""" |
| 29 | page_numbers = [n for n in \ |
| 30 | range(context["page"] - adjacent_pages, context["page"] + adjacent_pages + 1) \ |
| 31 | if n > 0 and n <= context["pages"]] |
| 32 | return { |
| 33 | "hits": context["hits"], |
| 34 | "results_per_page": context["results_per_page"], |
| 35 | "page": context["page"], |
| 36 | "pages": context["pages"], |
| 37 | "page_numbers": page_numbers, |
| 38 | "next": context["next"], |
| 39 | "previous": context["previous"], |
| 40 | "has_next": context["has_next"], |
| 41 | "has_previous": context["has_previous"], |
| 42 | "show_first": 1 not in page_numbers, |
| 43 | "show_last": context["pages"] not in page_numbers, |
| 44 | } |
| 45 | |
| 46 | register.inclusion_tag("paginator.html", takes_context=True)(paginator) |
| 47 | }}} |
| 48 | |
| 49 | Sample paginator.html: |
| 50 | {{{ |
| 51 | {% spaceless %} |
| 52 | <span class="paginate-pages" onclick="Paginator.jumpToPage({{ pages }})" title="Page Jump">{{ pages }} Pages</span> |
| 53 | {% if show_first %}<span class="paginate-first"><a href="?page=1" title="First Page">«</a></span>{% endif %} |
| 54 | {% if has_previous %}<span class="paginate-previous"><a href="?page={{ previous }}" title="Previous Page"><</a></span>{% endif %} |
| 55 | {% for num in page_numbers %} |
| 56 | {% ifequal num page %} |
| 57 | <span class="paginate-current" title="Current Page">{{ num }}</span> |
| 58 | {% else %} |
| 59 | <span class="paginate-link"><a href="?page={{ num }}" title="Page {{ num }}">{{ num }}</a></span> |
| 60 | {% endifequal %} |
| 61 | {% endfor %} |
| 62 | {% if has_next %}<span class="paginate-next"><a href="?page={{ next }}" title="Next Page">></a></span>{% endif %} |
| 63 | {% if show_last %}<span class="paginate-last"><a href="?page={{ pages }}" title="Last Page">»</a></span>{% endif %} |
| 64 | {% endspaceless %} |
| 65 | }}} |
| 66 | |
| 67 | paginator.js, used by the above: |
| 68 | {{{ |
| 69 | var Paginator = |
| 70 | { |
| 71 | jumpToPage: function(pages) |
| 72 | { |
| 73 | var page = prompt("Enter a number between 1 and " + pages + " to jump to that page", ""); |
| 74 | if (page != undefined) |
| 75 | { |
| 76 | page = parseInt(page, 10) |
| 77 | if (!isNaN(page) && page > 0 && page <= pages) |
| 78 | { |
| 79 | window.location.href = "?page=" + page; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | }}} |