温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

django实现分页(pure-pagination)

发布时间:2020-07-17 06:49:15 来源:网络 阅读:1026 作者:MonCheri_Flower 栏目:开发技术

安装

pip install django-pure-pagination
具体步骤搜索GitHub里面详情。
class OrgView(View):
    """
        课程机构列表功能
    """

    def get(self, request):
    
        all_orgs = CourseOrg.objects.all()  # 课程机构
        org_nums = all_orgs.count()  # 记录多少家
        # 城市
        all_citys = CityDict.objects.all()        # 课程机构进行分页
        try:
            page = request.GET.get('page', 1)        except PageNotAnInteger:
            page = 1
        p = Paginator(all_orgs, 5, request=request)
        orgs = p.page(page)        return render(request, 'org-list.html', {            "all_orgs": orgs,            "all_citys": all_citys,            'org_nums': org_nums,
        })
通过传过来的orgs,自动生成页脚
{#  分页#}
                    <div class="pageturn">
                        <ul class="pagelist">
                            {#  如果有,获取上一页 #}
                            {% if all_orgs.has_previous %}
                                <li class="long"><a href="?{{ all_orgs.previous_page_number.querystring }}"
                                                    class="page">上一页</a></li>
                            {% endif %}
                            
                            {# 判断剩余页面的显示方法: #}
                            {% for page in all_orgs.pages %}
                            
                                {% if page %}
                                
                                    {# 判断page和 all_orgs.number是否相等 : }
                                    {% ifequal page all_orgs.number %}
                                        <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a></li>
                                    {% else %}
                                        <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a></li>
                                    {% endifequal %}
                                {% else %}
                                    <li class="none"><a href="">...</a></li>
                                {% endif %}
                            {% endfor %}
                            
                            {#  如果有,获取下一页 #}
                            {% if all_orgs.has_next %}
                                <li class="long"><a href="?{{ all_orgs.next_page_number.querystring }}">下一页</a></li>
                            {% endif %}
                            
                        </ul>
                    </div>

总结

切记:做分页获取数据要调用 .object_list
{% for course_org in all_orgs.object_list %}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI