Develop their own plug-python pagination

Renderings:

 

 

 views.py file

 1 from django.shortcuts import render
 2 from app01.pager import Pagination
 3 # Create your views here.
 4 
 5 user_list = []
 6 
 7 for i in range(1,666):
 8     user = {'name':'root%s'% i ,'age':i}
 9     user_list.append(user)
10 
11 
12 def index(request):
13     cur_page = request.GET.get('p')
14 
15     item = Pagination(len(user_list),cur_page,'index.html')
16     data = user_list[item.start:item.end]
17     return render(request,'index.html',{'data':data,'item':item})

Develop their own paging file pager.py

  . 1  class Pagination:
   2      DEF  the __init__ (Self, maxPageItems, currenPageNum, URL, pageItemLs = 20 is, maxPageNum =. 11 ):
   . 3          '' ' 
  . 4  
  . 5          : param maxPageItems: total number of data page_items_max ---
   . 6          : param currenPageNum: Current page curren_page_num ---
   7          : param pageItemLs: a number of data show page_item_list ---
   8          : param maxPageNum: page number page number shows up max_page_num ---
   9          : param url: paging in which page url ---
 10          ' '' 
. 11          self.url = URL
 12 is          self.page_items_max =maxPageItems
 13 is          self.page_item_list = pageItemLs
 14  
15          # If the display page number is greater than a maximum total number of pages, then the total number of pages up to page number assigned to the display 
16          self.max_page_num = maxPageNum IF self.total_page_num> maxPageNum the else self.total_page_num
 . 17          '' ' 
18          if the incoming current page number is not an integer, the current page number directly assigned 1,
 19          if the incoming current page number is less than 0, the current page number directly assigned 1,
 20          if the incoming data is larger than the current page can be divided out the maximum number of pages, current page number assigned to put the maximum number of pages
 21          otherwise put the incoming current page number assigned to the current page number
 22          '' ' 
23          the try :
 24-              v = int (currenPageNum)
 25             if v <= 0 :
 26                 self.curren_page_num = 1
 27             elif v > self.total_page_num:
 28                 self.curren_page_num = self.total_page_num
 29             else:
 30                 self.curren_page_num = v
 31         except Exception as e:
 32             self.curren_page_num = 1
 33 
 34 
 35     @property
 36     def total_page_num(self):
 37         '''
 38         计算总页数
 39         :return:
 40         '' ' 
41 is          Total, B = divmod (self.page_items_max, self.page_item_list)
 42 is          Total = Total +. 1 IF ! B = 0 the else Total
 43 is          return   Total
 44 is  
45      @Property
 46 is      DEF Start (Self):
 47          ' '' is calculated the starting position of the slice data slice '' ' 
48          return (self.curren_page_num -1) * self.page_item_list
 49  
50      @Property
 51 is      DEF end (Self):
 52 is          '' ' the end of the slice data slice position calculating ' '' 
53 is          return  * self.curren_page_num self.page_item_list
 54 is  
55  
56 is      DEF pagenum_range (Self):
 57 is          '' ' 
58          dynamically generated page
 59          : return:
 60          ' '' 
61 is          # to half the number of the page displayed is a critical point 
62 is          Page = self.max_page_num / / 2
 63 is          IF self.curren_page_num <= Page:
 64              # if less than a critical point of the current page, the page display is 1-- the maximum number of pages can be separated 
65              return Range (. 1,. 1 self.max_page_num + )
 66  
67              # if ( this page + page) is greater than the total number of pages, the page number is displayed (total number of pages - displaying a maximum number of page 1) - (total number of pages. 1 +) 
68          IF(Page self.curren_page_num +)> self.total_page_num:
 69              return Range (self.total_page_num - self.max_page_num. 1 +, + 1'd self.total_page_num )
 70  
71 is           # page number is displayed (current page number - page) - (this page Page. 1 + +) 
72          return   Range (self.curren_page_num - Page, Page self.curren_page_num + +. 1 )
 73 is  
74      DEF item_list (Self):
 75          '' ' 
76          returns the HTML code
 77          : return:
 78          ' '' 
79  
80          Item = []
 81          ELE = '<li><a href="%s?p=1">首页</a></li>' % (self.url)
 82         item.append(ele)
 83         if self.curren_page_num == 1 :
 84             ele = '<li class="disabled"><a>上一页</a></li>'
 85         else:
 86             ele = '<li><a href="%s?p=%s">上一页</a></li>' % (self.url,self.curren_page_num - 1)
 87         item.append(ele)
 88         for i in self.pagenum_range():
 89             if i == self.curren_page_num:
 90                 ele = '<li class="active"><a href="%s?p=%s">%s</a></li>' % (self.url,i,i)
 91             else:
 92                 ele = '<li><a href="%s?p=%s">%s</a></li>' % (self.url, i, i)
 93             item.append(ele)
 94 
 95         if self.curren_page_num == self.total_page_num:
 96             ele = '<li class="disabled"><a>下一页</a></li>'
 97         else:
 98             ele = '<li><a href="%s?p=%s">下一页</a></li>'% (self.url,self.curren_page_num + 1)
 99         item.append(ele)
100 
101         ele = '<li><a href="%s?p=%s">尾页</a></li>' % (self.url,self.total_page_num)
102         item.append(ele)
103 
104 
105         return ''.join(item)
<!DOCTYPE html>
<html lang="en">
<head>
    {% load staticfiles %}
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static 'plugin/bootstrap-3.3.7-dist/css/bootstrap.css' %}">
</head>
<body>

    <ul>
        {% for i in data %}
            <li> {{ i.name }} - {{ i.age }}</li>
        {% endfor %}
    </ul>

    <nav aria-label="...">
      <ul class="pagination">
        {{ item.item_list | safe}}
     </ul>
   </nav>



</body>
</html>
index.html

 

 

Guess you like

Origin www.cnblogs.com/xieys-1993/p/11910638.html