1 fromDjango.utils.safestringImportMark_safe2 3 4 classPage:5 def __init__(Self, current_page, Data_count, per_page_count=2, pager_num=7):6Self.current_page =Current_page7Self.data_count =Data_count8Self.per_page_count =Per_page_count9Self.pager_num =Pager_numTen One @property A defStart (self): - return(self.current_page-1) *Self.per_page_count - the @property - defEnd (self): - returnSelf.current_page *Self.per_page_count - + @property - defTotal_count (self): +V, y =Divmod (Self.data_count, Self.per_page_count) A ify: atV + = 1 - returnv - - defpage_str (Self, base_url): -Page_list = [] - in ifSelf.total_count <Self.pager_num: -Start_index = 1 toEnd_index = Self.total_count + 1 + Else: - ifSelf.current_page <= (self.pager_num + 1)/2: theStart_index = 1 *End_index = self.pager_num + 1 $ Else:Panax NotoginsengStart_index = Self.current_page-(self.pager_num-1)/2 -End_index = Self.current_page + (self.pager_num + 1)/2 the if(Self.current_page + (self.pager_num-1)/2) >Self.total_count: +End_index = Self.total_count + 1 AStart_index = self.total_count-self.pager_num + 1 the + ifSelf.current_page = = 1: -Prev ='<a class= "page" href= "javascript:void (0);" > Prev </a>' $ Else: $Prev ='<a class= "page" href= "%s?p=%s" > Prev </a>'% (Base_url, self.current_page-1,) - page_list.append (prev) - the forIinchRange (int (start_index), int (end_index)): - ifi = =Self.current_page:Wuyitemp ='<a class= "page active" href= "%s?p=%s" >%s</a>'%(Base_url, I, i) the Else: -temp ='<a class= "page" href= "%s?p=%s" >%s</a>'%(Base_url, I, i) Wu page_list.append (temp) - About ifSelf.current_page = =Self.total_count: $NEX ='<a class= "page" href= "javascript:void (0);" > Next </a>' - Else: -NEX ='<a class= "page" href= "%s?p=%s" > Next </a>'% (Base_url, Self.current_page + 1,) - Page_list.append (NEX) A +Jump =""" the <input type= ' text '/><a onclick= ' Jumpto (This, "%s?p="); ' >GO</a> - <script> $ function Jumpto (ths,base) { the var val = ths.previousSibling.value; the Location.href = base + val; the } the </script> - """%(Base_url,) in the page_list.append (jump) the AboutPage_str = Mark_safe ("". Join (page_list)) the the returnPage_str
User manual for the paging plugin:
1. Create the Utils directory in the app sibling directory
2. Create the pagination.py file within the Utils directory and copy the above code
3. View function Invocation example.
1 defHost (Request):2 ifRequest.method = ="GET":3Host_list =models. HostInfo.objects.all ()4Group_list =models. HostGroup.objects.all ()5 Print(">>>", Group_list)6 7Current_page = Request. Get.get ('P', 1)8Current_page =Int (current_page)9val = Request. Get.get ('Per_page_count', 2)Tenval =Int (val) OnePage_obj =pagination. Page (Current_page, Len (host_list), Val) Adata =Host_list[page_obj.start:page_obj.end] -Page_str = Page_obj.page_str ("/host") - returnRender (Request,"host.html",{"host_list": Data,"group_list": Group_list,"Page_str":p age_str}) the elifRequest.method = ="POST": - Print("Post Method") -hostname = Request. Post.get ("hostname") -HostIP = Request. Post.get ("HostIP") +Hostpassword = Request. Post.get ("Password") -group_id = Request. Post.get ("group_id") +Models. HostInfo.objects.create (serverip=hostip,servername=hostname,serverpassword=hostpassword,servergroup_id=group_id) A returnredirect"/host")
View Code
4. Front-end HTML reference add
1 <style>2 . Pagination. Page{3 Display:Inline-block;4 padding:5px;5 Background-color:Cyan;6 margin:5px;7 }8 . Pagination. Page.active{9 Background-color:Brown;Ten Color: White; One } A - </style> - the - - - </Div> + <Divstyle= "Align:center;p osition:fixed;bottom:3%;left:40%"class= "pagination"> - {{PAGE_STR}} + </Div>
View Code
Django Pagination Plugin