分页的使用

一、分页插件插件地址

二、使用方式

  • 1、安装依赖包

    pipenv install django-pure-pagination
      # 如果安装不成功可以,你可以尝试使用下面命令
      # pip install https://github.com/Burakhan/django-pure-pagination/archive/master.zip
    
  • 2、添加到app

    INSTALLED_APPS = (
        ...
        'pure_pagination',
    )
    
  • 3、根据需要自己配置文件,不配置就使用默认的就可以

    PAGINATION_SETTINGS = {
        'PAGE_RANGE_DISPLAYED': 10,
        'MARGIN_PAGES_DISPLAYED': 2,
    
        'SHOW_FIRST_PAGE_WHEN_INVALID': True,
    }
    
  • 4、在视图类中使用

    from django.shortcuts import render_to_response
    from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
    
    def index(request):
        """
        定义一个分页的视图
        """
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
    
        objects = ['john', 'edward', 'josh', 'frank']
    
        # Provide Paginator with the request object for complete querystring generation
    
        p = Paginator(objects, 2, request=request)
    
        people = p.page(page)
    
        return render_to_response('index.html', {
            'people': people,
        }
    
  • 5、在视图中数据

      {# 注意这个地方要调用.object_list #}
      {% for article in article_list.object_list %}
              <dl class="article-list">
                      <dt class="article-list-title">
                              <a href="/article/article_detail/{{ article.id }}"
                                      target="_blank">{{ article.title }}</a>
                      </dt>
                      <dd class="article-list-footer">
                              <div class="info text-left">
                                      <span>{{ article.create_at }}</span>
                                      <span>阅读数:<span>{{ article.read_num }}</span></span>
                                      <span>评论数:<span>{{ article.comment_num }}</span></span>
                                      <span>点赞数:<span>{{ article.click_num }}</span></span>
                              </div>
                              <div class="edit text-right">
                                      {% if article.author == request.user %}
                                              <a href="#">取消置顶</a>
                                              <a href="{% url 'article:edit-article' article.id %}">编辑</a>
                                              <a href="javascript:void(0);" class="del-btn" data-id="{{ article.id }}">删除</a>
                                      {% else %}
                                              <a href="#">收藏</a>
                                              <a href="javascript:void(0);" class="click-num" data-id="{{ article.id }}">
                                                      {% if article.id in blog_id_list %}
                                                              取消点赞
                                                      {% else %}
                                                              点赞
                                                      {% endif %}
                                              </a>
                                      {% endif %}
                              </div>
                      </dd>
              </dl>
      {% endfor %}
    
  • 6、使用页码功能

      <div class="page">
              <nav aria-label="Page navigation">
                      <ul class="pagination">
                              {# 如果有上一页就显示 #}
                              {% if article_list.has_previous %}
                                      <li class="long"><a href="?{{ article_list.previous_page_number.querystring }}">上一页</a>
                                      </li>
                              {% endif %}
                              {# 显示中间的页码 #}
                              {% for page in article_list.pages %}
                                      {% if page %}
                                              {% ifequal page article_list.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 article_list.has_next %}
                                      <li class="long"><a href="?{{ article_list.next_page_number.querystring }}">下一页</a></li>
                              {% endif %}
                      </ul>
              </nav>
      </div>
    

results matching ""

    No results matching ""