分頁功能在每個網站都是必要的,對於分頁來說,其實就是根據使用者的輸入計算出應該顯示在頁面上的資料在資料庫表中的起始位置。
確定分頁需求:
1. 每页显示的数据条数 2. 每页显示页号链接数 3. 上一页和下一页 4. 首页和末页
效果圖:
首先,利用django內建的分頁功能,寫分頁類別:
1 from django.core.paginator import Paginator, Page # 导入django分页模块 2 3 4 class PageInfo(object): 5 def __init__(self, current_page, all_count, base_url, per_page=10, show_page=11): 6 """ 7 8 :param current_page: 当前页 9 :param all_count: 总页数10 :param base_url: 模板11 :param per_page: 每页显示数据条数12 :param show_page: 显示链接页个数13 """14 #若url错误,默认显示第一页(错误类型可能为:空页面编号,非整数型页面编号)15 try:16 self.current_page = int(current_page)17 except Exception as e:18 self.current_page = 119 20 #根据数据库信息条数得出总页数 21 a, b = divmod(all_count, per_page)22 if b:23 a += 124 self.all_page = a 25 26 self.base_url = base_url27 self.per_page = per_page28 self.show_page = show_page29 30 #当前页起始数据id31 def start_data(self): 32 return (self.current_page - 1) * self.per_page33 34 #当前页结束数据id35 def end_data(self): 36 return self.current_page * self.per_page37 38 #动态生成前端html39 def pager(self):40 page_list = []41 half = int((self.show_page - 1)/2)42 #如果:总页数 show_page47 else:48 #如果:current_page - half 总页数,默认显示页数范围为:总页数 - show_page ~ 总页数54 if self.current_page + half > self.all_page:55 end_page = self.all_page + 156 start_page = end_page - self.show_page57 else:58 start_page = self.current_page - half59 end_page = self.current_page + half + 160 61 #首页62 first_page = "
然後,在views中寫入方法(此處寫在app01中):
1 from utils.pagnition import PageInfo # 从文件中导入上步自定义的分页模块2 3 def custom(request):4 all_count = models.UserInfo.objects.all().count() # 获取要显示数据库的总数据条数5 page_info = PageInfo(request.GET.get('page'), all_count, '/custom.html/',) # 生成分页对象6 user_list = models.UserInfo.objects.all()[page_info.start_data():page_info.end_data()] # 利用分页对象获取当前页显示数据7 return render(request, 'custom.html', {'user_list': user_list, 'page_info': page_info}) # 模板渲染
然後,在templates目錄下寫「custom.html」檔案:
1 nbsp;html> 2 3 4 <meta> 5 <title>customers</title> 6 {# 引入bootstrap样式#} 7 <link> 8 9 10 <h1 id="customers">customers</h1>11 {#当前页显示的数据#}12
- 13 {% for row in user_list %}14
- {{ row.name }} 15 {% endfor %}16
最後,新增url關係(urls.py):
1 from django.conf.urls import url2 from django.contrib import admin3 from app01 import views as app01_views4 5 urlpatterns = [6 url(r'^custom.html/$', app01_views.custom),7 ]
至此,就完成了利用django的分頁功能自訂分頁模組,可以應用在不同的商業頁面上。
參考資料:
#1. Python之路【第十七篇】:Django【進階篇 】
以上是Django介紹之分頁實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境