1
嗨,我有一个基于类的视图,我想用一些函数来装饰它的dispatch方法,以便在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
结果是“ProjectDetailView没有返回HttpResponse对象”。凡做我犯错和我该怎么做,我知道它简单的事情,但这是我的第一个装饰者做任何有用的事情!