. 1 .views.py
from app01 Import pager DEF Custom (Request): # current page: current_page = request.GET.get ( ' Page ' ) # database total rows ALL_COUNT are = models.UserInfo2.objects.all () COUNT (). # instantiating the object parameters: the current page number, number of rows in the database, 10 lines per page, url, page 11 per page page_info = pager.Pager (current_page, ALL_COUNT are, 10, ' /custom.html ' ,. 11 ) # user list displayed per page user_list = models.UserInfo2.objects.all () [page_info.start (): page_info.end ()] # accessories li Previous page Next string type of a tag page_str = page_info.pager() return render(request, 'custom.html', {'user_list':user_list, 'page_str':page_str})
2.custom.html <body> <h1>用户列表</h1> <ul> {% for row in user_list %} <li>{{ row.name }}</li> {% endfor %} </ul> <!--boorstrap分页--> <!--上 开始--> <nav aria-label="Page navigation"> <ul class="pagination"> <!--上 结束--> <!- | safe is to tell the browser that it is safe, otherwise the correct style cannot be displayed-> {{page_str | safe}} <!- Start next- > </ ul > </ nav > <!- End next- > </ body >
3.urls.py from app01 import views urlpatterns = [ url('custom.html', views.custom), ]
4. Custom pager module, which has Pager class Pager (object): def __init__ (self, current_page, all_count, per_page, base_url, show_page): "" " current_page: current page number all_count: total number of database rows per_page : per page show how many pieces of data, or how many rows base_url: user input url show_page: how many pages display a total "" " # try to execute, to determine the current page is not a string type of numbers, and if so, is strong to digital type, # then add attributes the try : self.current_page = int (current_page) # If not, there may be a string, the current page number to jump to the first page of the except Exception AS E: self.current_page =. 1 self.per_page = per_page # divmod is modulo, divmod (101,10) and the result is 10, 10 is the number of pages, page number 10 + 1 is the total number of A, b = divmod (ALL_COUNT are, per_page) # If b is greater than 0 means not divisible by the total number of pages = number of pages +1 IF b: A = A + 1 # total number of pages of property self.all_pager = A # per total number of pages displayed self.show_page = show_page self.base_url = base_url # 第 条条 开始def start (self): return (self.current_page-1) * self.per_page # 第 一个 条 开始defEnd (Self): return self.current_page * self.per_page # Suppose = 10 per_page # page display start and end page (excluding end piece) # 10-10 # 210-20 # 320-30 # Get all the tags of the string type of the previous page and the next page def pager (self): # v = "<a href='/custom.html?page=1'> 1 </a> <a href = ' </a> /custom.html?page=2'>2 " # return v # page list page_list = [] # ####################### Analyzing ############################ page # the number of pages per page half 11 to facilitate the taking back to the front of the current page 5 Page and last 5 pages int = Half ((self.show_page -. 1) / 2 ) # Page # 11 if the total number of pages <Showing the IF self.all_pager < self.show_page: the begin =. 1 STOP = self.all_pager. 1 + # + 1'd is because the range to the end of the head not to # if the total number of pages> = the number of pages per page. 11 the else : # If the current page number <= 5, in order to prevent occurrence of negative page, where always show 1,11 IF self.current_page <= Half : the begin =. 1 STOP = self.show_page. 1 + # If the current page number>. 5: the else : # If the current page number +5> the total number of pages, in order to prevent exceeding the total page number ifHalf + self.current_page> self.all_pager: the begin = self.all_pager - self.show_page +. 1 STOP = self.all_pager. 1 + # If the current page number +5 <= total number of pages of the else : the begin = self.current_page - Half STOP = self.current_page + half + 1 # ###################### ################## ############### # If the current page number <= 1, no previous jump point, is not set because href URL IF self.current_page <= 1 : PREV = " <Li> <a href='#'> Previous </a> </ li> " # If the current page number> 1, href sets the url and current page number -1 else : prev = " <li> <a href='%s?page=%s'> previous page </a> </ li> " % ( self.base_url, self.current_page - 1 ,) # page tag list additional previous page page_list.append (prev) # ####################### ## page ################################# for i in the Range (the begin, STOP): # If the page number is equal to The current page number, this label is selected, write class = 'active', it is the style class of bootstrap paging if i == self.current_page: temp = " <li class = 'active'> <a href = '% s ? page =% s'>% s </a></li>" %(self.base_url, i, i,) else : temp = " <li> <a href='%s?page=%s'>% s </a> </ li> " % (self.base_url, i , i,) page_list.append (temp) # ######################## #Next ############ #################### # If the current page number> = total number of pages, not the next point jump IF self.current_page> = self.all_pager: NEX = " <li> <a href='#'> Next </a> </ Li> " # If the current page number <the total number of pages, href url and set the current page number + 1'd the else : NEX = " <Li><a href='%s?page=%s'>下一页</a></li>"% (Self.base_url, self.current_page +. 1 ,) # page list is added next label page_list.append (NEX) ################## return string type All li a tag ################## # '' .join [ 'a', 'B', 'C', 'D', ....] The result is 'a, b, c, d ...' return '' .join (page_list)