frankster
frankster

Reputation: 1528

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

I have a view deriving from SingleTableView.

Instructions for disabling pagination revolve around using a call to RequestConfig, however I don't have implemented in my view the function that takes the request parameter.

I have tried overriding the get_table_pagination() function in the view and the table_pagination attribute however this doesn't work.

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)

Upvotes: 3

Views: 1840

Answers (2)

bryan villa
bryan villa

Reputation: 11

Overwrite method paginate(..\django_tables2\tables.py) in YourTable(Table):

The lines that are with #, carry out the pagination, comment them and this is deactivated.

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

Upvotes: 1

Alasdair
Alasdair

Reputation: 308909

If you want to disable pagination, then you need to set table_pagination=False. Setting it to None means the view uses the default pagination.

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

Instead of setting table_pagination, you could override get_table_pagination as follows, but there isn't any advantage in doing so.

    def get_table_pagination(self):
        return False

Upvotes: 2

Related Questions