【问题标题】:How do I control pagination using a class-based view in django-tables2?如何在 django-tables2 中使用基于类的视图来控制分页?
【发布时间】:2015-06-15 22:15:31
【问题描述】:

我有一个来自 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)

【问题讨论】:

  • 您想设置哪些分页选项(或者您只是想禁用分页)。如果您展示您尝试过的代码,而不是仅仅说它不起作用,将会很有帮助。
  • 我想禁用分页。我已经合并了上面视图的代码。

标签: python django view pagination django-tables2


【解决方案1】:

如果要禁用分页,则需要设置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

【讨论】:

  • 是的,我只尝试覆盖该方法,因为直接在属性上设置值不起作用。我不敢相信它像 False vs None 一样简单!谢谢
【解决方案2】:

覆盖YourTable(Table)中的方法paginate(..\django_tables2\tables.py)

带#号的行,进行分页,注释掉,这样就失效了。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 2020-11-06
    • 2019-08-29
    • 1970-01-01
    • 2019-09-24
    相关资源
    最近更新 更多