嗨我有一个基于类的视图,我想用一些函数来装饰它的调度方法,这样在args / kwargs的基础上我会执行一些有用的操作。 基于类的视图的代码如下:
from django.utils.decorators import method_decorator
class ProjectDetailView(FormMixin, DetailView):
template_name = 'account/inner-profile-page.html'
model = ProjectDetail
form_class = CommentForm
context_object_name = 'project'
@method_decorator(view_count)
def dispatch(self, *args, **kwargs):
return super(ProjectDetailView,self).dispatch(*args, **kwargs)
def get_object(self, queryset=None):
user = User.objects.get(user_slug=self.kwargs['user_slug'])
title_slug = self.kwargs['title_slug'].replace(' ','-')
return get_object_or_404(ProjectDetail, title_slug = title_slug, user=user)
我的简化装饰器看起来像这样:
def view_count(func):
def actual_decorator(*args, **kwargs):
#do something useful here
func(*args, **kwargs)
return actual_decorator
结果是&#34; ProjectDetailView没有返回一个HttpResponse对象&#34;。我在哪里犯错,我应该怎么做,我知道它很简单,但这是我的第一个装饰做任何有用的事情!< / p>
答案 0 :(得分:7)
您错过了return
:
def actual_decorator(*args, **kwargs):
#do something useful here
return func(*args, **kwargs)