django中如何實現分頁功能
阿新 • • 發佈:2019-04-24
效果 style detail script 並且 request model 數量 news
1.在html頁面中導入js文件和css文件
<link rel="stylesheet" href="../../../static/css/jquery.pagination.css"> <script type="text/javascript" src="../../../static/js/jquery-1.12.4.min.js"></script> <script type="text/javascript" src="../../../static/js/jquery.pagination.min.js"></script>
2.寫一個展示分頁的div容器
<div id="pagination" 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獲取當前頁數,定義每頁展示的數量,和返回數據等
from django.core.paginator import Paginator
def detail(request,id): category = models.Category.objects.all() news = models.News.objects.filter(cate=id).all() # 從url上獲取當前請求的頁數 p = request.GET.get(‘page‘,1) current_page = int(p) # 每頁顯示的條數 page_count = 1 # 顯示數據庫數據,並且規定每頁顯示多少條數據 page = Paginator(news,page_count) # 當前請求的頁數 news = page.get_page(current_page) # 顯示的總頁數 total_page = page.num_pages return render(request,‘app1/news.html‘,locals())
django中的分頁功能已經完成,效果圖如下:
django中如何實現分頁功能