如何在django中實現分頁功能
1.在html頁面中導入js文件和css文件
<link rel="stylesheet" href="../../../static/css/jquery.pagination.css" rel="external nofollow" >
<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
# 顯示數據庫數據,并且規(guī)定每頁顯示多少條數據
page = Paginator(news,page_count)
# 當前請求的頁數
news = page.get_page(current_page)
# 顯示的總頁數
total_page = page.num_pages
return render(request,'app1/news.html',locals())
django中的分頁功能已經完成,效果圖如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
解決python 讀取excel時 日期變成數字并加.0的問題
這篇文章主要介紹了python 讀取excel時, 日期變成數字并加.0的問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
Python爬取用戶觀影數據并分析用戶與電影之間的隱藏信息!
看電影前很多人都喜歡去 『豆瓣』 看影評,所以我爬取44130條 『豆瓣』 的用戶觀影數據,分析用戶之間的關系,電影之間的聯系,以及用戶和電影之間的隱藏關系,需要的朋友可以參考下2021-06-06

