如何在django中實(shí)現(xiàn)分頁(yè)功能
1.在html頁(yè)面中導(dǎo)入js文件和css文件
<link rel='stylesheet' href='http://m.cgvv.com.cn/static/css/jquery.pagination.css' rel='external nofollow' ><script type='text/javascript' src='http://m.cgvv.com.cn/static/js/jquery-1.12.4.min.js'></script><script type='text/javascript' src='http://m.cgvv.com.cn/static/js/jquery.pagination.min.js'></script>
2.寫(xiě)一個(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. 推薦一個(gè)好看Table表格的css樣式代碼詳解2. 基于Surprise協(xié)同過(guò)濾實(shí)現(xiàn)短視頻推薦方法示例3. 不使用XMLHttpRequest對(duì)象實(shí)現(xiàn)Ajax效果的方法小結(jié)4. vue-electron中修改表格內(nèi)容并修改樣式5. 微信小程序?qū)崿F(xiàn)商品分類頁(yè)過(guò)程結(jié)束6. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式7. ASP新手必備的基礎(chǔ)知識(shí)8. AJAX實(shí)現(xiàn)文件上傳功能報(bào)錯(cuò)Current request is not a multipart request詳解9. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總10. ASP常用日期格式化函數(shù) FormatDate()
