When there is a lot of data in the database, we usually do pagination display in the front page.
Paging data can be achieved in the front page, you can also implement paging in the back end.
Back-end implementation of the principle of paging is a time request a page of data.
Ready to work
We use a batch script to create some test data (Save the following code to bulk_create.py file into the root directory of your Django project, can be directly executed.)
import os if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "about_orm.settings") import django django.setup() from app01 import models bulk_obj = (models.Publisher(name='沙河第{}出版社'.format(i)) for i in range(300)) models.Publisher.objects.bulk_create(bulk_obj)
Custom Paging
DEF be publisher_list (Request): # the number of pages taken from the URL currently visited the try : current_page = int (request.GET.get ( ' Page ' )) the except Exception AS E: # take or not the page numbers are not the default number of impressions page 1 current_page. 1 = # total data amount TOTAL_COUNT = models.Publisher.objects.count () # define how many data per page per_page = 10 # calculates the total number of pages total_page, More = divmod (TOTAL_COUNT, per_page) IF More : total_page +. 1 = # number displayed on the page definition page up (for symmetrical, usually set to an odd number) =. 11 max_show half_show = 2 // max_show # calculate the range of pages displayed page IF total_page <= max_show: # total number of pages is less than the maximum number of page numbers PAGE_START. 1 = PAGE_END = total_page elif current_page half_show +> = total_page: # the right bounds PAGE_END = total_page PAGE_START = total_page - max_show elif current_page - half_show <= 1: # left out of bounds PAGE_START = 1 PAGE_END = max_show the else : # normal page interval - PAGE_START = current_page half_show PAGE_END = current_page + half_show # data index start position DATA_START = (. 1-current_page) * per_page data_end = current_page * per_page be publisher_list = : models.Publisher.objects.all () [data_end DATA_START] # the generation page numbering shown page_html_list = [] page_html_list.append ( ' <NAV Aria-label = "Page Navigation"> <UL class = "the pagination"> ' ) # plus Home first_li = ' <Li> <a the href = "/ be publisher_list / ? page = 1 "> Home </a></li>' page_html_list.append(first_li) # 加上一页 if current_page == 1: prev_li = '<li><a href="#"><span aria-hidden="true">«</span></a></li>' else: prev_li = '<li><a href="/publisher_list/?page={}"><span aria-hidden="true">«</span></a></li>'.format(current_page - 1) page_html_list.append(prev_li) for i in range(page_start, page_end + 1): if i == current_page: li_tag = '<li class="active"><a href="/publisher_list/?page={0}">{0}</a></li>'.format(i) else: li_tag = '<li><a href="/publisher_list/?page={0}">{0}</a></li>'.format(i) page_html_list.append(li_tag) # 加下一页 if current_page == total_page: next_li = '<li><a href="#"><span aria-hidden="true">»</span></a></li>' else: next_li = '<li><a href="/publisher_list/?page={}"><span aria-hidden="true">»</span></a></li>'.format(current_page + 1) page_html_list.append(next_li) # 加尾页 page_end_li = '<li><a href="/publisher_list/?page={}">尾页</a></li>'.format(total_page) page_html_list.append(page_end_li) page_html_list.append('</ul></nav>') page_html = "".join(page_html_list) return render(request, "publisher_list.html", {"publisher_list": publisher_list, "page_html": page_html})
class Pagination (Object): "" " Custom tab (Bootstrap Edition) " "" DEF the __init__ (Self, current_page, TOTAL_COUNT, the base_url, per_page = 10, max_show =. 11 ): "" " : param current_page: currently requested page : param total_count: the total amount of data : param base_url: request the URL of : param per_page: the amount of data displayed per page, the default value is 10 : param max_show: how many pages show up on the page, the default value is 11 "" " the try : self.current_page = int (current_page) the except Exception AS E: # take or not the page numbers are not the default number of impressions on a self.current_page = 1# Showing defines how many data self.per_page = per_page # calculates the total number of page total_page, More = divmod (TOTAL_COUNT, per_page) IF More: total_page + =. 1 self.total_page = total_page # up the page number displayed on the page number is defined (for symmetrical, is generally set to an odd number) self.max_show = max_show self.half_show = 2 // max_show self.base_url = the base_url @Property DEF Start (Self): return (-self.current_page. 1) * self.per_page @Property DEFEnd (Self): return self.current_page * self.per_page DEF page_html (Self): # calculate page displayed page range IF self.total_page <= self.max_show: # total number of pages is less than the maximum number of page numbers PAGE_START. 1 = PAGE_END = self.total_page elif self.current_page + self.half_show> = self.total_page: # the right of cross-border PAGE_END = self.total_page PAGE_START = self.total_page - self.max_show elif self.current_page - self.half_show <= 1: # left out of bounds =. 1 PAGE_START PAGE_END = self.max_show the else : # normal page interval PAGE_START = self.current_page - self.half_show PAGE_END = self.current_page + self.half_show # page displayed on the generation page page_html_list = [] page_html_list.append ( ' <NAV label =-Aria "Page Navigation"> <UL class = "the pagination"> ' ) # plus Home first_li = ' <Li> <a href="{}?page=1"> Home </a> </ li> ' .format (self.base_url) page_html_list.append(first_li) # 加上一页 if self.current_page == 1: prev_li = '<li><a href="#"><span aria-hidden="true">«</span></a></li>' else: prev_li = '<li><a href="{}?page={}"><span aria-hidden="true">«</span></a></li>'.format( self.base_url, self.current_page - 1) page_html_list.append(prev_li) for i in range(page_start, page_end + 1): if i == self.current_page: li_tag = '<li class="active"><a href="{0}?page={1}">{1}</a></li>'.format(self.base_url, i) else: li_tag = '<li><a href="{0}?page={1}">{1}</a></li>'.format(self.base_url, i) page_html_list.append(li_tag) # 加下一页 if self.current_page == self.total_page: next_li = '<li><a href="#"><span aria-hidden="true">»</span></a></li>' else: next_li = '<li><a href="{}?page={}"><span aria-hidden="true">»</span></a></li>'.format( self.base_url, self.current_page + 1) page_html_list.append(next_li) # 加尾页 page_end_li = '<li><a href="{}?page={}">尾页</a></li>'.format(self.base_url, self.total_page) page_html_list.append(page_end_li) page_html_list.append('</ul></nav>') return "".join(page_html_list)
Rear DEF be publisher_list (Request): # take the current from the URL to access the page number current_page = int (request.GET.get ( ' Page ' )) # than len (models.Publisher.objects.all ()) more efficient total_count = models.Publisher.objects.count () page_obj = Pagination (current_page, TOTAL_COUNT, request.path_info) Data = models.Publisher.objects.all () [page_obj.start: page_obj.end] page_html = page_obj.page_html () return the render (Request, " publisher_list.html " , about locals ()) distal loop show that: { %for i in publisher_list %} .... {% endfor %} {{ page_obj.page_html|safe }}
Django built pagination
from django.shortcuts Import the render from django.core.paginator Import Paginator, EmptyPage, PageNotAnInteger L = [] for I in Range (999 ): L.append (I) DEF index (Request): current_page = request.GET.get ( ' P ' ) the paginator = Paginator (L, 10 ) # per_page: number of entries per page # COUNT: total number of data # NUM_PAGES: pages # page_range: total number of pages in the index range, such as: (1, 10) , (1,200) # Page: Page Object the try : Posts = paginator.page (current_page) # Is there has_next Next # next_page_number Next Page # Are there has_previous Previous # previous_page_number Previous Page # data list after object_list Page # Number The current page # paginator paginator Object the except PageNotAnInteger: Posts = paginator.page (. 1 ) the except EmptyPage: Posts = paginator.page (paginator.num_pages) return the render (Request, 'index.html', {'posts': posts})
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <ul> {% for item in posts %} <li>{{ item }}</li> {% endfor %} </ul> <div class="pagination"> <span class="step-links"> {% if posts.has_previous %} <a href="?p={{ posts.previous_page_number }}">Previous</a> {% endif %} <span class="current"> Page {{ posts.number }} of {{ posts.paginator.num_pages }}. </span> {% if posts.has_next %} <a href="?p={{ posts.next_page_number }}">Next</a> {% endif %} </span> </div> </body> </html>