""" custom pagination component """ class Pagination(object): def __init__(self, data_num, current_page, url_prefix,params, per_page=10, max_show=3): """ to initialize. :param data_num: total number of data :param current_page: current page :param url_prefix: The link prefix of the generated page number :param per_page: how many pieces of data to display per page :param max_show: The maximum number of page numbers displayed on the page """ self.data_num = data_num self.per_page = per_page self.max_show = max_show self.url_prefix = url_prefix #Calculate the number of pages self.page_num, more = divmod(data_num, per_page) if more: self.page_num += 1 try: self.current_page = int(current_page) except Exception as e: self.current_page = 1 #If the number of pages passed from the URL is negative if self.current_page <= 0: self.current_page = 1 #If the number of pages passed from the URL exceeds the maximum number of pages elif self.current_page > self.page_num: self.current_page = self.page_num #Show the last page by default # Calculate half the number of pages self.half_show = max_show // 2 #How much is displayed on the far left of the page number if self.current_page - self.half_show <= 1 : self.page_start = 1 self.page_end = self.max_show elif self.current_page + self.half_show >= self.page_num: #If the right side is out of bounds self.page_end = self.page_num self.page_start = self.page_num - self.max_show else: self.page_start = self.current_page - self.half_show #The page number is displayed on the far right self.page_end = self.current_page + self.half_show import copy self.params=copy.deepcopy(params) # {"page":"12","title_startwith":"py","id__gt":"5"} @property def start(self): #Where does the data start to cut return (self.current_page - 1) * self.per_page @property def end(self): #Where to cut the data slice return self.current_page * self.per_page def page_html(self): #Generate page number l = [] #Add a home page l.append( ' <li><a href="{}?page=1">Home</a></li> ' .format (self.url_prefix)) #Add a previous page if self.current_page == 1 : l.append('<li class="disabled" ><a href="#">«</a></li>'.format(self.current_page)) else: l.append('<li><a href="{}?page={}">«</a></li>'.format(self.url_prefix, self.current_page - 1)) # {"page":"12","title_startwith":"py","id__gt":"5"} # "page=12&title_startwith=py&id__gt=5" print(self.params.urlencode()) for i in range(self.page_start, self.page_end + 1): self.params["page"]=i # # {"page":"7","title_startwith":"py","id__gt":"5"} # "page=7&title_startwith=py&id__gt=5" if i == self.current_page: tmp = '<li class="active"><a href="{0}?page={1}">{1}</a></li>'.format(self.url_prefix, i) else: tmp = '<li><a href="{0}?{1}">{2}</a></li>'.format(self.url_prefix, self.params.urlencode(),i) l.append(tmp) #Add a next page if self.current_page == self.page_num: l.append('<li class="disabled"><a href="#">»</a></li>'.format(self.current_page)) else: l.append('<li><a href="{}?page={}">»</a></li>'.format(self.url_prefix, self.current_page + 1)) # 加一个尾页 l.append('<li><a href="{}?page={}">尾页</a></li>'.format(self.url_prefix, self.page_num)) return "".join(l)
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Latest version of Bootstrap core CSS files --> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <ul> {% for foo in book_list %} <li>{{ foo }}</li> {% endfor %} </ul> <nav> <ul class="pagination"> {{ pagination.page_html|safe }} </ul> </nav> </body> </html>