django -less与基于类的视图示例 [英] django-endless with class based views example

查看:135
本文介绍了django -less与基于类的视图示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用基于类的视图。我很难理解如何使用基于类的视图,我将实施 django-endless -pagination twitter styling paging。



我可以举个例子说明一下吗?



这是我的观点:

  class EntryDetail(DetailView): 

渲染对象的细节视图
默认情况下,这是一个从`self.queryset`查找的模型实例,但是
视图将支持显示*任何*对象,通过覆盖`self.get_object()`。

context_object_name ='条目'
template_name =blog / entry.html
slug_field ='slug'
slug_url_kwarg ='slug'

def get_object(self,query_set = None):

返回视图显示的对象。

默认情况下,这需要在URLconf中使用self.queryset和pk或slug参数
,但子类可以覆盖此值以返回任何对象

slug = self.kwargs.get(self.slug_url_kwarg,None)
return get_object_or_404(Entry,slug = slug)


解决方案

ce这是一个广泛的问题,我现在想结合几个分页解决方案。



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 only:

  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



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


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)

解决方案

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.

这篇关于django -less与基于类的视图示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆