如何在django中實(shí)現(xiàn)分頁(yè)功能
1.在html頁(yè)面中導(dǎo)入js文件和css文件
<link rel='stylesheet' href='http://www.cgvv.com.cn/static/css/jquery.pagination.css' rel='external nofollow' ><script type='text/javascript' src='http://www.cgvv.com.cn/static/js/jquery-1.12.4.min.js'></script><script type='text/javascript' src='http://www.cgvv.com.cn/static/js/jquery.pagination.min.js'></script>
2.寫一個(gè)展示分頁(yè)的div容器
<div class='page'></div>
3.前端分頁(yè)邏輯
<script> $(function(){ $('#pagination').pagination({ currentPage:{{current_page}}, totalPage:{{total_page}}, callback:function(current){ window.location.href = ’?page=’+current} });});</script>
4.django獲取當(dāng)前頁(yè)數(shù),定義每頁(yè)展示的數(shù)量,和返回?cái)?shù)據(jù)等
from django.core.paginator import Paginatordef detail(request,id): category = models.Category.objects.all() news = models.News.objects.filter(cate=id).all() # 從url上獲取當(dāng)前請(qǐng)求的頁(yè)數(shù) p = request.GET.get(’page’,1) current_page = int(p) # 每頁(yè)顯示的條數(shù) page_count = 1 # 顯示數(shù)據(jù)庫(kù)數(shù)據(jù),并且規(guī)定每頁(yè)顯示多少條數(shù)據(jù) page = Paginator(news,page_count) # 當(dāng)前請(qǐng)求的頁(yè)數(shù) news = page.get_page(current_page) # 顯示的總頁(yè)數(shù) total_page = page.num_pagesreturn render(request,’app1/news.html’,locals())
django中的分頁(yè)功能已經(jīng)完成,效果圖如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python調(diào)用接口合并Excel表代碼實(shí)例2. 一文透徹詳解.NET框架類型系統(tǒng)設(shè)計(jì)要點(diǎn)3. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)4. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效5. .net如何優(yōu)雅的使用EFCore實(shí)例詳解6. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車7. 通過Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例8. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析9. Python快速將ppt制作成配音視頻課件的操作方法10. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖
