SoFunction
Updated on 2024-11-19

Django implementation of class-based paging functionality

Development of personal blogs often avoid the implementation of paging features, the implementation method is largely divided into two functions and classes. This article is based on the class to realize the paging function, the following is the detailed code:

class Page:
 page_num = 0
 total_count = 0
 tartype = ""
 total_page = 0 # of total pages
 per_page = 8 # 8 articles per page
 max_page = 11 # Maximum number of pages per page option
 half_page = max_page // 2
 category = "" # Mark the ID of the category to which the link belongs, home page is empty by default

 def __init__(self, page_num, total_count, tartype, category):
 """
 Initialization function: receive the number of pages, the total number of data, calculate the total number of pages and filter the wrong page number data
 :param page_num: number of page numbers
 :param total_count: total_data_count
 :param tartype: category value
 :param category: Category ID
 """
 self.page_num = page_num
 self.total_count = total_count
  = tartype
 self.total_page, m = divmod(self.total_count, self.per_page) # of total page numbers with a remainder of m
 if category:
   = category
 if m:
  self.total_page += 1
 if self.total_page < self.max_page:
  self.max_page = self.total_page
  self.half_page = self.max_page // 2
 # Filtering the wrong get data
 if self.page_num > self.total_page:
  self.page_num = self.total_page
 elif self.page_num < 1:
  self.page_num = 1

 def data_start(self):
 return (self.page_num - 1) * self.per_page

 def data_end(self):
 return self.page_num * self.per_page

 def page_html(self):
 # Calculate page options
 page_start = self.page_num - self.half_page
 page_end = self.page_num + self.half_page
 if page_start <= 1:
  page_start = 1
  page_end = self.max_page
 if page_end >= self.total_page:
  page_end = self.total_page
  page_start = self.total_page - self.max_page + 1
 # Calculate Previous Next Options
 if self.page_num == 1:
  last_page = self.page_num
  next_page = self.page_num + 1
 else:
  if self.page_num == self.total_page:
  last_page = self.page_num - 1
  next_page = self.page_num
  else:
  last_page = self.page_num - 1
  next_page = self.page_num + 1
 print()
 html_str_list = []
 html_str_list.append('<li><a href="/{}/?page={}{}" >«</a></li>'.format(, last_page, ))
 for i in range(page_start, page_end + 1):
  if i == self.page_num:
  tmp = '<li><a class="active" href="/{}/?page={}{}" >{}</a></li>'.format(, i, , i)
  html_str_list.append(tmp)
  continue
  tmp = '<li><a href="/{}/?page={}{}" >{}</a></li>'.format(, i, , i)
  html_str_list.append(tmp)
 html_str_list.append('<li><a href="/{}/?page={}{}" >»</a></li>'.format(, next_page, ))
 page_html = "".join(html_str_list)
 return page_html

Control the number of articles displayed per page and the maximum number of page numbers displayed by the paging module by modifying per_page and max_page.

Realization Ideas:create page class object, pass parameters for page_num (current page number), total_count (total number of articles), tartype (classification value), category (belongs to the category ID) [Note: tartpe used to distinguish between index page or category page, category for the query of the category ID, the index page value is empty. The value of index page is empty. can be modified according to their own functions to be achieved ] constructor will calculate the total number of pages and filter out erroneous data. Need to achieve a certain function is to directly call the class method can be.

class method:data_start() calculates the starting index value of the article on the current page
data_end() calculates the starting index value of the article on the current page
page_html() returns the html code of the paging module, which can be directly rendered to the front end.

Realize the effect:

When there are fewer articles:

When there are more articles:

This is the whole content of this article.