Django标签结果分页不像我预期的那样工作

时间:2016-01-27 15:42:34

标签: python django pagination

我正在尝试对从标签中返回的结果进行分页,但它无法正常工作。如果我有三个帖子,那么paginator会有三个结果页面,如果我有两个帖子,那么paginator会有两个结果页面。我有多少帖子这就是分页器将拥有多少页面。如果我使用它来从数据库返回正常结果,则paginator可以工作。我做的是复制它并尝试修改它以适应标签分页。但它并没有像我预期的那样发挥作用。我还把分页放在一个包含文件中,试图让事情变干,我不确定我的问题是不是我的代码。

pagination.html:

<div class="text-center" style="margin-bottom: 20px">
<ul class="pagination">
{% if queryset.has_previous %}
    <li><a href="?page=1"><<</a></li>
    <li><a href="?{{ page_request_var }}={{ queryset.previous_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">prev</a></li>
{% endif %}


{% for i in paginator.page_range %}

 <li {% if page_obj.number == i %} class="active" {% endif %}><a href="?page={{i}}">{{i}}</a><li>
<!--Page {{ queryset.number }} of {{ queryset.paginator.num_pages }}.-->

{% endfor %}


{% if queryset.has_next %}
    <li><a href="?{{ page_request_var }}={{ queryset.next_page_number }}
    {% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">next</a></li>
    <li><a href="?page={{ queryset.paginator.num_pages }}">>></a></li>
{% endif %}
</ul>

这个分页不起作用我想把它变成views.py:

def tag_list(request, slug=None):
instance = get_object_or_404(Tag, slug=slug)
ins = instance.post_set.all()

query = request.GET.get("q")
if query:
    ins = ins.filter(
            Q(title__icontains=query) |
            Q(content__icontains=query) |
            Q(user__first_name__icontains=query) |
            Q(user__last_name__icontains=query)
    ).distinct()

paginator = Paginator(ins, 1)
page_request_var = "page"
page = request.GET.get(page_request_var)
try:
    queryset = paginator.page(page)
except PageNotAnInteger:
    # If page is not an integer, deliver first page.
    queryset = paginator.page(1)
except EmptyPage:
    # If page is out of range (e.g. 9999), deliver last page of results.
    queryset = paginator.page(paginator.num_pages)

context = {
    "instance": ins,
    "queryset": queryset,
    "paginator": paginator,

}
return render(request, "posts/tag_list.html", context)

以下是我复制分页的地方。这个工作

def post_list(request):
today = timezone.now().date()
queryset_list = Post.objects.active()
if request.user.is_staff or request.user.is_superuser:
    queryset_list = Post.objects.all()

query = request.GET.get("q")
if query:
    queryset_list = queryset_list.filter(
            Q(title__icontains=query) |
            Q(content__icontains=query) |
            Q(user__first_name__icontains=query) |
            Q(user__last_name__icontains=query)
    ).distinct()
paginator = Paginator(queryset_list, 6)  # Show 25 contacts per page
page_request_var = "page"
page = request.GET.get(page_request_var)
try:
    queryset = paginator.page(page)
except PageNotAnInteger:
    # If page is not an integer, deliver first page.
    queryset = paginator.page(1)
except EmptyPage:
    # If page is out of range (e.g. 9999), deliver last page of results.
    queryset = paginator.page(paginator.num_pages)
context = {
        "queryset": queryset,
        "title": "Posts",
        "page_request_var": page_request_var,
        "today": today,
        "queryset_list": queryset_list,
        "paginator": paginator,
    }

return render(request, "posts/post_list.html", context)

1 个答案:

答案 0 :(得分:0)

分页符的第二个参数是每页显示多少结果,目前你将它设置为1,根据你的评论,你可能希望这是4

paginator = Paginator(ins, 1)

应该是

paginator = Paginator(ins, 4)

您也永远不会将page_request_var传回模板,这意味着它无法找到正确的页面,然后返回相同的页面

context = {
    "instance": ins,
    "queryset": queryset,
    "paginator": paginator,
    "page_request_var": page_request_var
}