Django 2.1.3 Error: __init__() takes 1 positional argument but 2 were given

前端 未结 1 764
小鲜肉
小鲜肉 2020-12-18 20:27

i am trying to develop a website whith Django 2.1.3 and python 3.7.1 When i go to the homepage i get this error:

TypeError at / __init__() takes 1 positiona

相关标签:
1条回答
  • 2020-12-18 20:43

    You need use as_view() at the end of class based views when declaring in the urls:

    path('', views.HomeView.as_view(), name='homepage'),
    

    Also, when using login_required decorator, you need to use it on dispatch method of CBV:

    from django.contrib.auth.decorators import login_required
    from django.utils.decorators import method_decorator
    
    class HomeView(ListView):
    
        @method_decorator(login_required)
        def dispatch(self, *args, **kwargs):
            return super(HomeView, self).dispatch(*args, **kwargs)
    
    0 讨论(0)
提交回复
热议问题