Django Page Paging function, if you have questions, please leave a message

Source: Internet
Author: User

URL file

 from Import Admin  from Import URL  from Import     = [url (r ' ^user_list ', views.user_list)]     

To views file

defuser_list (Request): Pagesizenumber= Request. Cookies.get ('Pagesizenumber', 10)#Gets the key (' Pagesizenumber ') passed by the cookie, which defaults to ten .Current_number = Request. Get.get ('P', 1)#get the front end a? p = value, this is the current page numberOne_page_number = 11#number of pages displayed per pagePagefi =pagenation. Page (List, Pagesizenumber, Current_number, One_page_number) #生成类 (Show_page, List_page)=pagefi.page_str () #返回两个参数returnRender (Request,'user_list.html', {'Show_page': Show_page,'List_page': List_page})

For the above pagenation. Page in the page.py file

 fromDjango.utils.safestringImportMark_safeclassPage:def __init__(Self, List, Pagesizenumber, current_number, One_page_number = 11):        #List Enter the incoming data, pagesizenumber the amount of data displayed per page, current_number the current page number, One_page_number: Number of pages per page, by defaultSelf.pagesizenumber =Pagesizenumber Self.current_number=Current_number Self.one_page_number=One_page_number Self. List=List @propertydefPage_number (self): Self.one_math_number=Int (self.pagesizenumber) Page_number, v= Divmod (len (self). List), Self.one_math_number)#Page_number Total page number, calculate the total number of pages required        returnPage_number @propertydefshow_page (self): Self.current_number= Int (self.current_number)#Current Page CountSelf.one_math_number =Int (self.pagesizenumber) Page_number, v= Divmod (len (self). List), Self.one_math_number)#calculate the total number of pages needed        ifV >0:page_number+ = 1#if greater than 1, page number +1Show_page = self. list[(self.current_number-1) * Self.one_math_number:self.current_number * Self.one_math_number]#the number of pages displayed in        returnShow_pagedefpage_str (self): List_page=[] Self.current_number=Int (self.current_number) Self.one_math_number=Int (self.pagesizenumber) Self.one_page_number=Int (self.one_page_number)ifSelf.page_number < self.one_page_number:#total number of pages is less than page numberStart_page = 1End_page= Int (self.page_number) + 1Else:#total number of pages is greater than single page number            ifSelf.current_number <= Int ((Self.one_page_number + 1)/2):#The current page number is less than the middle of a page numberStart_page = 1End_page= Int (self.one_page_number) + 1Print(start_page)Print(end_page)elifSelf.page_number-int ((self.one_page_number-1)/2) > Self.current_number >Int ((Self.one_page_number+ 1)/2):#The current number of pages is greater than the number of page numbers in the middle of the total page number -one page number middle #开始发生变化start_page = Int (Self.current_number-(Self.one_page_number)-1)/2) End_page= Int (Self.current_number + ((self.one_page_number) + 1)/2)            Else: Start_page= Self.page_number-self.one_page_number#Current page number is greater than total page number-the middle of a single page numberEnd_page = Self.page_number + 1#Previous Page button        ifSelf.current_number = = 1: x= ("<a class= ' paging active ' href= ' # > Prev </a>")        Else: x= ("<a class= ' paging active ' href= '/user_list/?p=%s ' > prev </a>"% (self.current_number-1,)) List_page.append (x)#Add a tag         forIinchRange (Start_page, end_page):ifi = =self.current_number:x= ("<a class= ' paging active ' href= '/user_list/?p=%s ' >%s</a>"%(i, i))Else: x= ("<a class= ' paging ' href= '/user_list/?p=%s ' >%s</a>"%(i, I)) list_page.append (x)#Next Page button        ifSelf.current_number = =self.page_number:x= ("<a class= ' paging active ' href= ' # ' > Next </a>")        Else: x= ("<a class= ' paging active ' href= '/user_list/?p=%s ' > next </a>"% (Self.current_number + 1,)) List_page.append (x) x=" "<input type= "text" ><a onclick = ' Jumpto (this, "/user_list/?p=") ';>go</a> <script> function Jumpto (ths, base) {var val = Ths.previousSibling.valu                         E                    Location.href = base + val; } </script>" "        #Ths.previousSibling.value Find the value of the input above it        #Location.href = base + val Jump        #x = " "        #<input type= ' text ' ><a onclick= ' Jumpto (This, "/user_list/?p=") ';>go</a>        #<script>        #function Jumpto (ths,base) {        #var val = ths.previousSibling.value;        #Location.href = base + val;        #      }        #</script>        #" "list_page.append (x) list_page="'. Join (List_page)Print(list_page) list_page=Mark_safe (list_page)returnSelf.show_page, List_page

For user_list.html files

<! DOCTYPE html>"en">"UTF-8"> <title>Title</title> <style>. Pagination. paging{padding:5px;            margin:5px; Background-Color:aqua;        Color:white; }. Pagination. paging.active{background-color:red;        Color:white; }    </style>         {% forIinchShow_page%}             <li>{{I}}</li>         {% ENDFOR%}     </ul>{#{% for I in List_page%}#}{#<a class= ' paging ' href= "/user_list/?p={{i}}" >{{I}}</a>#}{#{% endfor%}#}<divclass= pagination>{{List_page}}</div>{#onchange Detection Change #}<select ID ='I1'Onchange="Changepagesize (This)"> <option value="Ten">10</option> <option value=" -">30</option> <option value=" -">50</option> <option value=" -">100</option> </select><script src ='/static/jquery-3.3.1.js'></script><script src="/static/jquery.cookie.js"></script><script>{#As soon as the file comes in, get the Pagesizenumber value, change the values, the initial value is 10#}$ (function () {var v= $.cookie ('Pagesizenumber'); if(v) {$('#i1'). Val (v);} Else{$('#i1'). Val (10)}    }); function Changepagesize (ths) {var v=$ (THS). Val (); $.cookie ('Pagesizenumber', V, {path:'/user_list/'}); Location.reload (); {#Refresh implementation Jump #}    }</script></body>

Django Page Paging function, if you have questions, please leave a message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.