Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-endless with class based views example

Tags:

python

django

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)
like image 313
GrantU Avatar asked Aug 02 '13 10:08

GrantU


1 Answers

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.

like image 189
Hieu Nguyen Avatar answered Oct 31 '22 17:10

Hieu Nguyen