如何在django中實(shí)現(xiàn)分頁(yè)功能
1.在html頁(yè)面中導(dǎo)入js文件和css文件
<link rel='stylesheet' href='http://m.lshqa.cn/static/css/jquery.pagination.css' rel='external nofollow' ><script type='text/javascript' src='http://m.lshqa.cn/static/js/jquery-1.12.4.min.js'></script><script type='text/javascript' src='http://m.lshqa.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. Intellij IDEA連接Navicat數(shù)據(jù)庫(kù)的方法2. 關(guān)于Mysql-connector-java驅(qū)動(dòng)版本問(wèn)題總結(jié)3. 使用css實(shí)現(xiàn)全兼容tooltip提示框4. CSS自定義滾動(dòng)條樣式案例詳解5. 詳解JavaScript作用域、作用域鏈和閉包的用法6. python 批量下載bilibili視頻的gui程序7. python中HTMLParser模塊知識(shí)點(diǎn)總結(jié)8. 通過(guò)工廠模式返回Spring Bean方法解析9. Ajax提交post請(qǐng)求案例分析10. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)
