[英]django-endless with class based views example
我是第一次使用基於類的視圖。 我無法理解如何使用基於類的視圖我將實現django-endless-pagination twitter樣式分頁。
我能舉例說明一個人會怎么做?
這是我的看法:
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)
由於這是一個廣泛的問題,我現在想結合幾種分頁解決方案。
1.使用通用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
僅使用urls.py
會更快:
url(r'^entries/$', ListView.as_view(model=Entry, paginate_by=10))
所以基本上你不需要在這個解決方案中使用django-endless-pagination。 你可以在這里檢查模板的例子: 如何使用基於Django類的通用ListViews的分頁?
2.使用django-endless-pagination的AjaxListView :
from endless_pagination.views import AjaxListView
class EntryList(AjaxListView):
model = Entry
context_object_name = 'entry_list'
page_template = 'entry.html'
或者更快(再次)使用urls.py
:
from endless_pagination.views import AjaxListView
url(r'^entries/$', AjaxListView.as_view(model=Entry))
參考: http : //django-endless-pagination.readthedocs.org/en/latest/generic_views.html
如果有人知道不同的解決方案,請評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.