【问题标题】:Django pagination in html templatehtml模板中的Django分页
【发布时间】:2023-03-26 14:14:01
【问题描述】:

我想使用分页,但我不能在 html 中使用。

views.py

def blog_list(request):
    articles = Blogmodel.objects.all()
    page = request.GET.get('page', 1)
    paginator = Paginator(articles, 20)
    

    try:
        page = paginator.page(page)
    except PageNotAnInteger:
        page = paginator.page(1)
    except EmptyPage:
        page = paginator.page(paginator.num_pages)
    args = {'articles':articles,'page':page}
    return render(request,'blog.html',args)

html

<nav aria-label="Page navigation example">
  <ul class="pagination">
    <li class="page-item"><a class="page-link" href="#">Previous</a></li>
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
    <li class="page-item"><a class="page-link" href="#">3</a></li>
    <li class="page-item"><a class="page-link" href="#">Next</a></li>
  </ul>
</nav>

谁能帮助我,如何在 html 中做到这一点?

【问题讨论】:

    标签: django django-views pagination django-templates django-pagination


    【解决方案1】:

    这是来自Corey MS的sn-p。

    {% if is_paginated %}
        {% if page_obj.has_previous %}
            <a href="?page=1">First</a>
            <a href="?page={{ page_obj.previous_page_number}}">Previous</a>
        {% endif %}
    
        {% for num in page_obj.paginator.page_range %}
            {% if page_obj.number == num %}
                <a href="?page={{ num }}">{{ num }}</a>
            {% elif num > page_obj.number|add:'-3' %}
                <a href="?page={{ num }}">{{ num }}</a>
            {% elif num > page_obj.number|add:'3' %}
                <a href="?page={{ num }}">{{ num }}</a>
            {% endif %}
        {% endfor %}
    
        {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}">Next</a>
            <a href="?page={{ page_obj.paginator.num_pages}}">Last</a>
        {% endif %}
    {% endif %}
    

    【讨论】:

      猜你喜欢
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 2011-04-03
      • 1970-01-01
      • 2021-01-02
      • 2020-09-13
      • 2020-11-21
      相关资源
      最近更新 更多