本文實例為大家分享了Django自定義分頁的具體代碼,供大家參考,具體內容如下
穩扎穩打版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
def book(request): # 從URL取參數(訪問的頁碼) page_num = request.GET.get( "page" ) try : # 將取出的page轉換為int類型 page_num = int(page_num) except Exception as e: # 當輸入的頁碼不是正經數字的時候 默認返回第一頁的數據 page_num = 1 # 數據庫總數據是多少條 total_count = models.Book.objects.all(). count () # 每一頁顯示多少條數據 per_page = 10 # 總共需要多少頁碼來展示 total_page, m = divmod(total_count, per_page) if m: total_page += 1 # 如果輸入的頁碼數超過了最大的頁碼數,默認返回最后一頁 if page_num > total_page: page_num = total_page # 定義兩個變量從哪里開始到哪里結束 data_start = (page_num - 1) * 10 data_end = page_num * 10 # 頁面上總共展示多少頁碼 max_page = 11 if total_page < max_page: max_page = total_page # 把從URL中獲取的page_num 當做是顯示頁面的中間值, 那么展示的便是當前page_num 的前五頁和后后五頁 half_max_page = max_page // 2 # 根據展示的總頁碼算出頁面上展示的頁碼從哪兒開始 page_start = page_num - half_max_page # 根據展示的總頁碼算出頁面上展示的頁碼到哪兒結束 page_end = page_num + half_max_page # 如果當前頁減一半 比1還小, 不然頁面上會顯示負數的頁碼 if page_start <= 1: page_start = 1 page_end = max_page # 如果 當前頁 加 一半 比總頁碼數還大, 不然頁面上會顯示比總頁碼還大的多余頁碼 if page_end >= total_page: page_end = total_page page_start = total_page - max_page + 1 # 從數據庫取值, 并按照起始數據到結束數據展示 all_book = models.Book.objects.all()[data_start:data_end] # 自己拼接分頁的HTML代碼 html_str_list = [] # # 加上首頁 html_str_list.append( '<li><a href="/book/?page=1" rel="external nofollow" >首頁</a></li>' ) # 斷一下 如果是第一頁,就沒有上一頁 if page_num <= 1: html_str_list.append( '<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>' ) else : # 不是第一頁,就加一個上一頁的標簽 html_str_list.append( '<li><a href="/book/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">«</span></a></li>' .format(page_num - 1)) for i in range(page_start, page_end + 1): # 如果是當前頁就加一個active樣式類 if i == page_num: tmp = '<li class="active"><a href="/book/?page={0}" rel="external nofollow" rel="external nofollow" >{0}</a></li>' .format(i) else : tmp = '<li><a href="/book/?page={0}" rel="external nofollow" rel="external nofollow" >{0}</a></li>' .format(i) html_str_list.append(tmp) # 判斷,如果是最后一頁,就沒有下一頁 if page_num >= total_page: html_str_list.append( '<li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>' ) else : # 不是最后一頁, 就加一個下一頁標簽 html_str_list.append( '<li><a href="/book/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span aria-hidden="true">»</span></a></li>' .format(page_num + 1)) # 加上尾頁 html_str_list.append( '<li><a href="/book/?page={}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a></li>' .format(total_page)) page_html = "" .join(html_str_list) return render(request, "book.html" , { "all_book" :all_book, "page_html" :page_html}) |
book.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title >書籍列表</ title > < link rel = "stylesheet" href = "/static/bootstrap/css/bootstrap.min.css" > </ head > < body > < div class = "container" > < table class = "table table-bordered" > < thead > < tr > < th >序列號</ th > < th >ID值</ th > < th >書名</ th > < th >時間</ th > </ tr > {% for book in all_book %} < tr > < td >{{ forloop.counter }}</ td > < td >{{ book.id }}</ td > < td >{{ book.name }}</ td > < td >{{ book.date }}</ td > </ tr > {% endfor %} </ thead > </ table > < nav aria-label = "Page navigation" > < ul class = "pagination" > {{ page_html|safe }} </ ul > </ nav > </ div > </ body > </ html > |
封裝保存版
封裝保存版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
class Page(object): def __init__(self, page_num, total_count, url_prefix, per_page=10, max_page=11): "" " :param page_num: 當前頁碼數 :param total_count: 數據總數 :param url_prefix: a標簽href的前綴 :param per_page: 每頁顯示多少條數據 :param max_page: 頁面上最多顯示幾個頁碼 "" " self.url_prefix = url_prefix self.max_page = max_page # 總共需要多少頁碼來展示 total_page, m = divmod(total_count, per_page) if m: total_page += 1 self.total_page = total_page try : # 將取出的page轉換為int類型 page_num = int(page_num) except Exception as e: # 當輸入的頁碼不是正經數字的時候 默認返回第一頁的數據 page_num = 1 # 如果輸入的頁碼數超過了最大的頁碼數,默認返回最后一頁 if page_num > total_page: page_num = total_page self.page_num = page_num # 定義兩個變量保存數據從哪兒取到哪兒 self.data_start = (page_num - 1) * 10 self.data_end = page_num * 10 # 頁面上總共展示多少頁碼 if total_page < self.max_page: self.max_page = total_page half_max_page = self.max_page // 2 # 頁面上展示的頁碼從哪兒開始 page_start = page_num - half_max_page # 頁面上展示的頁碼到哪兒結束 page_end = page_num + half_max_page # 如果當前頁減一半 比1還小, 不然頁面上會顯示負數的頁碼 if page_start <= 1: page_start = 1 page_end = self.max_page # 如果 當前頁 加 一半 比總頁碼數還大, 不然頁面上會顯示比總頁碼還大的多余頁碼 if page_end >= total_page: page_end = total_page page_start = total_page - self.max_page + 1 self.page_start = page_start self.page_end = page_end @property def start(self): return self.data_start @property def end (self): return self.data_end def page_html(self): # 自己拼接分頁的HTML代碼 html_str_list = [] # # 加上首頁 html_str_list.append( '<li><a href="{}?page=1">首頁</a></li>' .format(self.url_prefix)) # 斷一下 如果是第一頁,就沒有上一頁 if self.page_num <= 1: html_str_list.append( '<li class="disabled"><a href="#"><span aria-hidden="true">«</span></a></li>' ) else : # 不是第一頁,就加一個上一頁的標簽 html_str_list.append( '<li><a href="{}?page={}"><span aria-hidden="true">«</span></a></li>' .format(self.url_prefix, self.page_num - 1)) for i in range(self.page_start, self.page_end + 1): # 如果是當前頁就加一個active樣式類 if i == self.page_num: tmp = '<li class="active"><a href="{0}?page={1}">{1}</a></li>' .format(self.url_prefix, i) else : tmp = '<li><a href="{0}?page={1}">{1}</a></li>' .format(self.url_prefix, i) html_str_list.append(tmp) # 判斷,如果是最后一頁,就沒有下一頁 if self.page_num >= self.total_page: html_str_list.append( '<li class="disabled"><a href="#"><span aria-hidden="true">»</span></a></li>' ) else : # 不是最后一頁, 就加一個下一頁標簽 html_str_list.append( '<li><a href="{}?page={}"><span aria-hidden="true">»</span></a></li>' .format(self.url_prefix, self.page_num + 1)) # 加上尾頁 html_str_list.append( '<li><a href="{}?page={}">尾頁</a></li>' .format(self.url_prefix, self.total_page)) page_html = "" .join(html_str_list) return page_html |
封裝版使用指南
1
2
3
4
5
6
7
8
|
def publisher(request): page_num = request.GET.get( "page" ) total_count = models.Publisher.objects.all(). count () # 調用封裝的Page類,傳入相應的參數 page_obj = Page(page_num, total_count, url_prefix= "/publisher/" , per_page=10, max_page=11) all_publisher = models.Publisher.objects.all()[page_obj.start:page_obj. end ] page_html = page_obj.page_html() return render(request, "publisher.html" , { "publisher" : all_publisher, "page_html" : page_html}) |
封裝版對應的HTML參考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title >圖書列表</ title > < link rel = "stylesheet" href = "/static/bootstrap/css/bootstrap.min.css" > </ head > < body > < div class = "container" > < table class = "table table-bordered" > < thead > < tr > < td >序列號</ td > < td >ID值</ td > < td >出版社</ td > < td >時間</ td > </ tr > </ thead > < tbody > {% for pub in publisher %} < tr > < th >{{ forloop.counter }}</ th > < th >{{ pub.id }}</ th > < th >{{ pub.name }}</ th > < th >{{ pub.date }}</ th > </ tr > {% endfor %} </ tbody > </ table > < nav aria-label = "Page navigation" > < ul class = "pagination" > {{ page_html|safe }} </ ul > </ nav > </ div > </ body > </ html > |
效果圖如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/yanjieli/archive/2018/09/24/9507272.html