2013-07-03 32 views
1

我有兩個視圖從templateview繼承,並需要通過required_login登錄。 當我單獨創建視圖時很簡單:從templateview繼承的許多視圖

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 

感謝

回答

2

我認爲錯誤在於複製/粘貼:)

取而代之:

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

試試這個:

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

感謝和抱歉,這個愚蠢的錯誤x) – smarber