Django 分页 "That page contains no results"
django pagination "That page contains no results"
我想使用 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'))
第一页没有错误,但最后一页总是有错误。
您正在检查 has_previous
但随后包括 next_page_number
。看起来你想包含 previous_page_number
而不是:
{% if products.has_previous %}
<a href="?page={{products.previous_page_number}}" title="">Previous</a>
{% endif %}
在 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 %}
也许您正在使用分页器进行一些尝试,并且您在模板中插入了多个分页器来测试它们 work/style?
如果是这样,即使您注释掉这些行,django 标签仍然有效。
所以,如果你想测试你的分页器,你必须完全删除其他的。
我想使用 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'))
第一页没有错误,但最后一页总是有错误。
您正在检查 has_previous
但随后包括 next_page_number
。看起来你想包含 previous_page_number
而不是:
{% if products.has_previous %}
<a href="?page={{products.previous_page_number}}" title="">Previous</a>
{% endif %}
在 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 %}
也许您正在使用分页器进行一些尝试,并且您在模板中插入了多个分页器来测试它们 work/style?
如果是这样,即使您注释掉这些行,django 标签仍然有效。
所以,如果你想测试你的分页器,你必须完全删除其他的。