Django Paginator (advanced), djangopaginator

Source: Internet
Author: User

Django Paginator (advanced), djangopaginator

I. Overview

In another blog, I introduced two paging methods in django. However, the paging refresh function is basically implemented, but there is still one problem, that is, when there are too many pages, all the pages will be displayed, which is not easy to say. What's more, when there are too many pages, the page bar will become very long. Therefore, while restructuring the previous practices, we also implemented the function of defining the number of page numbers displayed in the page bar.

Final effect:


This blog post aims to deepen understanding of the use of Paginator. After you are familiar with it, you can also implement similar functions of "jump" and "next five pages.


Ii. Paginator

Blog post mentioned that using multiple tables in a page, using url forms such as http://XXX.html? Table = 4 & page = 2, where table is the number of tables, and page indicates the current page number of the table.

Therefore, views. py parses the url and uses the get method of the request.

from hello.paging import get_page_msgdef user_review(req):<span style="white-space:pre"></span>...<span style="white-space:pre"></span>...<span style="white-space:pre"></span>page=req.GET.get('page','')<span style="white-space:pre"></span>if page == '':<span style="white-space:pre"></span>page = 1<span style="white-space:pre"></span>table=req.GET.get('table','')<span style="white-space:pre"></span>if table == '':<span style="white-space:pre"></span>table = 1<span style="white-space:pre"></span>selectItemList=[user,user_review]<span style="white-space:pre"></span>index=int(table)-1<span style="white-space:pre"></span>count=len(selectItemList)<span style="white-space:pre"></span><span style="white-space:pre"></span>resultItem,p_pages=get_page_msg(limit,selectItemList,index,count,page)<span style="white-space:pre"></span>return render_to_response('user-review.html',{'user':resultItem[0],'p_pages_user':p_pages[0],'user_review':resultItem[1],'p_pages_user_review':p_pages[1]},context_instance=RequestContext(req))


Explanations:

Page, table ------------------ Initialization is 1

Limit --------------------------- the number of projects displayed on each page in each table. ajax technology can be used to change the number of projects like datatables.

SelectItemList ------------- a list. each item is the original data in a table.

Index --------------------------- the number of tables currently clicked, starting from 0th

Count ------------------------ number of tables, that is, the length of selectItemList

Page ------------------------- the page number of the selected table to be displayed.

ResultItem ----------------- a list. each item is a Paginator. page () instance.

P_pages ------------------- a list. Each item represents the page number list to be displayed.


So the focus is the get_page_msg () function. Encapsulation:

# Coding: utf-8from django. core. paginator import Paginator, EmptyPage, PageNotAnInteger ''' parameter 1: number of entries displayed on each page parameter 2: List of data to be displayed parameter 3: form parameter 4: number of tables processed in total parameter 5: When the page is turned over, the first two parameters of the corresponding page number are set. The last three parameters of all tables correspond only to the corresponding table '''def get_page_msg (limit, selectItemList, index, count, page): p_pages = [] resultItem = [] for I in range (count): selectItemList [I] = Paginator (selectItemList [I], limit) p_pages.append (selectItemList [I]. page_range [0: 7]) resultItem. append (selectItemList [I]. page (1) if int (page) <4: # here, if the current page is smaller than 5, p_pages [index] = selectItemList [index]. page_range [] # pr is used to obtain the page number list. When the current page is smaller than 4, page 1st to page 7th is displayed in the template, for example, 1 2 3 4 5 6 7 elif int (selectItemList [index]. num_pages)-int (page) <4: # If the last page minus the current page is less than 4, num_pages = int (selectItemList [index]. num_pages) p_pages [index] = selectItemList [index]. page_range [num_pages-7: num_pages] # page number List displays the last 7 pages, if there are 30 pages, then: 24 25 26 27 28 29 30 else: # In other cases, p_pages [index] = selectItemList [index]. page_range [int (page)-4: int (page) + 3] # The first three to the last three of the current page are displayed in other cases. For example, if the current page is 10th, the result shows: 7 8 9 10 11 12 13 try: resultItem [index] = selectItemList [index]. page (page) Does T EmptyPage: resultItem [index] = selectItemList [index]. page (selectItemList [index]. num_pages) return resultItem, p_pages

Explanations:

For... range... ----------------------- create a list composed of count Paginator instances, a list composed of seven default display ranges, and a default Paginator count. list of page (1) Instances

. Page_range --------------------- list of the total range of the page number. When the slice operation is used, it is automatically switched to the last when the range is exceeded (python slice Operation)

. Num_pages -------------------- the last number on the page

These actions can be encapsulated as a class, refer to http://www.linuxyw.com/309.html


Then, the code is changed:

User-review.html

<div>       <span style="white-space:pre"></span>{%  include "pages/user_review.html" %}</div>

Pages/user-review.html

<Ul style = "float: left"> total <span >{{ user_review.paginator.count }}</span> tasks, total <span >{{ user_review.paginator.num_pages }}</span> pages </ul> <ul class = "pagination" style = "float: right ">{% if user_review.has_previous %} <li> <a href = "? Table = 2 & page = {user_review.previus_page_number} "class =" prev ">{{ previus_link_decorator | safe }} previous page </a> </li >{% else %} <li class = "paginate_button previous disabled"> <span class = "disabled prev" >{{ previus_link_decorator | safe }} previous page </span> </li >{% endif %} {% if user_review.has_previous %} <li> <a href = "? Table = 2 & page = 1 "class =" prev ">{{ previus_link_decorator | safe }} homepage </a> </li >{% else %} <li class = "paginate_button previous disabled"> <span class = "disabled prev" >{{ previus_link_decorator | safe }} homepage </span> </li >{% endif %}{% for page in p_pages_user_review %} {% if page %} {% ifequal page user_review.number %} <li class = "active"> <span class = "current page" >{{ page} </span> </li >{% else %} <li> <a href = "? Table = 2 & page {page_suffix }}={ {page }{{ getvars} "class =" page ">{{ page }}</a> </ li >{% endifequal % }{% endif % }{% endfor % }{% if user_review.has_next %} <li> <a href = "? Table = 2 & page = {user_review.paginator.num_pages} "class =" next "> last page {next_link_decorator | safe }}</a> </li >{% else %} <li class = "paginate_button next disabled"> <span class = "disabled next"> last page: {next_link_decorator | safe }}</span> </li >{% endif %} {% if user_review.has_next %} <li> <a href = "? Table = 2 & page = {user_review.next_page_number} "class =" next "> next page {next_link_decorator | safe }}</a> </li >{% else %} <li class = "paginate_button next disabled"> <span class = "disabled next"> next page {next_link_decorator | safe }}</span> </li >{% endif %} </ul>

Brief description:

When. py sends a Paginator. page () object, you can call xxx. paginator. num_pages gets the page number of the last page, which is equivalent to the number of pages in views. use Paginator in py. num_pages can be directly transferred to the front-end to achieve the same effect, depending on how the individual decides, whether to put it on the background or the front-end.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.