我第一次使用基於類的視圖。我無法低估如何使用基於類的視圖,我將實施django-endless-pagination Twitter樣式分頁。django - 基於類的視圖例子
我可以舉一個例子說明如何去做這件事嗎?
這是我的看法:
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)
爲什麼'DetailView'與分頁有什麼關係?你只有1個對象,就是它 –
你想使用ListView。您可能會發現此問題有幫助:http://stackoverflow.com/questions/5907575/how-do-i-use-pagination-with-django-class-based-generic-listviews – meshy