如何在django中實(shí)現(xiàn)分頁功能
1.在html頁面中導(dǎo)入js文件和css文件
<link rel='stylesheet' href='http://www.leifengta.com.cn/static/css/jquery.pagination.css' rel='external nofollow' ><script type='text/javascript' src='http://www.leifengta.com.cn/static/js/jquery-1.12.4.min.js'></script><script type='text/javascript' src='http://www.leifengta.com.cn/static/js/jquery.pagination.min.js'></script>
2.寫一個(gè)展示分頁的div容器
<div class='page'></div>
3.前端分頁邏輯
<script> $(function(){ $('#pagination').pagination({ currentPage:{{current_page}}, totalPage:{{total_page}}, callback:function(current){ window.location.href = ’?page=’+current} });});</script>
4.django獲取當(dāng)前頁數(shù),定義每頁展示的數(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)求的頁數(shù) p = request.GET.get(’page’,1) current_page = int(p) # 每頁顯示的條數(shù) page_count = 1 # 顯示數(shù)據(jù)庫數(shù)據(jù),并且規(guī)定每頁顯示多少條數(shù)據(jù) page = Paginator(news,page_count) # 當(dāng)前請(qǐng)求的頁數(shù) news = page.get_page(current_page) # 顯示的總頁數(shù) total_page = page.num_pagesreturn render(request,’app1/news.html’,locals())
django中的分頁功能已經(jīng)完成,效果圖如下:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于android studio的layout的xml文件的創(chuàng)建方式2. 詳解Android studio 動(dòng)態(tài)fragment的用法3. 什么是python的自省4. 圖文詳解vue中proto文件的函數(shù)調(diào)用5. Android如何加載Base64編碼格式圖片6. 解決Android studio xml界面無法預(yù)覽問題7. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟8. node以及npm版本不對(duì)應(yīng)出錯(cuò)的完美解決方法9. input submit、button和回車鍵提交數(shù)據(jù)詳解10. ASP.NET MVC實(shí)現(xiàn)區(qū)域或城市選擇

網(wǎng)公網(wǎng)安備