django中如何实现分页功能
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中的分页功能已经完成,效果图如下:
python 中文名:蟒蛇,设计者:Guido van Rossum