分頁器
1.分頁器
1.1 分頁推導
- 首先我們需要明確的時候
- get請求也是可以攜帶引數的
- 所以我們在朝後端傳送檢視資料的同時可以攜帶一個引數告訴後端我們想看第幾頁的資料
- 其次我們還需要知道一個點
- queryset物件是支援索引取值和切片操作的
- 但是不支援負數索引情況
def get_page_book(request):
book_queryset = models.Book.objects.all()
# 起始頁碼,步長,終止的頁碼
# 從前端傳入,book_page/?page_num=1/
# 獲取前端傳入的頁數,在後段定義好每一頁有多少條資料,切片切除指定資料
# 比如起始頁為1
page = 2
# 每頁5條資料
page_num = 5
# 起始條數是1
# 擷取條數是5
# 終止條數是6
# 【1:1+5】切片顧頭不顧尾
# 第二頁
# 起始條數是 (2-1)*5+1
# 擷取條數是5
# 終止條數是(2*5)+1
# 總頁數:資料量/每一頁的條數=整數+小數
# 整數 + 1
# start_num page end_num page_all
if page == 1:
start_num = 1
else:
# 第二頁
page_start = page - 1
start_num = page_start * page_num + 1
end_num = page * page_num + 1
query_sert_data = book_queryset[start_num:end_num]
return render(request, 'book.html', locals())
def get_page_book(request):
# 需要檢視第幾頁
page = request.GET.get('page', 1)
try:
page = int(page)
except Exception:
page = 1
# 每頁展示多少條
page_num = 10
# 起始位置
start_page = (page - 1) * page_num + 1
# 終止位置
end_page = page * page_num + 1
book_obj = models.Book.objects.all()
queryset = book_obj[start_page:end_page]
return render(request, 'book.html', locals())
1.2 分頁器元件
1.2.1 bootstrap元件
透過點選底部頁碼進行指定的分頁,無法展示全部資料
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="?page=1">1</a></li>
<li><a href="?page=2">2</a></li>
<li><a href="?page=3">3</a></li>
<li><a href="?page=4">4</a></li>
<li><a href="?page=5">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
1.3 動態計算頁數
1.3.1 內建方法之divmod
- 內建函式
divmod(x, y)
- 用於執行整數除法和取模運算,並返回一個包含商和餘數的元組。
- 引數x和y是兩個數字
- 以下是
divmod()
函式的使用示例:
result = divmod(9, 2)
print(result) # 輸出 (4, 1)
result = divmod(14, 3)
print(result) # 輸出 (4, 2)
# 餘數只要不是0就需要在第一個數字上加一
- 在第一個示例中,我們將9除以2,得到商4和餘數1。
- 在第二個示例中,我們將14除以3,得到商4和餘數2。
divmod()
函式對於需要同時獲得商和餘數的情況非常有用。
- 它可以用於計算進位制轉換、時間單位轉換等問題
def get_page_book(request):
book_obj = models.Book.objects.all()
# 需要檢視第幾頁
page = request.GET.get('page', 1)
try:
page = int(page)
except Exception:
page = 1
# 每頁展示多少條
page_num = 10
# 總資料量
all_page_count = book_obj.count()
# 計算一共需要多少頁
page_count, other = divmod(all_page_count, page_num)
if other:
page_count += 1
html_page = ""
for count in range(1, page_count + 1):
html_page += f'<li><a href="?page={count}">{count}</a></li>'
# 起始位置
start_page = (page - 1) * page_num + 1
# 終止位置
end_page = page * page_num + 1
queryset = book_obj[start_page:end_page]
return render(request, 'book.html', locals())
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="{% static 'jQuery/jquery.min.js' %}"></script>
<script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>
<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.min.css' %}">
</head>
<body>
{% for book in queryset %}
<p>{{ book.title }}</p>
{% endfor %}
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
{# 前段轉義 - 將後端的html頁面進行轉義,轉為前端頁碼/也可以後端做這件事 #}
{{ html_page|safe }}
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</body>
</html>
1.4 美化動態分頁器
def get_page_book(request):
book_obj = models.Book.objects.all()
# 需要檢視第幾頁
page = request.GET.get('page', 1)
try:
page = int(page)
except Exception:
page = 1
# 每頁展示多少條
page_num = 10
# 總資料量
all_page_count = book_obj.count()
# 宣告一個左側的頁數
left_menu_num = page
if page < 6:
page = 6
# 計算一共需要多少頁
page_count, other = divmod(all_page_count, page_num)
if other:
page_count += 1
html_page = ""
for count in range(page - 5, page + 6):
if left_menu_num == count:
# 第一頁高亮顯示
html_page += f'<li class="active"><a href="?page={count}" >{count}</a></li>'
elif count > page_count:
break
else:
html_page += f'<li><a href="?page={count}" >{count}</a></li>'
# 起始位置
start_page = (page - 1) * page_num + 1
1.5 封裝分頁器
1.5.1 自定義分頁器封裝程式碼
class Pagination(object):
def __init__(self, current_page, all_count, per_page_num=2, pager_count=11):
"""
封裝分頁相關資料
:param current_page: 當前頁
:param all_count: 資料庫中的資料總條數
:param per_page_num: 每頁顯示的資料條數
:param pager_count: 最多顯示的頁碼個數
"""
try:
current_page = int(current_page)
except Exception as e:
current_page = 1
if current_page < 1:
current_page = 1
self.current_page = current_page
self.all_count = all_count
self.per_page_num = per_page_num
# 總頁碼
all_pager, tmp = divmod(all_count, per_page_num)
if tmp:
all_pager += 1
self.all_pager = all_pager
self.pager_count = pager_count
self.pager_count_half = int((pager_count - 1) / 2)
@property
def start(self):
return (self.current_page - 1) * self.per_page_num
@property
def end(self):
return self.current_page * self.per_page_num
def page_html(self):
# 如果總頁碼 < 11個:
if self.all_pager <= self.pager_count:
pager_start = 1
pager_end = self.all_pager + 1
# 總頁碼 > 11
else:
# 當前頁如果<=頁面上最多顯示11/2個頁碼
if self.current_page <= self.pager_count_half:
pager_start = 1
pager_end = self.pager_count + 1
# 當前頁大於5
else:
# 頁碼翻到最後
if (self.current_page + self.pager_count_half) > self.all_pager:
pager_end = self.all_pager + 1
pager_start = self.all_pager - self.pager_count + 1
else:
pager_start = self.current_page - self.pager_count_half
pager_end = self.current_page + self.pager_count_half + 1
page_html_list = []
# 新增前面的nav和ul標籤
page_html_list.append('''
<nav aria-label='Page navigation>'
<ul class='pagination'>
''')
first_page = '<li><a href="?page=%s">首頁</a></li>' % (1)
page_html_list.append(first_page)
if self.current_page <= 1:
prev_page = '<li class="disabled"><a href="#">上一頁</a></li>'
else:
prev_page = '<li><a href="?page=%s">上一頁</a></li>' % (self.current_page - 1,)
page_html_list.append(prev_page)
for i in range(pager_start, pager_end):
if i == self.current_page:
temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i,)
else:
temp = '<li><a href="?page=%s">%s</a></li>' % (i, i,)
page_html_list.append(temp)
if self.current_page >= self.all_pager:
next_page = '<li class="disabled"><a href="#">下一頁</a></li>'
else:
next_page = '<li><a href="?page=%s">下一頁</a></li>' % (self.current_page + 1,)
page_html_list.append(next_page)
last_page = '<li><a href="?page=%s">尾頁</a></li>' % (self.all_pager,)
page_html_list.append(last_page)
# 尾部新增標籤
page_html_list.append('''
</nav>
</ul>
''')
return ''.join(page_html_list)
1.5.2 自定義分頁器使用示例
def get_book(request):
book_list = models.Book.objects.all()
current_page = request.GET.get("page",1)
all_count = book_list.count()
page_obj = Pagination(current_page=current_page,all_count=all_count,per_page_num=10)
page_queryset = book_list[page_obj.start:page_obj.end]
return render(request,'booklist.html',locals())
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
{% for book in page_queryset %}
<p>{{ book.title }}</p>
{% endfor %}
{{ page_obj.page_html|safe }}
</div>
</div>
</div>