加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – django-endless与基于类的视图示例

发布时间:2020-12-16 21:49:12 所属栏目:Python 来源:网络整理
导读:我是第一次使用基于类的视图.我无法理解如何使用基于类的视图我将实现django-endless-pagination twitter样式分页. 我能举例说明一个人会怎么做? 这是我的看法: class EntryDetail(DetailView): """ Render a "detail" view of an object. By default this

我是第一次使用基于类的视图.我无法理解如何使用基于类的视图我将实现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.您可以在此处查看模板示例:How do I use pagination with Django class based generic 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

如果有人知道不同的解决方案,请评论.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读