Django中分頁
py文件代碼
"""
自定義分頁組件
能夠返回分頁的數據和分頁的HTML代碼
"""
from django.http import QueryDict
class Pagination(object):
def __init__(self, current_page, total_count, url_prefix, query_dict=QueryDict(mutable=True), per_page=10,
show_page=9):
"""
初始化分分頁器
:param url_prefix: a標籤的URL前綴
:param current_page: 當前頁碼數
:param total_count: 數據總數
:param query_dict: 空的QueryDict()對象,而且是可修改的
:param per_page: 每一頁顯示多少數據, 默認值是10
:param show_page: 頁面顯示的頁碼數, 默認值是9
"""
# 0.分頁的URL前綴
self.url_prefix = url_prefix
self.query_dict = query_dict
# 1. 每一頁顯示10條數據
self.per_page = per_page
assert per_page > 0
# 2. 計算須要多少頁
total_page, more = divmod(total_count, per_page)
if more:
total_page += 1
self.total_page = total_page
# 3. 當前頁碼
try:
current_page = int(current_page)
except Exception as e:
current_page = 1
current_page = total_page if current_page > total_page else current_page
# 頁碼必須是大於0的數
if current_page < 1:
current_page = 1
self.current_page = current_page
# 4. 頁面最多顯示的頁碼數
self.show_page = show_page
# 5. 最多顯示頁碼的一半
self.half_show_page = self.show_page // 2
@property
def start(self):
# 數據切片的開始位置
return self.per_page * (self.current_page - 1)
@property
def end(self):
# 數據切片的結束爲止
return self.current_page * self.per_page
# 定義一個返回HTML代碼的方法
def page_html(self):
# 若是總頁碼數小於最大要顯示的頁碼數
if self.total_page < self.show_page:
show_page_start = 1
show_page_end = self.total_page
# 左邊越界
elif self.current_page - self.half_show_page < 1:
show_page_start = 1
show_page_end = self.show_page
# 右邊越界
elif self.current_page + self.half_show_page > self.total_page:
show_page_end = self.total_page
show_page_start = self.total_page - self.show_page + 1
else:
show_page_start = self.current_page - self.half_show_page
# 頁面顯示頁碼的結束
show_page_end = self.current_page + self.half_show_page
# 生成分頁的HTML代碼
page_list = []
# 添加分頁代碼的前綴
page_list.append('<nav aria-label="Page navigation"><ul class="pagination">')
# 添加首頁
self.query_dict['page'] = 1
page_list.append(f'<li><a href="{self.url_prefix}?{self.query_dict.urlencode()}">首頁</a></li>')
# 添加上一頁
if self.current_page - 1 < 1: # 已經到頭啦,不讓點上一頁啦
page_list.append(
'<li class="disabled"><a href="" aria-label="Previous"><span aria-hidden="true">«</span></a></li>')
else:
self.query_dict['page'] = self.current_page - 1
page_list.append(
f'<li><a href="{self.url_prefix}?{self.query_dict.urlencode()}" aria-label="Previous">'
f'<span aria-hidden="true">«</span></a></li>'
)
for i in range(show_page_start, show_page_end + 1):
self.query_dict['page'] = i
if i == self.current_page:
s = f'<li class="active"><a href="{self.url_prefix}?{self.query_dict.urlencode()}">{i}</a></li>'
else:
s = f'<li><a href="{self.url_prefix}?{self.query_dict.urlencode()}">{i}</a></li>'
page_list.append(s)
# 添加下一頁
if self.current_page + 1 > self.total_page:
page_list.append(
'<li class="disabled"><a href="" aria-label="Next"><span aria-hidden="true">»</span></a></li>')
else:
self.query_dict['page'] = self.current_page + 1
page_list.append(
f'<li><a href="{self.url_prefix}?{self.query_dict.urlencode()}" aria-label="Next">'
f'<span aria-hidden="true">»</span></a></li>'
)
# 添加尾頁
self.query_dict['page'] = self.total_page
page_list.append(f'<li><a href="{self.url_prefix}?{self.query_dict.urlencode()}">尾頁</a></li>')
# 添加分頁代碼的後綴
page_list.append('</ul></nav>')
page_html = ''.join(page_list)
return page_html
實例化對象
def dashboard(request):
# 獲取當前網頁路徑
url_prefix = request.path_info
# 獲取當前頁碼數
current_page = request.GET.get('page', 1)
# 獲取全部客戶的信息
customer_list = Customer.objects.all()
# 實例分頁代碼
pagination = Pagination(current_page=current_page, total_count=customer_list.count(), url_prefix=url_prefix, per_page=8)
# 獲取當前頁的數據
data = customer_list[pagination.start:pagination.end]
page_html = pagination.page_html()
return render(request, "Dashboard.html", {'customer_list': data, "page_html": page_html})
前端代碼
<div>
{{ page_html|safe }}
</div>
使用django腳本去建立數據
- 批量建立數據
- 使用Django腳本去建立數據
- bulk_create()
- 分頁
- 純Python基礎寫的,
- django裏面獲取當前url
- request.path_info 獲取當前路徑
- request.get_full_path 獲取全路徑
實際樣式