6

I'm using Class Based Views for the first time. I'm having trouble understating how using class based views I would implement django-endless-pagination twitter styling paging.

Could I have an example of how one would go about this?

This is my view:

class EntryDetail(DetailView):
    """
    Render a "detail" view of an object.
    By default this is a model instance looked up from `self.queryset`, but the
    view will support display of *any* object by overriding `self.get_object()`.
    """
    context_object_name = 'entry'
    template_name = "blog/entry.html"
    slug_field = 'slug'
    slug_url_kwarg = 'slug'

    def get_object(self, query_set=None):
        """
        Returns the object the view is displaying.

        By default this requires `self.queryset` and a `pk` or `slug` argument
        in the URLconf, but subclasses can override this to return any object.
        """
        slug = self.kwargs.get(self.slug_url_kwarg, None)
        return get_object_or_404(Entry, slug=slug)
GrantU
  • 6,325
  • 16
  • 59
  • 89
  • why does `DetailView` have anything to do with pagingation? You have only 1 object and that's it – Hieu Nguyen Aug 02 '13 at 11:46
  • You want to use ListView. You may find this question helpful: http://stackoverflow.com/questions/5907575/how-do-i-use-pagination-with-django-class-based-generic-listviews – meshy Aug 02 '13 at 11:47

1 Answers1

4

Since this is a broad question, I would like to combine several solutions for pagination now.

1.Use the generic ListView:

from django.views.generic import ListView

class EntryList(ListView):
    model = Entry
    template_name = 'blog/entry_list.html'
    context_object_name = 'entry_list'
    paginate_by = 10

It would be way faster using only urls.py:

url(r'^entries/$', ListView.as_view(model=Entry, paginate_by=10))

So basically you don't need django-endless-pagination in this solution. You can check the example of template here: How do I use pagination with Django class based generic ListViews?

2.Use django-endless-pagination's AjaxListView:

from endless_pagination.views import AjaxListView    
class EntryList(AjaxListView):
    model = Entry
    context_object_name = 'entry_list'
    page_template = 'entry.html'

Or faster (again) with urls.py only:

from endless_pagination.views import AjaxListView

url(r'^entries/$', AjaxListView.as_view(model=Entry))

Reference: http://django-endless-pagination.readthedocs.org/en/latest/generic_views.html

If anyone knows different solution, please comment.

Community
  • 1
  • 1
Hieu Nguyen
  • 8,563
  • 2
  • 36
  • 43
  • I don't understand `paginate_by = 10` of the ListView example. The django docs don't explain this. Which code should evaluate the `paginate_by = 10`? – guettli Apr 28 '16 at 19:56
  • 1
    `ListView` subclass `MultipleObjectMixin`. And you can check its docs about pagination here: https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin – Hieu Nguyen Apr 29 '16 at 15:20