Question

Hi i have a class based view, and i want to decorate its dispatch method with some function so that on basis of args/kwargs i'll perform something useful. code for class based view goes like this:

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)

My simplified decorator looks like this:

def view_count(func):
    def actual_decorator(*args, **kwargs):
        #do something useful here
        func(*args, **kwargs)
    return actual_decorator

the result is "ProjectDetailView didn't return an HttpResponse object".where do i err and what should i do, i know its simple thing but this is my first decorator doing anything useful !

Was it helpful?

Solution

You're missing a return:

def actual_decorator(*args, **kwargs): 
    #do something useful here 
    return func(*args, **kwargs)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top