django实现分页(pure-pagination)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django实现分页(pure-pagination)相关的知识,希望对你有一定的参考价值。

安装

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 %}


本文出自 “13261842” 博客,请务必保留此出处http://13271842.blog.51cto.com/13261842/1965730

以上是关于django实现分页(pure-pagination)的主要内容,如果未能解决你的问题,请参考以下文章

django实现分页功能

django1.8.2 建站实现分页显示功能

Django实现分页器功能

使用Django实现分页器功能

django的分页--不全也未实现

Django ajax异步请求分页的实现