django分页"该页面不包含任何结果"

时间:2018-01-19 14:56:24

标签: django django-models django-views django-pagination

我想使用django分页。第一页可以加载产品,但最后一页没有显示。

这是我的索引视图:

def index(request):

    categories = Category.objects.filter(parent_category=None)

    product_list = Product.objects.filter(is_deleted=False).order_by('created_at')
    paginator = Paginator(product_list, 1) # Show 25 contacts per page
    page = request.GET.get("page",1)

    try:
        products = paginator.page(page)
    except PageNotAnInteger:
        products = paginator.page(1)
    except EmptyPage:
        products = paginator.page(1)

    context = {
        'products': products,
        'categories': categories
    }
    return render(request, 'product/urunler.html', context)

这是我的模板:

<div class="blog-pagination">
                        <ul class="flat-pagination style1">
                            {% if products.has_previous %}
                                <li class="prev">
                                    <a href="?page={{products.previous_page_number}}" title="">
                                        <img src="{% static 'images/icons/left-1.png' %}" alt="">Önceki Sayfa
                                    </a>
                                </li>
                            {% else %}
                                <li class="prev">
                                    <a href="#" title="">
                                        <img src="{% static 'images/icons/left-1.png' %}" alt="">Önceki Sayfa
                                    </a>
                                </li>
                            {% endif %}
                            {% for i in products.paginator.page_range %}
                                {% if products.number == i %}
                                    <li class="active">
                                        <a href="?page={{i}}" class="waves-effect" title="">{{i}}</a>
                                    </li>
                                {% else %}
                                    <li>
                                        <a href="?page={{i}}" class="waves-effect" title="">{{i}}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}
                            {% if products.has_previous %}
                                <li class="next">
                                    <a href="?page={{products.next_page_number}}" title="">
                                        Sonraki Sayfa
                                        <img src="{% static 'images/icons/right-1.png' %}" alt="">
                                    </a>
                                </li>
                            {% else %}
                            {% if products.has_next %}
                                <li class="next">
                                        <a href="" title="">
                                            Sonraki Sayfa
                                            <img src="{% static 'images/icons/right-1.png' %}" alt="">
                                        </a>
                                </li>
                            {% endif %}                                                           
                            {% endif %}                           
                        </ul>
                        <div class="clearfix"></div>
                    </div>

我的数据库中有2个产品我能够显示第一页,但最后一页未显示此处是追溯:

Traceback (most recent call last):
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
ecommerce_1            |     response = get_response(request)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
ecommerce_1            |     response = self.process_exception_by_middleware(e, request)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
ecommerce_1            |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
ecommerce_1            |   File "/ecommerce/Product/views.py", line 22, in index
ecommerce_1            |     return render(request, 'product/urunler.html', context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/shortcuts.py", line 30, in render
ecommerce_1            |     content = loader.render_to_string(template_name, context, request, using=using)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/loader.py", line 68, in render_to_string
ecommerce_1            |     return template.render(context, request)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py", line 66, in render
ecommerce_1            |     return self.template.render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 207, in render
ecommerce_1            |     return self._render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
ecommerce_1            |     return self.nodelist.render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
ecommerce_1            |     bit = node.render_annotated(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
ecommerce_1            |     return self.render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 177, in render
ecommerce_1            |     return compiled_parent._render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 199, in _render
ecommerce_1            |     return self.nodelist.render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
ecommerce_1            |     bit = node.render_annotated(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
ecommerce_1            |     return self.render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/loader_tags.py", line 72, in render
ecommerce_1            |     result = block.nodelist.render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
ecommerce_1            |     bit = node.render_annotated(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
ecommerce_1            |     return self.render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/defaulttags.py", line 322, in render
ecommerce_1            |     return nodelist.render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 990, in render
ecommerce_1            |     bit = node.render_annotated(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
ecommerce_1            |     return self.render(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 1040, in render
ecommerce_1            |     output = self.filter_expression.resolve(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 708, in resolve
ecommerce_1            |     obj = self.var.resolve(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 849, in resolve
ecommerce_1            |     value = self._resolve_lookup(context)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/template/base.py", line 911, in _resolve_lookup
ecommerce_1            |     current = current()
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/core/paginator.py", line 158, in next_page_number
ecommerce_1            |     return self.paginator.validate_number(self.number + 1)
ecommerce_1            |   File "/usr/local/lib/python3.6/site-packages/django/core/paginator.py", line 50, in validate_number
ecommerce_1            |     raise EmptyPage(_('That page contains no results'))

第一页没有任何错误,但最后一页总是出错。

3 个答案:

答案 0 :(得分:0)

您正在检查has_previous,但随后又包括next_page_number。看起来您想要包含previous_page_number

{% if products.has_previous %}
    <a href="?page={{products.previous_page_number}}" title="">Previous</a>
{% endif %}

答案 1 :(得分:0)

在for循环之后的代码中,您正在检查has_previous,并且您正在犯小错误。 for循环后的代码应为

{% if products.has_next %}
 <li class="next">
  <a href="?page={{products.next_page_number}}" title="">Sonraki Sayfa
    <img src="{% static 'images/icons/right-1.png' %}" alt=""></a>
 </li>
{% else %}
 <li class="next">
  <a href="" title="">Sonraki Sayfa
    <img src="{% static 'images/icons/right-1.png' %}" alt=""></a>
 </li>
{% endif %}

答案 2 :(得分:0)

也许您也可能使用分页器进行了一些尝试,并在模板中插入了多个标签来测试其工作/样式?
如果是这样,即使您注释掉这些行,django标签仍然可以使用。
因此,如果要测试分页器,则必须完全删除其他分页器。