zoukankan      html  css  js  c++  java
  • django-paginator

    django 自带了一个分页的功能 ,使用起来很方便 。下面是使用方法 :

    views.py
    def index(request):
        context  =  {}
        article = Article.objects.all()
        page = request.GET.get('page',1)
        context = get_paginator(article,page)
        return render(request, 'home/index.html',context)
    
    为了让主函数不要过于复杂,将分页器的功能分离出来了。

    分页的主代码 :

    from django.core.paginator import Paginator,InvalidPage,EmptyPage,PageNotAnInteger
    
    def get_paginator(obj,page):
        page_size = 10 #每页显示的数量
        after_range_num = 5
        before_range_num = 6 
        context = {}
        try:
            page = int(page)
            if page <1 :
                page = 1 
        except ValueError:
            page = 1 
        paginator = Paginator(obj,page_size)
        try:
            obj = paginator.page(page)
        except(EmptyPage,InvalidPage,PageNotAnInteger):
            obj = paginator.page(1)
        
        if page >= after_range_num:
            page_range = paginator.page_range[page-after_range_num:page+before_range_num]
        else:
            page_range = paginator.page_range[0:int(page)+before_range_num]
        
        context["page_objects"]=obj
        context["page_range"]=page_range
        return context 

    templates 中如下实现:

        <div id="content">
            <div id="mainbar">
                {% for article in page_objects.object_list %}
                <div class="node_article">
                    <h3><a href='{% url home.views.index %}'>{{article.subject}}</a></h3>
                    <p>{{article.content|safe|escape}}</p>
                </div>
                {%endfor%}
                <div>
                    <p class="page_container">
                    {% if page_objects.has_previous %}
                    <a href="?page={{ page_objects.previous_page_number }}" title="Pre">{% trans "next page"%}</a> {% endif %}
                    {% for p in page_range %}
                        {% ifequal p page_objects.number %}
                            <span style="font-size:16px;" title="当前页"><b>{{p}}</b></span>        
                            {% else %}<a href="?page={{p}}" title="第{{p}}页">{{p}}</a>{% endifequal %}
                    {% endfor %}     
                    {% if page_objects.has_next %}
                    <a href="?page={{ page_objects.next_page_number }}" title="Next">{% trans "next page"%}</a>
                    {% endif %}  
                    <b>
                        {%if page_objects.paginator.num_pages%} {{ page_objects.paginator.num_pages }}{% trans "page"%}{%endif%}
                    </b>
                    </p>
                </div>
            </div>
        </div>
  • 相关阅读:
    Python环境的安装
    tar.xz如何解压:linux和windows下tar.xz解压命令介绍
    设置SVN忽略文件和目录(文件夹)
    C#【Thread】Interlocked 轻量级锁
    手把手教你做个AR涂涂乐
    理解UV贴图
    unity animation readonly 无法加事件?
    LUA Metatables
    增强现实阴影
    unity shader tags
  • 原文地址:https://www.cnblogs.com/muzinan110/p/5019303.html
Copyright © 2011-2022 走看看