django分頁設計並封裝
阿新 • • 發佈:2018-08-31
orm nta fix pre lose {} html_ books har
一、封裝後的代碼
二、在視圖函數中如何調用
三、get點
一、封裝後的代碼
1 class Page(): 2 3 def __init__(self, page_num, total_count, url_prefix, per_page=10, max_page=11): 4 """ 5 6 :param page_num: 當前頁碼數 7 :param total_count: 數據總數 8 :param url_prefix: a標簽href的前綴 9 :param per_page: 每頁顯示多少條數據mypage.py10 :param max_page: 頁面上最多顯示幾個頁碼 11 """ 12 self.url_prefix = url_prefix 13 self.max_page = max_page 14 # 每一頁顯示多少條數據 15 # 總共需要多少頁碼來展示 16 total_page, m = divmod(total_count, per_page) 17 if m: 18 total_page += 1 19 self.total_page = total_page20 21 try: 22 page_num = int(page_num) 23 # 如果輸入的頁碼數超過了最大的頁碼數,默認返回最後一頁 24 if page_num > total_page: 25 page_num = total_page 26 except Exception as e: 27 # 當輸入的頁碼不是正經數字的時候 默認返回第一頁的數據 28 page_num = 1 29 self.page_num = page_num30 31 # 定義兩個變量保存數據從哪兒取到哪兒 32 self.data_start = (page_num - 1) * 10 33 self.data_end = page_num * 10 34 35 # 頁面上總共展示多少頁碼 36 if total_page < self.max_page: 37 self.max_page = total_page 38 39 half_max_page = self.max_page // 2 40 # 頁面上展示的頁碼從哪兒開始 41 page_start = page_num - half_max_page 42 # 頁面上展示的頁碼到哪兒結束 43 page_end = page_num + half_max_page 44 # 如果當前頁減一半 比1還小 45 if page_start <= 1: 46 page_start = 1 47 page_end = self.max_page 48 # 如果 當前頁 加 一半 比總頁碼數還大 49 if page_end >= total_page: 50 page_end = total_page 51 page_start = total_page - self.max_page + 1 52 self.page_start = page_start 53 self.page_end = page_end 54 55 @property 56 def start(self): 57 return self.data_start 58 59 @property 60 def end(self): 61 return self.data_end 62 63 64 def page_html(self): 65 # 自己拼接分頁的HTML代碼 66 html_str_list = [] 67 # 加上第一頁 68 html_str_list.append(‘<li><a href="{}?page=1">首頁</a></li>‘.format( self.url_prefix)) 69 70 # 判斷一下 如果是第一頁,就沒有上一頁 71 if self.page_num <= 1: 72 html_str_list.append(‘<li class="disabled"><a href="#"><span aria-hidden="true">«</span></a></li>‘.format(self.page_num-1)) 73 else: 74 # 加一個上一頁的標簽 75 html_str_list.append(‘<li><a href="{}?page={}"><span aria-hidden="true">«</span></a></li>‘.format( self.url_prefix, self.page_num-1)) 76 77 for i in range(self.page_start, self.page_end+1): 78 # 如果是當前頁就加一個active樣式類 79 if i == self.page_num: 80 tmp = ‘<li class="active"><a href="{0}?page={1}">{1}</a></li>‘.format(self.url_prefix, i) 81 else: 82 tmp = ‘<li><a href="{0}?page={1}">{1}</a></li>‘.format( self.url_prefix, i) 83 84 html_str_list.append(tmp) 85 86 # 加一個下一頁的按鈕 87 # 判斷,如果是最後一頁,就沒有下一頁 88 if self.page_num >= self.total_page: 89 html_str_list.append(‘<li class="disabled"><a href="#"><span aria-hidden="true">»</span></a></li>‘) 90 else: 91 html_str_list.append(‘<li><a href="{}?page={}"><span aria-hidden="true">»</span></a></li>‘.format( self.url_prefix, self.page_num+1)) 92 # 加最後一頁 93 html_str_list.append(‘<li><a href="{}?page={}">尾頁</a></li>‘.format( self.url_prefix, self.total_page)) 94 95 page_html = "".join(html_str_list) 96 return page_html
二、在視圖函數中如何調用
1 from app.mypage import Page 2 3 def books(request): 4 # 以參數形式得到當前的page頁 5 page_num = request.GET.get(‘page‘) 6 total_count = models.Book.objects.all().count() 7 page_obj = Page(page_num, total_count, url_prefix=‘/books/‘) 8 ret = models.Book.objects.all()[page_obj.start: page_obj.end] 9 page_html = page_obj.page_html() 10 return render(request, ‘books.html‘, {‘books‘: ret, ‘page_html‘: page_html})views.py
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>test demo</title> 6 <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css"> 7 </head> 8 <body> 9 <div class="container"> 10 <table class="table table-bordered"> 11 <thead> 12 <tr> 13 <th>number</th> 14 <th>id</th> 15 <th>name</th> 16 </tr> 17 </thead> 18 <tbody> 19 {% for book in books %} 20 <tr> 21 <td>{{ forloop.counter }}</td> 22 <td>{{ book.id }}</td> 23 <td>{{ book.name }}</td> 24 </tr> 25 {% endfor %} 26 </tbody> 27 </table> 28 <nav aria-label="Page navigation"> 29 <ul class="pagination"> 30 {{ page_html|safe }} 31 </ul> 32 </nav> 33 34 </div> 35 </body> 36 </html>books.html
三、get點
1.未封裝的代碼
1 def books(request): 2 page_num = request.GET.get("page") # 127.0.0.1:8000/book/?page=1 3 # 每頁顯示多少數據 4 per_page = 10 5 # 總數據是多少 6 total_count = models.Book.objects.all().count() 7 # 總共需要多少頁碼來展示 8 total_page, m = divmod(total_count, per_page) # divmod(23, 3);divmod(20,0) 9 if m: 10 total_page += 1 11 try: 12 page_num = int(page_num) # 得到的page參數是字符串形式的 13 if page_num > total_page: 14 page_num = total_page 15 except Exception as e: 16 # 當輸入的頁碼不是數字的時候,默認返回第一頁 17 page_num = 1 18 19 data_start = (page_num-1)*10 20 data_end = (page_num)*10 21 22 # 頁面上總共展示多少頁碼 23 max_page = 11 24 half_max_page = max_page //2 25 26 # 頁面展示的頁面從哪開始 27 page_start = page_num - half_max_page 28 page_end = page_num + half_max_page 29 if page_start <= 1: 30 page_start = 1 31 page_end = max_page + 1 32 33 # 如果當前頁加上一半 比總頁碼數還大 34 if page_end >= total_page: 35 page_end = total_page 36 page_start = total_page - max_page 37 38 # 每一頁取10個值 39 all_book = models.Book.objects.all()[data_start:data_end] 40 41 # 自己拼接分頁的html代碼 42 html_str_list = [] 43 # 加上第一頁 44 html_str_list.append(‘<li><a href="/books/?page=1">第一頁</li>‘) 45 46 # 加上上一頁 47 if page_num <= 1: 48 html_str_list.append(‘<li class="disabled"><a href="#"><span aria-hidden="true">«</span></a></li>‘) 49 else: 50 51 html_str_list.append(‘<li><a href="/books/?page={}"><span aria-hidden="true">«</span></a></li>‘.format(page_num-1)) 52 53 for i in range(page_start, page_end): 54 # 如果是當前頁就加一個active樣式 55 if i == page_num: 56 tmp = ‘<li class="active"><a href="/books/?page={0}">{0}</li>‘.format(i) 57 else: 58 tmp = ‘<li><a href="/books/?page={0}">{0}</li>‘.format(i) 59 html_str_list.append(tmp) 60 # 加上下一頁 61 if page_num >= total_page: 62 html_str_list.append(‘<li class="disabled"><a href="#"><span aria-hidden="true">»</span></a></li>‘) 63 else: 64 html_str_list.append(‘<li><a href="/books/?page={}"><span aria-hidden="true">»</span></a></li>‘.format(page_num+1)) 65 # 加上最後一頁 66 67 html_str_list.append(‘<li><a href="/books/?page={}">尾頁</li>‘.format(total_page)) 68 69 page_html = ‘‘.join(html_str_list) 70 71 return render(request, ‘books.html‘, {‘books‘: all_book, ‘page_html‘: page_html})未封裝的代碼.py
2.divmod
>>>divmod(22, 3) >>>(7, 1) >>>divmod(20, 2) >>>(10, 0) >>>divmod(23, 2) >>>(11, 1)
3.‘/‘ 和 ‘//‘ 的返回值
>>>10/2 >>>5.0 # 浮點數 >>>10//2 >>>5 # 整數 >>>abs(-10) >>>10 # 絕對值
4.ORM是對象,有索引值
>>>models.Book.objects.all() # QuerySet對象 >>>models.Book.objects.all()[0: 3] #可以根據索引取值
5.上一頁和下一頁圖標
« # 上一頁 << » # 下一頁 >>
6.url參數傳到後臺的類型是str
7.list轉為str
>>>li = [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘] >>>new_li = ‘‘.join(li) >>>new_li >>>‘12345‘ >>>new_li = ‘ ‘.join(li) >>>new_li >>>‘1 2 3 4 5‘
django分頁設計並封裝