分頁功能的實現
直接上程式碼吧:
基礎版
import math
class Page:
def __init__(self, total_num, page_num, every_page_num=10, show_page_num=9):
"""
:param total_num: 資料庫獲取到的資料的總數
:param page_num: 前端獲取的頁數
:param every_page_num: 自定義每頁顯示條數
:param show_page_num: 顯示分頁數目
"""
self.total_num = total_num
self.page_num = page_num
self.every_page_num = every_page_num
self.show_page_num = show_page_num
self.page_tags_html = ''
self.page_start_num = 0
self.page_end_num = 0
self.start_page_num = 1
self.end_page_num = 1
try:
self.page_num = int(self.page_num)
except:
self.page_num = 1
# 通過向上取整計算出資料的總頁數
self.total_page_num = math.ceil(self.total_num / self.every_page_num)
if self.total_page_num:
# 判斷從前端得到的頁碼數是否小於1,小於1就重置為1
if self.page_num < 1:
self.page_num = 1
# 判斷從前端得到的頁碼數是否大於總頁碼數,大於就重置為最大頁碼數
elif self.page_num > self.total_page_num:
self.page_num = self.total_page_num
# 當前頁的開始資料
self.page_start_num = (self.page_num - 1) * self.every_page_num
# 當前頁的結束資料
self.page_end_num = (self.page_num) * self.every_page_num
# 判斷總頁碼數和要展示的頁數,如果總頁碼數大於要展示的頁碼數就展示對應的頁數否則有多少頁就展示多少頁
if self.total_page_num > self.show_page_num:
self.start_page_num = self.page_num - (self.show_page_num // 2)
self.end_page_num = self.page_num + (self.show_page_num // 2)
if self.start_page_num < 1:
self.start_page_num = 1
self.end_page_num = self.show_page_num
if self.end_page_num > self.total_page_num:
self.end_page_num = self.total_page_num
self.start_page_num = (self.total_page_num - self.show_page_num)
else:
self.start_page_num = 1
self.end_page_num = self.total_page_num
@property
def start(self):
return self.page_start_num
@property
def end(self):
return self.page_end_num
@property
def html(self):
pre_page_num = self.page_num - 1 if self.page_num > 1 else self.page_num
next_page_num = self.page_num + 1 if self.page_num < self.total_page_num else self.total_page_num
The_first_page = '<li><a href="?page=1">首頁</a>' \
f'</li><li><a href="?page={pre_page_num}">«</a></li>'
The_last_page = f'<li><a href="?page={next_page_num}">»</a></li>' \
f'<li><a href="?page={self.total_page_num}">尾頁</a></li></ul></nav>'
self.page_tags_html = f'<nav aria-label="Page navigation"> <ul class="pagination">{The_first_page}'
for i in range(self.start_page_num, self.end_page_num + 1):
if i == self.page_num:
self.page_tags_html += f'<li class="active"><a href="?page={i}">{i}</a></li>'
else:
self.page_tags_html += f'<li><a href="?page={i}">{i}</a></li>'
self.page_tags_html += The_last_page
return self.page_tags_html
高階版(增加了url引數的保留功能)
import math
import copy
class Page:
def __init__(self, total_num, page_num, request, every_page_num=10, show_page_num=9):
"""
:param total_num: 資料庫獲取到的資料的總數
:param request: 當前url請求資料,用於獲取url引數
:param page_num: 前端獲取的頁數
:param every_page_num: 自定義每頁顯示條數
:param show_page_num: 顯示分頁數目
"""
self.total_num = total_num
self.page_num = page_num
self.every_page_num = every_page_num
self.show_page_num = show_page_num
self.page_tags_html = ''
self.page_start_num = 0
self.page_end_num = 0
self.start_page_num = 1
self.end_page_num = 1
try:
self.page_num = int(self.page_num)
except:
self.page_num = 1
# 獲取當前url請求中的引數
params = copy.deepcopy(request.GET)
self.params = params
# 通過向上取整計算出資料的總頁數
self.total_page_num = math.ceil(self.total_num / self.every_page_num)
if self.total_page_num:
# 判斷從前端得到的頁碼數是否小於1,小於1就重置為1
if self.page_num < 1:
self.page_num = 1
# 判斷從前端得到的頁碼數是否大於總頁碼數,大於就重置為最大頁碼數
elif self.page_num > self.total_page_num:
self.page_num = self.total_page_num
# 當前頁的開始資料
self.page_start_num = (self.page_num - 1) * self.every_page_num
# 當前頁的結束資料
self.page_end_num = (self.page_num) * self.every_page_num
# 判斷總頁碼數和要展示的頁數,如果總頁碼數大於要展示的頁碼數就展示對應的頁數否則有多少頁就展示多少頁
if self.total_page_num > self.show_page_num:
self.start_page_num = self.page_num - (self.show_page_num // 2)
self.end_page_num = self.page_num + (self.show_page_num // 2)
if self.start_page_num < 1:
self.start_page_num = 1
self.end_page_num = self.show_page_num
if self.end_page_num > self.total_page_num:
self.end_page_num = self.total_page_num
self.start_page_num = (self.total_page_num - self.show_page_num)
else:
self.start_page_num = 1
self.end_page_num = self.total_page_num
@property
def start(self):
return self.page_start_num
@property
def end(self):
return self.page_end_num
@property
def html(self):
pre_page_num = self.page_num - 1 if self.page_num > 1 else self.page_num
next_page_num = self.page_num + 1 if self.page_num < self.total_page_num else self.total_page_num
self.params["page"] = 1
The_home_page_html = f'<li><a href="?{self.params.urlencode()}">首頁</a></li>'
self.params["page"] = pre_page_num
pre_page_num_html = f'<li><a href="?{self.params.urlencode()}">«</a></li>'
The_first_page_html = The_home_page_html + pre_page_num_html
self.params["page"] = next_page_num
next_page_num_html = f'<li><a href="?{self.params.urlencode()}">»</a></li>'
self.params["page"] = self.total_page_num
The_end_page_html = f'<li><a href="?{self.params.urlencode()}">尾頁</a></li></ul></nav>'
The_last_page_html = next_page_num_html + The_end_page_html
self.page_tags_html = f'<nav aria-label="Page navigation"> <ul class="pagination">{The_first_page_html}'
for i in range(self.start_page_num, self.end_page_num + 1):
self.params["page"] = i
if i == self.page_num:
self.page_tags_html += f'<li class="active"><a href="?{self.params.urlencode()}">{i}</a></li>'
else:
self.page_tags_html += f'<li><a href="?{self.params.urlencode()}">{i}</a></li>'
self.page_tags_html += The_last_page_html
return self.page_tags_html
分頁的使用:
class Home(views.View):
def get(self, request):
article_list = models.Article.objects.all()
totol_num = article_list.count()
page_num = request.GET.get("page", 1)
# 分頁例項化傳參
page_obj = Page(total_num=totol_num, page_num=page_num, every_page_num=9)
# 根據分頁拿到的start和end切片得到所要的資料
data = article_list[page_obj.start:page_obj.end]
# 呼叫html方法得到分頁的html程式碼片段
page_html = page_obj.html
return render(request, 'home.html', {'article_list': data, 'page_html': page_html})
ps:在python指令碼中呼叫django環境實現資料庫批量新增資料操作
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fenye.settings")
if __name__ == "__main__":
import django
django.setup()
from app01 import models
def create():
obj_list = [models.Book(name = f"pyhton{i}") for i in range(160)]
models.Book.objects.bulk_create(obj_list)
create()
#
# def delete():
# models.Book.objects.all().delete()
# delete()
相關文章
- web 實現分頁列印功能Web
- 使用 jQuery 實現分頁功能jQuery
- DjangoRestFramework 實現分頁功能與搜尋功能DjangoRESTFramework
- 文章內容分頁功能實現
- Vue + element.ui table 分頁功能+搜尋功能的實現VueUI
- Vue + Element UI + Lumen 實現通用表格功能 - 分頁VueUI
- MyBatis實現分頁的方式MyBatis
- elementUI實現分頁UI
- vue + axios 實現分頁引數傳遞,高階搜尋功能實現VueiOS
- Spring Boot入門系列(十六)使用pagehelper實現分頁功能Spring Boot
- DjangoRestFramework框架三種分頁功能的實現 - 在DjangoStarter專案模板中封裝DjangoRESTFramework框架封裝
- 關於黑馬旅遊網的實現 --- 分頁查詢功能,點選分頁碼不顯示資料
- web端 網頁端分享功能的實現Web網頁
- 實現一個頁面鎖屏的功能
- Java實現後端分頁Java後端
- PHP 使用 Redis 實現分頁PHPRedis
- 47.DRF實現分頁
- indexdb實現分頁查詢Index
- 使用LayUI實現AJAX分頁UI
- Django 前端BootCSS 實現分頁Django前端bootCSS
- 建立一個SpringBoot專案,實現簡單的CRUD功能和分頁查詢Spring Boot
- iOS 類知乎”分頁”效果的實現?iOS
- Jquery 實現頁面倒數計時的功能jQuery
- 原生JavaScript實現頁面回到頂部的功能JavaScript
- 分頁實現前臺後臺不同效果,分頁類引入
- 分頁實現前五後五ajax區域性重新整理方式分頁實現
- uni-app之實現分頁APP
- SpringBoot 整合Mybatis + PageHelper 實現分頁Spring BootMyBatis
- 用AutoLayout實現分頁滾動
- Python | 實現pdf檔案分頁Python
- js實現網頁端錄音功能JS網頁
- SpringMVC+Spring Data JPA +Bootstrap 分頁實現和模糊查詢分頁SpringMVCboot
- 【MyBatis框架】MyBatis實現物理分頁和邏輯(記憶體)分頁MyBatis框架記憶體
- vue+Element-ui實現分頁效果VueUI
- SSM框架實現分頁查詢例子SSM框架
- django 網站實現簡單分頁Django網站
- Mybatis 分頁:Pagehelper + 攔截器實現MyBatis
- 教你如何使用MvcPager實現分頁效果MVC