您的位置:首页技术文章
文章详情页

如何在django中实现分页功能

【字号: 日期:2024-10-09 18:16:16浏览:42作者:猪猪

1.在html页面中导入js文件和css文件

<link rel='stylesheet' href='https://www.haobala.com/static/css/jquery.pagination.css' rel='external nofollow' ><script type='text/javascript' src='https://www.haobala.com/static/js/jquery-1.12.4.min.js'></script><script type='text/javascript' src='https://www.haobala.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
相关文章: