一 本身封裝一個分頁類mypagecss
""" 網頁分頁功能,封裝了一個類,若是要使用須要傳三個參數,current_page, total_count, url_prefix """ # 封裝分頁類 class MyPage(object): def __init__(self, current_page, total_count, url_prefix, per_page=10, max_show=7): """ 初始化一個我本身定義的分頁實例 :param current_page: 當前頁碼 :param total_count: 總的數據量 :param url_prefix: 分頁中a標籤的url前綴 :param per_page: 每個顯示多少條數據 :param max_show: 頁面上最多顯示多少個頁碼 """ self.total_count = total_count self.per_page = per_page self.max_show = max_show self.url_prefix = url_prefix # 最多顯示頁碼數的一半 half_show = max_show // 2 # 由於URL取到的參數是字符串格式,須要轉換成int類型 try: current_page = int(current_page) except Exception as e: # 若是輸入的頁碼不是正經頁碼,默認展現第一頁 current_page = 1 # 求總共須要多少頁顯示 total_page, more = divmod(total_count, per_page) if more: total_page += 1 # 若是輸入的當前頁碼數大於總數據的頁碼數,默認顯示最後一頁 if current_page > total_page: current_page = total_page self.current_page = current_page # 計算一下顯示頁碼的起點和終點 show_page_start = current_page - half_show show_page_end = current_page + half_show # 特殊狀況特殊處理 # 1. 當前頁碼 - half_show <= 0 if current_page - half_show <= 0: show_page_start = 1 show_page_end = max_show # 2. 當前頁碼數 + hale_show >= total_page if current_page + half_show >= total_page: show_page_end = total_page show_page_start = total_page - max_show + 1 # 3. 總共須要的頁碼數 < max_show if total_page < max_show: show_page_start = 1 show_page_end = total_page self.show_page_start = show_page_start self.show_page_end = show_page_end self.total_page = total_page # 數據切片的起點 @property def start(self): return (self.current_page - 1) * self.per_page # 數據切片的終點 @property def end(self): return self.current_page * self.per_page # 序號也跟着變 def num(self): return (self.current_page-1)*self.per_page # 分頁的html代碼 def page_html(self): tmp = [] page_html_start = '<nav aria-label="Page navigation" class="text-center"><ul class="pagination">' page_html_end = '</ul></nav>' tmp.append(page_html_start) # 添加一個首頁 tmp.append('<li><a href="/{}?page=1">首頁</a></li>'.format(self.url_prefix)) # 添加一個上一頁 # 噹噹前頁是第一頁的時候不能再點擊上一頁 if self.current_page - 1 <= 0: tmp.append( '<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a></li>') else: tmp.append( '<li><a href="/{}?page={}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>'.format( self.url_prefix, self.current_page - 1)) # for循環添加要展現的頁碼 for i in range(self.show_page_start, self.show_page_end + 1): # 若是for循環的頁碼等於當前頁碼,給li標籤加一個active的樣式 if self.current_page == i: tmp.append('<li class="active"><a href="/{1}?page={0}">{0}</a></li>'.format(i, self.url_prefix)) else: tmp.append('<li><a href="/{1}?page={0}">{0}</a></li>'.format(i, self.url_prefix)) # 添加一個下一頁 # 當前 當前頁已是最後一頁,應該不讓下一頁按鈕能點擊 if self.current_page + 1 > self.total_page: tmp.append( '<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">»</span></a></li>') else: tmp.append( '<li><a href="/{}?page={}" aria-label="Previous"><span aria-hidden="true">»</span></a></li>'.format( self.url_prefix, self.current_page + 1)) # 添加一個尾頁 tmp.append('<li><a href="/{}?page={}">尾頁</a></li>'.format(self.url_prefix, self.total_page)) tmp.append(page_html_end) page_html = "".join(tmp) return page_html
二 而後在views函數裏面調用html
記得先導入你本身寫的那個類數據庫
import mypage
def book_list(request): book = models.Book.objects.all() total_count = book.count() current_page = request.GET.get("page") # 分頁功能開始 page_boj = mypage.MyPage(current_page, total_count, url_prefix="book_list") data = book[page_boj.start:page_boj.end] # 從第幾頁顯示到第幾頁 page_html = page_boj.page_html() # 頁面 page_num = page_boj.num() # 序號 return render(request, "book_list.html", {"book": data, "page_html": page_html, "num": page_num})
三 html設置django
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>列表</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.min.css"> </head> <body> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> {# # table-striped 設置灰色背景#} <table class="table table-bordered table-striped"> <thead> <tr> <th>序號</th> <th>id</th> <th>書名</th> <th>出版社id</th> </tr> </thead> <tbody> {% for book in book %} <tr> <td>{{ forloop.counter|add:num}}</td> <td>{{ book.id }}</td> <td>{{ book.title }}</td> <td>{{ book.publisher.name }}</td> </tr> {% endfor %} </tbody> </table> {{ page_html|safe }} </div> </div> </div> </body> </html>
四 驗證:bootstrap
如何快速向數據庫中插入批量數據,建立的時候用bulk_creat去建立app
1 import os 2 import django 3 if __name__ == "__main__": 4 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "練習.settings") 5 django.setup() 6 from app01 import models 7 # 建立500個書籍對象 8 book_list=[models.Book(title="貝多芬第{}交響曲".format(i),publisher_id=i) for i in range (500)] 9 # 批量提交到數據庫 10 models.Book.objects.bulk_create(book_list)