【问题标题】:EmptyPage error when the page already exist in django当页面已存在于 django 中时出现 EmptyPage 错误
【发布时间】:2019-09-16 03:02:03
【问题描述】:

我对 django 有疑问。

我创建了一个分页功能,一切都很好,但是当我尝试进入分页的最后一页时,我收到错误"EmptyPage this page does not contain results"

但是那个页面真的存在!还有剩余的项目,但它没有显示查询的最后五个项目。

这是我的功能:

def clasification(request):
    categoria = Clasificacion.objects.filter(existencia=True)
    paginator = Paginator(categoria, 5)
    page = request.GET.get('page')
    try:
        items = paginator.page(page)
    except PageNotAnInteger:
        items = paginator.page(1)
    except EmptyPage:
        items = paginator.page(paginator.num_pages)
    contexto = {'meta_description':'',
                'meta_keywords':'',
                'items':items}
    return render(request, 'adminview/clasification.html', contexto)

这里似乎一切都很好......

看一下 HTML:

{% if items.has_next or items.has_previous %}
          <ul class="pagination">
            {% if items.has_previous %}
            <li class="page-item"><a class="page-link" href="?page={{ items.previous_page_number }}">Anterior</a></li>
            {% else %}
            <li class="page-item disabled"><a class="page-link">Anterior</a></li>
            {% endif %}
            {% for page in items.paginator.page_range %}
            <li class="page-item {% if items.number == page %}active{% endif %}"><a class="page-link" href="?page={{ page }}">{{ page }}</a></li>
            {% endfor %}
            {% if items.has_next %}
            <li class="page-item"><a class="page-link" href="?page={{ items.next_page_number }}">Siguiente</a></li>
            {% else %}
            <li class="page-item disabled"><a class="page-link" href="?page={{ items.next_page_number }}">Siguiente</a></li>
            {% endif %}
          </ul>
          {% endif %}

我不知道为什么会出现这个错误。

希望你能帮助我。

谢谢!

【问题讨论】:

  • 文件adminview/clasification.htmlapp/template文件夹中吗?
  • 是的,在“ 'DIRS': [os.path.join(BASE_DIR, 'templates')] ”,意思是project/templates文件夹

标签: python django templates pagination


【解决方案1】:

在您的模板中,当items.has_next 为假时,您尝试使用items.next_page_number

这将解决您的问题:

{% if items.has_next %}
  <li class="page-item"><a class="page-link" href="?page={{ items.next_page_number }}">Siguiente</a></li>
{% else %}
  <li class="page-item disabled"><a class="page-link">Siguiente</a></li>
{% endif %}

【讨论】:

    猜你喜欢
    • 2017-07-24
    • 2021-11-26
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多