Page out
I. Django built-in paging
From django.shortcuts Import 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 ')
Paginator = Paginator (L, 10)
# Per_page: Shows the number of items per page
# Count: Total number of data
# num_pages: Total pages
# Page_range: Index range of total pages, such as: (1,10), (1,200)
# Page:page Objects
Try
Posts = Paginator.page (current_page)
# Has_next Whether there is a next page
# Next_page_number Next Page
# does Has_previous have a previous page
# Previous_page_number Previous Page
# object_list List of data after pagination
# current Page
# Paginator Paginator Objects
Except Pagenotaninteger:
Posts = paginator.page (1)
Except Emptypage:
Posts = Paginator.page (paginator.num_pages)
return render (Request, ' index.html ', {' posts ': posts})
views.py
From django.shortcuts import renderfrom django.core.paginator import paginator, emptypage, Pagenotanintegerl = []for i in Range (999): l.append (i) def index (request): current_page = Request. Get.get (' P ') paginator = Paginator (L, ten) # Per_page: Number of entries per page # Count: total Data # Num_pages: Total number of pages # Page_range: Index range of total pages, such as: (1,10), (1,200) # page: Page Object Try: posts = paginator.page (Current_ page) # Has_next If there's a next page # next_page_number next page # # has_previous If there's a page # Previous_page_number Previous Page # object_list data list after pagination # number current page # paginator Paginator object except Pagenotaninteger: posts = paginator.page (1) except Emptypage: posts = Paginator.page (paginator.num_pages) return render (Request, ' index.html ', {' posts ': posts})
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<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
<! DOCTYPE html>
From django.shortcuts Import Render
From Django.core.paginator import Paginator, Emptypage, Pagenotaninteger
Class Custompaginator (paginator):
def __init__ (self, current_page, Max_pager_num, *args, **kwargs):
"" "
:p Aram Current_page: Current page
:p Aram Max_pager_num: Maximum number of pages displayed
:p Aram args:
:p Aram Kwargs:
: return:
"" "
Self.current_page = Int (current_page)
Self.max_pager_num = Max_pager_num
Super (Custompaginator, self). __ Init__ (*args, **kwargs)
def page_num_range (self):
# current Page
# Self.current_page
# Total Pages
# self.num_pages
# Maximum number of pages displayed
# Self.max_pager_num
Print (1)
If Self.num_pages < self.max_pager_num:
Return range (1, self.num_pages + 1)
Print (2)
part = Int (SELF.MAX_PAGER_NUM/2)
If Self.current_page-part < 1:
Return range (1, Self.max_pager_num + 1)
Print (3)
If Self.current_page + part > Self.num_pages:
Return Range (Self.num_pages + 1-self.max_pager_num, self.num_pages + 1)
Print (4)
Return range (Self.current_page-part, Self.current_page + part + 1)
L = []
For I in range (999):
L.append (i)
def index (Request):
Current_page = Request. Get.get (' P ')
Paginator = Custompaginator (Current_page, one, L, 10)
# Per_page: Shows the number of items per page
# Count: Total number of data
# num_pages: Total pages
# Page_range: Index range of total pages, such as: (1,10), (1,200)
# Page:page Objects
Try
Posts = Paginator.page (current_page)
# Has_next Whether there is a next page
# Next_page_number Next Page
# does Has_previous have a previous page
# Previous_page_number Previous Page
# object_list List of data after pagination
# current Page
# Paginator Paginator Objects
Except Pagenotaninteger:
Posts = paginator.page (1)
Except Emptypage:
Posts = Paginator.page (paginator.num_pages)
return render (Request, ' index.html ', {' posts ': posts})
Extended built-in paging: views.py
From django.shortcuts import renderfrom django.core.paginator import paginator, Emptypage, Pagenotanintegerclass Custompaginator (paginator): Def __init__ (self, current_page, Max_pager_num, *args, **kwargs): "" ":p Aram Current_page: Current page:p Aram Max_pager_num: The maximum number of pages displayed:p Aram args::p Aram Kwargs:: Return: " "" Self.current_page = Int (current_page) Self.max_pager_num = Max_pager_num super (custompaginator, SE LF). __init__ (*args, **kwargs) def page_num_range (self): # current Page # self.current_page # Total Pages # Self.num_pages # Number of pages displayed up to # Self.max_pager_num print (1) if self.num_pages < Self.max_pager _num:return Range (1, self.num_pages + 1) print (2) part = Int (SELF.MAX_PAGER_NUM/2) if s Elf.current_page-part < 1:return range (1, self.max_pager_num + 1) print (3) if Self.current_ Page + part > self.num_pages: Return Range (Self.num_pages + 1-self.max_pager_num, self.num_pages + 1) print (4) return range (s Elf.current_page-part, Self.current_page + part + 1) L = []for i in Range (999): L.append (i) def index (request): Curr Ent_page = Request. Get.get (' P ') Paginator = Custompaginator (Current_page, one, L, ten) # Per_page: Number of entries per page # Count: Total number of data # num _pages: Total Pages # Page_range: Index range of total pages, such as: (1,10), (1,200) # Page:page Object try:posts = Paginator.page (current_ Page) # Has_next If there is a page # Next_page_number next page # has_previous if there's a previous page # previous_page_number Page # # object_list data List after page # number current page # Pag Inator Paginator object except pagenotaninteger:posts = Paginator.page (1) except Emptypage:po STS = Paginator.page (Paginator.num_pages) return render (Request, ' index.html ', {' posts ': posts})<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<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%}
{% for I in posts.paginator.page_num_range%}
<a href= "? p={{i}}" >{{I}}</a>
{% ENDFOR%}
{% if Posts.has_next%}
<a href= "? p={{Posts.next_page_number}}" >Next</a>
{% ENDIF%}
</span>
<span class= "Current" >
Page {{Posts.number}} of {{posts.paginator.num_pages}}.
</span>
</div>
</body>
Extended built-in paging: Html
<! DOCTYPE html>
Second, custom paging
Paging functionality is necessary for each site, and for paging, it is the user's input that calculates the starting position in the database table.
1. Set the number of data bars per page
2. User Input page number (first page, second page ...) )
3, according to the set of each page shows the number of bars and the current page number, calculate the need to take the data table starting position
4, in the data table according to the starting position value, the page output data
The demand is coming again, and the page needs to be displayed on the page. such as: [Previous][1][2][3][4][5][next page]
1. Set the number of data bars per page
2. User Input page number (first page, second page ...) )
3. Set the number of pages displayed
4. Get the total number of current data
5, according to the set display number of pages and the total number of data calculated, total pages
6, according to the set of each page shows the number of bars and the current page number, calculate the need to take the data table starting position
7, in the data table according to the starting position value, the page output data
8, output pagination HTML, such as: [Prev][1][2][3][4][5][Next]
#!/usr/bin/env python# _*_coding:utf-8_*_from django.utils.safestring Import Mark_safe class PageInfo (object): Def __in It__ (self,current,totalitem,peritems=5): Self.__current=current self.__peritems=peritems self.__total Item=totalitem def from (self): return (self.__current-1) *self.__peritems def to (self): return Self.__c Urrent*self.__peritems def totalpage (self): #总页数 result=divmod (self.__totalitem,self.__peritems) if ResU Lt[1]==0:return Result[0] Else:return result[0]+1 def custompager (baseurl,currentpage,total Page): #基础页, current page, total pages perpager=11 #总页数 <11 #0-totalpage #总页数 >11 #当前页大于5 currentPage-5--current Page+5 #currentPage +5 Whether the total number of pages, more than the total number of pages, end is the total number of pages #当前页小于5 0--begin=0 end=0 if Totalpage <= 11 : begin=0 end=totalpage else:if currentpage>5:begin=currentpage-5 end=c Urrentpage+5If End > Totalpage:end=totalpage else:begin=0 end=11 pager_list=[] If currentpage<=1:first= "<a href=" > Home </a> "else:first=" <a href= '%s%d ' > Home </a> ; "% (baseurl,1) pager_list.append (first) if currentpage<=1:prev=" <a href= "> Previous </a>" Els e:prev= "<a href= '%s%d ' > Prev </a>"% (baseurl,currentpage-1) pager_list.append (prev) for i in rang E (begin+1,end+1): if i = = currentpage:temp= "<a href= '%s%d ' class= ' selected ' >%d</a>"% (base url,i,i) else:temp= "<a href= '%s%d ' >%d</a>"% (baseurl,i,i) pager_list.append (temp) If currentpage>=totalpage:next= "<a href= ' # ' > Next </a>" else:next= "<a href= '%s%d ' > Next Page </a> "% (baseurl,currentpage+1) pager_list.append (next) if currentpage>=totalpage:last=" <a HRE f= "> End </a>" else:last= "<a href= '%s%d ' > End </a>"% (baseurl,totalpage) pager_list.append (last) result= '. Join ( Pager_list) return Mark_safe (Result) #把字符串转成html语言
#!/usr/bin/env python# _*_coding:utf-8_*_from django.utils.safestring Import Mark_safe class PageInfo (object): Def __in It__ (self,current,totalitem,peritems=5): Self.__current=current self.__peritems=peritems self.__total Item=totalitem def from (self): return (self.__current-1) *self.__peritems def to (self): return Self.__c Urrent*self.__peritems def totalpage (self): #总页数 result=divmod (self.__totalitem,self.__peritems) if ResU Lt[1]==0:return Result[0] Else:return result[0]+1 def custompager (baseurl,currentpage,total Page): #基础页, current page, total pages perpager=11 #总页数 <11 #0-totalpage #总页数 >11 #当前页大于5 currentPage-5--current Page+5 #currentPage +5 Whether the total number of pages, more than the total number of pages, end is the total number of pages #当前页小于5 0--begin=0 end=0 if Totalpage <= 11 : begin=0 end=totalpage else:if currentpage>5:begin=currentpage-5 end=c Urrentpage+5If End > Totalpage:end=totalpage else:begin=0 end=11 pager_list=[] If currentpage<=1:first= "<a href=" > Home </a> "else:first=" <a href= '%s%d ' > Home </a> ; "% (baseurl,1) pager_list.append (first) if currentpage<=1:prev=" <a href= "> Previous </a>" Els e:prev= "<a href= '%s%d ' > Prev </a>"% (baseurl,currentpage-1) pager_list.append (prev) for i in rang E (begin+1,end+1): if i = = currentpage:temp= "<a href= '%s%d ' class= ' selected ' >%d</a>"% (base url,i,i) else:temp= "<a href= '%s%d ' >%d</a>"% (baseurl,i,i) pager_list.append (temp) If currentpage>=totalpage:next= "<a href= ' # ' > Next </a>" else:next= "<a href= '%s%d ' > Next Page </a> "% (baseurl,currentpage+1) pager_list.append (next) if currentpage>=totalpage:last=" <a HRE f= "> End </a>" else:last= "<a href= '%s%d ' > End </a>"% (baseurl,totalpage) pager_list.append (last) result= '. Join ( Pager_list) return Mark_safe (Result) #把字符串转成html语言
To summarize, there are three things to do when paging:
- To create a class that processes paging data
- Get data based on paging data
- Output pagination HTML, that is: [prev][1][2][3][4][5][Next]
Js:location.href=base Base path for page jumps in the front end of pagination +val page number
django-Sub-page