我如何在Django分页中显示活动页面

时间:2018-11-20 17:10:07

标签: javascript python jquery html django

我正在使用django 2.0并尝试制作此博客,问题是,一旦我单击下一页,分页部分中的活动栏就不会从1更改为2,请参见图片image

我不知道哪里出了错

这是views.py

def post_list(request):
    object_list=Post.objects.filter(status='Published').order_by("-created")
    pages = pagination(request,object_list,3)
    context={
        'items':pages[0],
        'page_range':pages[1],
    }
    return render(request,"blog.html",context)

分页功能

from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator

def pagination(request,data,num=10):
    paginator = Paginator(data,num) # Show 5 contacts per page

    page = request.GET.get('page',5)
    try:
        items=paginator.page(page)
    except PageNotAnInteger:
        items=paginator.page(5)
    except EmptyPage:
        items=paginator.page(paginator.num_pages)
    index=items.number=1
    max_index=len(paginator.page_range)
    start_index=index - 5 if index >= 5 else 0
    end_index=index + 5 if index <= max_index - 5 else max_index
    page_range=paginator.page_range[start_index:end_index]
    return items, page_range

和pagination.html

<nav>
{% if items.has_other_pages %}
  <ul class="pagination">
    {% if items.has_previous %}
      <li><a href="?page={{ items.previous_page_number }}">&laquo;</a></li>
    {% else %}
      <li class="disabled"><span>&laquo;</span></li>
    {% endif %}
    {% for i in page_range %}
      {% if items.number == i %}
        <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
      {% else %}
        <li><a href="?page={{ i }}">{{ i }}</a></li>
      {% endif %}
    {% endfor %}
    {% if items.has_next %}
      <li><a href="?page={{ items.next_page_number }}">&raquo;</a></li>
    {% else %}
      <li class="disabled"><span>&raquo;</span></li>
  </ul>
    {% endif %}
{% endif %}
  </nav>

blog.html

{% for obj in items %}
    {% if obj.status == 'Published' %}
      <article>
        <img src="{{obj.thumb.url}}" alt="" />
        <div class="post-content">
          <h2>{{obj.title}}</h2>
            {{obj.created}}  Author {{obj.user}} <h4><a href="{% url 'category_detail' slug=obj.Category.slug %}">{{obj.Category}}</a></h4>
            <hr/>
            <p>{{obj.body}}</p>
    <a class="mtr-btn button-navy ripple" href= "{% url 'post_detail' obj.slug %}">Continue reading →</a><br>
    </div>
    </article>
    {% endif %}
{% endfor %}
{% include 'pagination.html' %}
</div>

1 个答案:

答案 0 :(得分:0)

= 1行中删除index = items.number = 1