【问题标题】:multiple pagination in django class based view - listview基于 django 类的视图中的多个分页 - listview
【发布时间】:2014-06-20 23:33:26
【问题描述】:

下面的代码对查询集进行了分页,但我如何对 context['guser1'] 进行分页

class AuthorList(ListView):
    template_name = 'authorList.html'
    paginate_by = 10
    queryset = Author.objects.order_by('date')

def get_context_data(self, **kwargs):
    context = super(AuthorList, self).get_context_data(**kwargs)
    context['guser1'] = Author.objects.order_by('date')
    return context

【问题讨论】:

标签: python django django-views django-class-based-views


【解决方案1】:

您必须像在视图中一样使用Paginator 对象。这是docs

【讨论】:

  • 你能举例说明如何使用基于类的视图。
【解决方案2】:

可以在模板中的上下文变量page_obj 下访问视图的分页器。页码作为 url 中的 GET 参数传递,page。这是一个简单的例子:

{% if page_obj.has_previous %}
    <a href="{% url "your_view_url" %}?page={{ page_obj.previous_page_number }}">Previous page</a>
{% endif %}
{% if page_obj.has_next %}
    <a href="{% url "your_view_url" %}?page={{ page_obj.next_page_number }}">Next page</a>
{% endif %}

【讨论】:

    猜你喜欢
    • 2017-05-08
    • 2014-10-12
    • 1970-01-01
    • 2021-09-10
    • 2017-01-22
    • 2019-09-24
    • 2011-09-18
    • 2013-10-22
    • 2021-01-22
    相关资源
    最近更新 更多