from django.shortcuts import render# create your views here.
from .models import book
from django.core.paginator import paginator,emptypage
def index(request):
"""# 批量匯入資料
book_list=
for i in range(100):
book = book.objects.create(title="book_%s" % i, price=i*i)
book.objects.bulk_create(book_list)
"""# 分頁器的使用:
book_list = book.objects.all()
paginator = paginator(book_list, 8) # 第二個引數設定每頁顯示的條數
print(paginator.count) # 資料總條數 100
print(paginator.num_pages) # 總頁數 13
print(paginator.page_range) # 頁碼的列表 range(1, 14)
page1 = paginator.page(1) # 第一頁的page物件
# 顯示某一頁具體資料的兩種方式:
# 方式一:
print("object_list", page1.object_list) # object_list , ,...
# 方式二:
for i in page1:
print(i)
"""輸出
book object (1)
book object (2)
book object (3)
book object (4)
book object (5)
book object (6)
book object (7)
book object (8)
"""page2 = paginator.page(2)
current_page_num = int(request.get.get("page", 1))
if paginator.num_pages > 11:
if current_page_num-5 < 1:
# 當前頁碼-5小於1時,預設顯示前11個頁碼
page_range = range(1, 12)
elif current_page_num + 5 > paginator.num_pages:
# 當前頁碼+5大於總頁數時,預設顯示後11個
page_range = range(paginator.num_pages-10, paginator.num_pages+1)
else:
# 顯示當前頁碼和左五右五共11個頁碼
page_range = range(current_page_num-5, current_page_num+6)
else:
# 不超過11個頁碼的情況,顯示所有頁碼
page_range = paginator.page_range
# 超出頁碼範圍或小於1的頁碼,會丟擲emptypage錯誤,做如下處理:
try:
current_page_num = int(request.get.get("page", 1)) # /?page=1 get取不到預設給它預設值:1
current_page = paginator.page(current_page_num)
print("object_list", current_page.object_list)
for i in current_page:
print(i)
except emptypage as e:
current_page = paginator.page(1) # 固定在出現emptypage時顯示首頁
return render(request, "index.html", locals())
注意:1、批量生成資料,新增到陣列中,再一次性寫入資料庫表中:book.objects.bulk_create(book_list)
2、分頁器物件生成後,.count檢視資料總條數,.num_pages顯示總頁數,.page_range頁碼列表。
3、顯示某一頁的具體資料有兩種方式:
# 方式一:print("object_list", page1.object_list) # object_list , ,...
# 方式二:
for i in page1:
print(i)
"""輸出
book object (1)
book object (2)
book object (3)
book object (4)
book object (5)
book object (6)
book object (7)
book object (8)
"""
page2 = paginator.page(2)
5、得到當前頁碼,且設定預設頁碼為1;根據當前頁碼得到當前頁物件。
current_page_num = int(request.get.get("page", 1)) # /?page=1 get取不到預設給它預設值:1current_page = paginator.page(current_page_num)
6、針對超出頁碼範圍的頁碼,會丟擲emptypage錯誤,可以做如下處理:
# 超出頁碼範圍或小於1的頁碼,會丟擲emptypage錯誤,做如下處理:try:
current_page_num = int(request.get.get("page", 1)) # /?page=1 get取不到預設給它預設值:1
current_page = paginator.page(current_page_num)
print("object_list", current_page.object_list)
for i in current_page:
print(i)
except emptypage as e:
current_page = paginator.page(1) # 固定在出現emptypage時顯示首頁
7、頁面具備翻頁功能,設定同時最多只能顯示11條,處理如下:
current_page_num = int(request.get.get("page", 1))if paginator.num_pages > 11:
if current_page_num-5 < 1:
# 當前頁碼-5小於1時,預設顯示前11個頁碼
page_range = range(1, 12)
elif current_page_num + 5 > paginator.num_pages:
# 當前頁碼+5大於總頁數時,預設顯示後11個
page_range = range(paginator.num_pages-10, paginator.num_pages+1)
else:
# 顯示當前頁碼和左五右五共11個頁碼
page_range = range(current_page_num-5, current_page_num+6)
else:
# 不超過11個頁碼的情況,顯示所有頁碼
page_range = paginator.page_range
注意:1、讀取當前頁碼資料生成標籤展示在頁面上:
2、引入bootstrap
body部分:
3、生成頁面頁碼標籤,路徑前面不變的情況下可以從?開始配置:
}}
(1)第一種方式是在當前頁碼做加減法:};}
(2)第二種方式是使用page物件自帶的方法:
Django元件 分頁器
批量匯入資料 booklist for i in range 100 book.objects.bulk create booklist 分頁器的使用 book list book.objects.all paginator paginator book list,10 print count pa...
Django元件 分頁器
批量匯入資料 booklist for i in range 100 book.objects.bulk create booklist 分頁器的使用 book list book.objects.all paginator paginator book list,10 print count pa...
Django元件之 分頁器
django自帶分頁器 paginator 用起來非常方便。怕自己寫的出bug,可以使用現成的 批量匯入資料 booklist for i in range 100 book.objects.bulk create booklist 分頁器的使用 book list book.objects.all...