There are great things in django - generic.views that allow you to list, create and edit models in a few lines. But, unfortunately, ordinary wrappers are not applicable to them, which, for example, check if the user is logged in.

urls.py:

url(r'^network/$', ComputerListView.as_view(), name='computer_list'), 

views.py:

 class ComputerListView(SiteCommonView, ListView): # класс SiteCommonView передан сюда, чтобы брать оттуда общий контекст для всех представлений model = Computer queryset = Computer.objects.all() #@auth_user_required #def as_view(self, *args, **kwargs): # return super(ComputerListView, self).as_view(self, *args, **kwargs) def get_context_data(self, **kwargs): c = super(ComputerListView, self).get_context_data(**kwargs) c.update(self.get_context()) # это единственное для чего нужен SiteCommonView c['title'] = u'Компьютеры в сети' return c # обертка def auth_user_required(*args_, **kwargs_): def wrapper(func): def tmp(*args, **kwargs): request = args[0] if request.user: profile = get_user_profile(request) if profile: kwargs['profile'] = profile return func(*args, **kwargs) raise Http403() return tmp return wrapper 

So, if we uncomment

 #def as_view(self, *args, **kwargs): # return super(ComputerListView, self).as_view(self, *args, **kwargs) 

We TypeError at unbound method as_view() must be called with ComputerListView instance as first argument (got nothing instead) error: TypeError at unbound method as_view() must be called with ComputerListView instance as first argument (got nothing instead)

If you uncomment

 #@auth_user_required 

we get TypeError at /network/ wrapper() got an unexpected keyword argument 'object_list'

How to write wrappers for class methods in this case?

    2 answers 2

    Decorating in URLconf

    The Result Of The as_view() method. The URLconf where you deploy your view:

     from django.contrib.auth.decorators import login_required, permission_required from django.views.generic import TemplateView from .views import VoteView urlpatterns = patterns('', (r'^about/', login_required(TemplateView.as_view(template_name="secret.html"))), (r'^vote/', permission_required('polls.can_vote')(VoteView.as_view())), ) 

    In addition, you do not have the correct decorator. The decorator’s sole argument should be the method being decorated. Those. should be something like this:

     from functools import wraps from django.utils.decorators import available_attrs def auth_user_required(func): @wraps(func, assigned=available_attrs(func)) def wrapped(request, *args, **kwargs): if request.user: profile = get_user_profile(request) if profile: kwargs['profile'] = profile return func(request, *args, **kwargs) raise Http403() return wrapped 

    You can also try to make cl. in the following way:

     class ComputerListView(SiteCommonView, ListView): # или SiteCommonView.dispatch, если он у вас определен там dispatch = auth_user_required(ListView.dispatch) # ... 

      For CBV, mixins can be used instead of standard decorators. There are standard, such as LoginRequiredMixin , which is perfect for your case:

       from django.contrib.auth.mixins import LoginRequiredMixin class ComputerListView(LoginRequiredMixin, SiteCommonView, ListView): model = Computer queryset = Computer.objects.all() 

      Among the third-party, the best option is django-braces. And, of course, you can write your own.