What's the difference between the two methods of decorating class-based views?

房东的猫 提交于 2019-12-07 06:44:41

问题


I'm writing a view that inherits from ListView, and am trying to restrict the view to logged-in users.

https://docs.djangoproject.com/en/dev/topics/class-based-views/#decorating-in-urlconf says that decorating with login_required in the URLconf "applies the decorator on a per-instance basis. If you want every instance of a view to be decorated, you need to take a different approach" -that approach being to decorate the dispatch method in the view code.

I thought I knew the difference between a class and an instance but this phrase doesn't mean anything to me. Could someone clarify? Apart from having a decorator in the URLconf as opposed to in your class definition, what are the differences between the two approaches?

The paragraph above that link seems to answer the question: "Since class-based views aren't functions, decorating them works differently depending on if you're using as_view or creating a subclass."

Really?? I seem to be able to use the URLconf approach with my subclass of ListView.


回答1:


Imagine you have the following class based view:

class PostListView(ListView):
     model = Post

ProtectedPostListView = login_required(PostListView.as_view())

and your urls.py:

url(r'posts$', ProtectedPostListView)

If you use this approach then you lose the ability to subclass ProtectedPostListView e.g

class MyNewView(ProtectedPostListView):
    #IMPOSSIBLE

and this is because the .as_view() returns a function and after applying the login_required decorator you are left with a function, so subclassing is not possible.

On the other hand if you go with the second approach i.e use the method decorator the subclassing is possible. e.g

class PostListView(ListView):
     model = Post

     @method_decorator(login_required)
     def dispatch(self, *args, **kwargs):
         return super(PostListView, self).dispatch(*args, **kwargs)

class MyNewView(PostListView):
     #LEGAL


来源:https://stackoverflow.com/questions/12132819/whats-the-difference-between-the-two-methods-of-decorating-class-based-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!