Piggybacks on the pagination-related template context variables provided by the object_list
generic view, adding extra context variables for use in displaying links for a given number of pages adjacent to the current page and determining if the first and last pages are included in the displayed links.
Also makes it easy to implement consistent paging all over your site by implementing your pagination controls in a single place - paginator.html.
Optionally accepts a single argument to specify the number of page links adjacent to the current page to be displayed.
Usage:
{% paginator %}
{% paginator 5 %}
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 | from django import template register = template.Library() def paginator(context, adjacent_pages=2): """ To be used in conjunction with the object_list generic view. Adds pagination context variables for use in displaying first, adjacent and last page links in addition to those created by the object_list generic view. """ page_numbers = [n for n in \ range(context['page'] - adjacent_pages, context['page'] + adjacent_pages + 1) \ if n > 0 and n <= context['pages']] return { 'hits': context['hits'], 'results_per_page': context['results_per_page'], 'page': context['page'], 'pages': context['pages'], 'page_numbers': page_numbers, 'next': context['next'], 'previous': context['previous'], 'has_next': context['has_next'], 'has_previous': context['has_previous'], 'show_first': 1 not in page_numbers, 'show_last': context['pages'] not in page_numbers, } register.inclusion_tag('paginator.html', takes_context=True)(paginator) |
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
VERY COOL! This saved me a bunch of work. I did make a minor change (just a style difference really):
page_numbers = range(max(0, context['page']-adjacent_pages), min(context['pages'], context['page']+adjacent_pages)+1)
#
Oh yea, in that patch I also changed it to use a decorator. I don't know much about python, but I think that is appropriate.
#
I think this is a very important improvement. I hate having to keep adding to the context list as my app matured. I should only have to add new context to my views. So, I came up with this.
To pass the entire context with modifications/additions to the template of an inclusion tag, you can use:
#
Could you please post a "view" example for this snippet?
#
Example of a view
View parameters and paginator.html?
How this template could be design in order to paginate any resource?
For instance, if you want to paginate instance of News model, then instance of Category model and so forth, links in paginator must be dynamics. Till now I can't figure out how to do this neat and clean.
So, how transmit those parameters to the template?
#
Can you suggest some more examples on using templatetags and how to use URLS for this.
Help me
#
I changed the page_numbers so that it would be a bit smarter and include the first page and the last page if it would otherwise include the page or two next to the first or last page. So, instead of getting page_numbers being [2,3,4] or [3,4,5], it would be [1,2,3,4] and [1,2,3,4,5].
I did this for doing flickr (and apparently digg)-like paginators, so you don't get things like [1, '...', 2, 3, 4] or [1, '...', 3 4 5] -- might as well just put a "2" page link in there instead of "...".
Here is the code change I came up with:
So for my paginator.html I use:
Sean
#
Nice work.
#
My version of the paginator function for Django 1.4
And the corresponding template.
#
Hello
I have created a paginator model but when I move pages does not show me the link to go back to back
The code is as follows:
<nav> {% If%} entrada.has_previus previous page {% Endif%}entrada.number Page {{}} of {{entrada.paginator.num_pages}} {% If%} entrada.has_next Next Page {% Endif%} </ nav>
#
Please login first before commenting.