我有一个页面blog.html
,并且分页序列加载得很好,但我查询分页器在页面上显示一个元素以测试分页是否有效。我在DB中有4个项目,分页读取4个元素,但显示所有页面上的所有元素。
views.py
def blog(request):
blog_cat = Blog_Categorie.objects.all()
all_blog = Blog.objects.all()
page = request.GET.get('page')
paginator = Paginator(all_blog, 1)
try:
blog = paginator.page(page)
except PageNotAnInteger:
blog = paginator.page(1)
except EmptyPage:
blog = paginator.page(paginator.num_pages)
return render_to_response('blog.html',
'blog':all_blog,'blog_cat':blog_cat,'blog_p':blog})
blog.html
<div class="col-lg-12 col-md-12 text-center">
{% if blog_p.has_other_pages %}
<ul class="pagination">
{% if blog_p.has_previous %}
<li><a href="?page={{ users.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in blog_p.paginator.page_range %}
{% if blog_p.number == i %}
<li class="active"><span>{{ i }}</span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if blog_p.has_next %}
<li><a href="?page={{ users.next_page_number }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
</div>