您的位置:首頁技術文章
文章詳情頁

如何在django中實現分頁功能

瀏覽:114日期:2024-10-09 18:16:16

1.在html頁面中導入js文件和css文件

<link rel='stylesheet' href='http://www.wxshucaidpc.com/static/css/jquery.pagination.css' rel='external nofollow' ><script type='text/javascript' src='http://www.wxshucaidpc.com/static/js/jquery-1.12.4.min.js'></script><script type='text/javascript' src='http://www.wxshucaidpc.com/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獲取當前頁數,定義每頁展示的數量,和返回數據等

from django.core.paginator import Paginatordef 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_pagesreturn render(request,’app1/news.html’,locals())

django中的分頁功能已經完成,效果圖如下:

如何在django中實現分頁功能

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Django
相關文章:
国产综合久久一区二区三区