如何在 django-tables2 中使用基于 class 的视图控制分页?

How do I control pagination using a class-based view in django-tables2?

我有一个派生自 SingleTableView 的视图。

关于禁用分页的说明围绕使用对 RequestConfig 的调用展开,但是在我看来我没有实现采用请求参数的函数。

我已经尝试覆盖视图中的 get_table_pagination() 函数和 table_pagination 属性,但这不起作用。

class DetailBuildView(SingleTableView):
    template_name = 'shoppinglist/detailbuild.html'
    table_class = BuildLineTable
    table_pagination = None
    def get_table_pagination(self):
      return None

    def get_queryset(self):
        self.shoppinglist = get_object_or_404(ShoppingList, id=self.kwargs['shoppinglist'])
        return BuildLine.objects.filter(shopping_list=self.shoppinglist)

如果你想禁用分页,那么你需要设置table_pagination=False。将其设置为 None 表示视图使用默认分页。

class DetailBuildView(SingleTableView):
    template_name = 'shoppinglist/detailbuild.html'
    table_class = BuildLineTable
    table_pagination = False

您可以按如下方式覆盖 get_table_pagination,而不是设置 table_pagination,但这样做没有任何优势。

    def get_table_pagination(self):
        return False

你的Table(Table)[=中覆盖方法paginate(..\django_tables2\tables.py) 17=]:

带#的行,进行分页,注释掉,就停用了。

from django.core.paginator import Paginator

     def paginate(self, paginator_class=Paginator, per_page=None, page=1, *args, **kwargs):
        """
        Paginates the table using a paginator and creates a ``page`` property
        containing information for the current page.

        Arguments:
            paginator_class (`~django.core.paginator.Paginator`): A paginator class to
                paginate the results.

            per_page (int): Number of records to display on each page.
            page (int): Page to display.

        Extra arguments are passed to the paginator.

        Pagination exceptions (`~django.core.paginator.EmptyPage` and
        `~django.core.paginator.PageNotAnInteger`) may be raised from this
        method and should be handled by the caller.
        """

        #per_page = per_page or self._meta.per_page
        #self.paginator = paginator_class(self.rows, per_page, *args, **kwargs)
        #self.page = self.paginator.page(page)

        return self