从templateview继承的许多视图

时间:2013-07-03 12:17:18

标签: django django-templates

我有两个视图继承自templateview,需要通过required_login登录。 我单独创建视图时很简单:

class AboutView(TemplateView):   
    template_name = 'app1/about.html'

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

class HelpView(TemplateView):   
    template_name = 'app1/help.html'

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

这确实有效。问题是为什么下面的代码不起作用

class StaticTemplateView(TemplateView):                        
    @method_decorator(login_required)                          
    def dispatch(self, *args, **kwargs):                       
        return super(AboutView, self).dispatch(*args, **kwargs)


class AboutView(StaticTemplateView):                           
    template_name = 'app1/about.html'                        


class HelpView(StaticTemplateView):                            
    template_name = 'app1/help.html'                         

错误:

super(type, obj): obj must be an instance or subtype of type

提前致谢

1 个答案:

答案 0 :(得分:2)

我认为错误在于复制/粘贴:)

相反:

return super(AboutView, self).dispatch(*args, **kwargs)

试试这个:

return super(StaticTemplateView, self).dispatch(*args, **kwargs)