gpt4 book ai didi

Django 分页 "That page contains no results"

转载 作者:行者123 更新时间:2023-12-04 16:06:58 25 4
gpt4 key购买 nike

我想使用 django 分页。第一页能够加载产品,但最后一页没有显示它们。

这是我的索引 View :

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'))

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

最佳答案

您正在查看 has_previous但随后包括 next_page_number .看起来您想包含 previous_page_number反而:

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

关于Django 分页 "That page contains no results",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48343814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com