Django自定義分頁效果
分頁功能在每個網(wǎng)站都是必要的,對于分頁來說,其實就是根據(jù)用戶的輸入計算出應該顯示在頁面上的數(shù)據(jù)在數(shù)據(jù)庫表中的起始位置。
確定分頁需求:
1. 每頁顯示的數(shù)據(jù)條數(shù)
2. 每頁顯示頁號鏈接數(shù)
3. 上一頁和下一頁
4. 首頁和末頁
效果圖:
首先,利用django內(nèi)置的分頁功能,寫分頁類:
from django.core.paginator import Paginator, Page # 導入django分頁模塊 class PageInfo(object): def __init__(self, current_page, all_count, base_url, per_page=10, show_page=11): """ :param current_page: 當前頁 :param all_count: 總頁數(shù) :param base_url: 模板 :param per_page: 每頁顯示數(shù)據(jù)條數(shù) :param show_page: 顯示鏈接頁個數(shù) """ #若url錯誤,默認顯示第一頁(錯誤類型可能為:空頁面編號,非整數(shù)型頁面編號) try: self.current_page = int(current_page) except Exception as e: self.current_page = 1 #根據(jù)數(shù)據(jù)庫信息條數(shù)得出總頁數(shù) a, b = divmod(all_count, per_page) if b: a += 1 self.all_page = a self.base_url = base_url self.per_page = per_page self.show_page = show_page #當前頁起始數(shù)據(jù)id def start_data(self): return (self.current_page - 1) * self.per_page #當前頁結(jié)束數(shù)據(jù)id def end_data(self): return self.current_page * self.per_page #動態(tài)生成前端html def pager(self): page_list = [] half = int((self.show_page - 1)/2) #如果:總頁數(shù) < show_page,默認顯示頁數(shù)范圍為: 1~總頁數(shù) if self.all_page < self.show_page: start_page = 1 end_page = self.all_page + 1 #如果:總頁數(shù) > show_page else: #如果:current_page - half <= 0,默認顯示頁數(shù)范圍為:1~show_page if self.current_page <= half: start_page = 1 end_page = self.show_page + 1 else: #如果:current_page + half >總頁數(shù),默認顯示頁數(shù)范圍為:總頁數(shù) - show_page ~ 總頁數(shù) if self.current_page + half > self.all_page: end_page = self.all_page + 1 start_page = end_page - self.show_page else: start_page = self.current_page - half end_page = self.current_page + half + 1 #首頁 first_page = "<li><a href='%s?page=%s'>首頁</a></li>" %(self.base_url, 1) page_list.append(first_page) #上一頁(若當前頁等于第一頁,則上一頁無鏈接,否則鏈接為當前頁減1) if self.current_page <= 1: prev_page = "<li><a href='#'>上一頁</a></li>" else: prev_page = "<li><a href='%s?page=%s'>上一頁</a></li>" %(self.base_url, self.current_page-1) page_list.append(prev_page) #動態(tài)生成中間頁數(shù)鏈接 for i in range(start_page, end_page): if i == self.current_page: temp = "<li class='active'><a href='%s?page=%s'>%s</a></li>" %(self.base_url, i, i) else: temp = "<li><a href='%s?page=%s'>%s</a></li>" % (self.base_url, i, i) page_list.append(temp) #下一頁(若當前頁等于最后頁,則下一頁無鏈接,否則鏈接為當前頁加1) if self.current_page >= self.all_page: next_page = "<li><a href='#'>下一頁</a></li>" else: next_page = "<li><a href='%s?page=%s'>下一頁</a></li>" %(self.base_url, self.current_page+1) page_list.append(next_page) #末頁(若總頁數(shù)只有一頁,則無末頁標簽) if self.all_page > 1: last_page = "<li><a href='%s?page=%s'>末頁</a></li>" % (self.base_url, self.all_page) page_list.append(last_page) return ''.join(page_list)
然后,在views中寫方法(此處寫在app01中):
from utils.pagnition import PageInfo # 從文件中導入上步自定義的分頁模塊 def custom(request): all_count = models.UserInfo.objects.all().count() # 獲取要顯示數(shù)據(jù)庫的總數(shù)據(jù)條數(shù) page_info = PageInfo(request.GET.get('page'), all_count, '/custom.html/',) # 生成分頁對象 user_list = models.UserInfo.objects.all()[page_info.start_data():page_info.end_data()] # 利用分頁對象獲取當前頁顯示數(shù)據(jù) return render(request, 'custom.html', {'user_list': user_list, 'page_info': page_info}) # 模板渲染
然后,在templates目錄下寫“custom.html"文件:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>customers</title> {# 引入bootstrap樣式#} <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css"> </head> <body> <h1>customers</h1> {#當前頁顯示的數(shù)據(jù)#} <ul> {% for row in user_list %} <li>{{ row.name }}</li> {% endfor %} </ul> {#分頁#} <nav aria-label="Page navigation"> <ul class="pagination"> {# 傳入page_info.pager#} {{ page_info.pager|safe }} </ul> </nav> </body> </html>
最后,新增url關系(urls.py):
from django.conf.urls import url from django.contrib import admin from app01 import views as app01_views urlpatterns = [ url(r'^custom.html/$', app01_views.custom), ]
至此,就完成了利用django的分頁功能自定義分頁模塊,可以應用在不同的業(yè)務頁面上。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python?matplotlib.pyplot.hist()繪制直方圖的方法實例
直方圖(Histogram)又稱質(zhì)量分布圖,是一種統(tǒng)計報告圖,由一系列高度不等的縱向條紋或線段表示數(shù)據(jù)分布的情況,一般用橫軸表示數(shù)據(jù)類型,縱軸表示分布情況,下面這篇文章主要給大家介紹了關于Python?matplotlib.pyplot.hist()繪制直方圖的相關資料,需要的朋友可以參考下2022-06-06淺談Pandas:Series和DataFrame間的算術元素
今天小編就為大家分享一篇淺談Pandas:Series和DataFrame間的算術元素,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12python數(shù)據(jù)庫開發(fā)之MongoDB安裝及Python3操作MongoDB數(shù)據(jù)庫詳細方法與實例
這篇文章主要介紹了python數(shù)據(jù)庫開發(fā)之MongoDB安裝及Python3操作MongoDB數(shù)據(jù)庫詳細方法與實例,需要的朋友可以參考下2020-03-03