The site has a built-in django paginator, I want to make not a simple page navigation (back and forth), but with a small page selection (like http://getbootstrap.com/components/#pagination ). But I do not know how to check the availability of the n (not a trace) of the page and how to make it work adequately. ps do not output all pages, but to the left 2 and to the right 2 (as in Google or Yandex)
2 answers
Unfortunately, you have to write your own paginator (or use the existing snippets), I did not find the finished component.
The idea is quite simple - you just need to count how many pages you need to display around the current page and whether there are enough pages to display the pages at the beginning and the pages at the end of the list.
All this can be arranged directly in the layout of the template, but from experience I will say that it turns out very cumbersome. It is better to write a templatetag ( or use ready-made ). If the skill allows you to own a python, then you can write your own paginator (or take a ready-made snippet ).
You can also search in the English part of the Internet, there it is called "fraction paginator", "digg-like paginator". Well, finally, a good article on this topic.
- Thank you, I did not think that they will answer in a few months. - Vladimir Saleev
It is possible to remake the standard paginator.
The transitions to the beginning / end and back / forward is quite clear how to do it after reading the standard documentation .
If you need to display links to all pages, then we iterate over p.page_range
To solve your problem, you need to calculate the "window", which will move through the list of all pages depending on the current page.
And it will be necessary to use in the template not the entire p.page_range , but an iterator over this window. Let's call it my_page_range .
You also need a constant that will store the number of pages visible to the left and right of the current one. Let's call it ADD_PAGES .
It should be noted that in the "extreme points" the number of pages to the left and right of the current one is not equal to each other.
We use an example from the documentation, adding the necessary changes to it:
... except EmptyPage: # If page is out of range (eg 9999), deliver last page of results. contacts = paginator.page(paginator.num_pages) # --- наш добавляемый код --- ADD_PAGES = 2 # количество страниц слева и справа от текущей # таким образом, все "окно" состоит из 5 страниц, включая текущую if contacts.paginator.num_pages > ADD_PAGES * 2 + 1: # количество страниц больше чем "окно" # найдем начало и конец "окна" для общего случая start_window = contacts.number - ADD_PAGES end_window = contacts.number + ADD_PAGES if start_window < 1: # если "окно вылезает" за левую границу # то левую границу ставим на 1-ю страницу start_window = 1 # а правую границу вычисляем относительно 1-й end_window = 1 + ADD_PAGES * 2 if end_window > contacts.paginator.num_pages: # если "окно вылезает" за правую границу # то правую границу ставим на последнюю страницу end_window = contacts.paginator.num_pages # а левую границу вычисляем относительно последней страницы start_window = end_window - ADD_PAGES * 2 # определим итератор для "окна" my_page_range = range(start_window, end_window + 1) else: # количество страниц меньше или равно "окну" # просто используем весь диапазон доступных страниц, т.е. page_range my_page_range = contacts.paginator.page_range It now remains to transfer to the my_page_range template and walk there through it in a loop when “drawing” the paginator buttons.