zoukankan      html  css  js  c++  java
  • 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中的分页功能已经完成,效果图如下:

  • 相关阅读:
    福大软工 · BETA 版冲刺前准备(团队)
    福大软工 · 第十一次作业
    Alpha 冲刺 (9/10)
    Alpha 冲刺 (8/10)
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
    Alpha 冲刺 (5/10)
    Alpha 冲刺 (4/10)
    福大软工1816 · 团队现场编程实战(抽奖系统)
    阿里八八β阶段Scrum(5/5)
  • 原文地址:https://www.cnblogs.com/lutt/p/10764440.html
Copyright © 2011-2022 走看看