如何在django中實現(xiàn)分頁功能
1.在html頁面中導入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.寫一個展示分頁的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獲取當前頁數(shù),定義每頁展示的數(shù)量,和返回數(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上獲取當前請求的頁數(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) # 當前請求的頁數(shù) news = page.get_page(current_page) # 顯示的總頁數(shù) total_page = page.num_pagesreturn render(request,’app1/news.html’,locals())
django中的分頁功能已經完成,效果圖如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:

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