【问题标题】:How to perform pagination for context object in django?如何在 django 中为上下文对象执行分页?
【发布时间】:2019-03-02 02:03:54
【问题描述】:

我在views.py中尝试过这样的事情:

class HomePage(TemplateView):
     template_name = "clouderp/index.html"

    def get_context_data(self, **kwargs):
        context = super(HomePage, self).get_context_data(**kwargs) 
        qs = Blog.objects.all()
        context['blog_list'] = qs

        page = self.request.GET.get('page')

        paginator = Paginator(qs, 4)

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

        context['users'] = users

        return context

在模板中:

{% if users.has_other_pages %}
<ul class="pagination">
  {% if users.has_previous %}
    <li><a href="?page={{ users.previous_page_number }}">&laquo;</a></li>
  {% else %}
    <li class="disabled"><span>&laquo;</span></li>
  {% endif %}
  {% for i in users.paginator.page_range %}
    {% if users.number == i %}
      <li class="active"><span>{{ i }} <span class="sr-only"></span></span></li>
    {% else %}
      <li><a href="?page={{ i }}">{{ i }}</a></li>
    {% endif %}
  {% endfor %}
  {% if users.has_next %}
    <li><a href="?page={{ users.next_page_number }}">&raquo;</a></li>
  {% else %}
    <li class="disabled"><span>&raquo;</span></li>
  {% endif %}
</ul>
{% endif %}

我使用 django 创建了一个博客应用程序
我想在我的 index.html 主页面中显示我的所有博客,并且还想为我的索引页面中的博客进行分页...
我想知道该怎么做...
因为我做博客的过程不是一次按4分页的...

【问题讨论】:

  • 使用ListView 代替TemplateView 可能更简单,因为ListView 已经有分页选项。
  • context['blog_list'] = qs 所以在你的模板中 blog_list 没有分页。 users 是分页的(为什么blog_list 的页面还是叫users?)。

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


【解决方案1】:

请尝试以下代码: 在views.py 代码中

    class BookListView(ListView):
    model=Book
    paginate_by=10
    template_name='book/book_list.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        print (context)
        paginator = context['paginator']
        page_numbers_range = 10  # Display 5 page numbers
        max_index = len(paginator.page_range)

        page = self.request.GET.get('page')
        print (self.request)
        current_page = int(page) if page else 1

        start_index = int((current_page - 1) / page_numbers_range) * page_numbers_range
        end_index = start_index + page_numbers_range
        if end_index >= max_index:
            end_index = max_index

        page_range = paginator.page_range[start_index:end_index]
        context['page_range'] = page_range
        return context

book_list = BookListView.as_view()

和模板:

<div class="container">
            <!-- Pagination -->
            {% if is_paginated %}
            <nav>
                <ul class="pagination justify-content-center" style="margin:20px 0">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}">
                        <span>Prev</span>
                    </a>
                    </li>
                {% else %}
                    <li class="disabled page-item">
                    <a class="page-link" href="#">
                        <span>Prev</span>
                    </a>
                    </li>
                {% endif %}

                {% for page in page_range %}
                    <li {% if page == page_obj.number %} class="active page-item" {% endif %}>
                    <a class="page-link" href="?page={{ page }}">{{ page }}</a>
                    </li>
                {% endfor %}
                {% if page_obj.has_next %}
                    <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.next_page_number }}">
                        <span>Next</span>
                    </a>
                    </li>
                {% else %}
                    <li {% if not page_obj.has_next %}class="disabled page-item"{% endif %}>
                    <a class="page-link" href="#">
                        <span>Next</span>
                    </a>
                    </li>
                {% endif %}
                </ul>
            </nav>
            {% endif %}
    </div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 2019-10-14
    • 2012-08-28
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    相关资源
    最近更新 更多