简体   繁体   中英

__init__() missing 1 required positional argument: 'request' parent constructor error while extending class

I am trying to extend my class based views from a baseView for my custom admin Section. The following is a view for the dashboard section.

class Dashboard(BaseAdminView):

    def __init__(self, request):
        super().__init__(request)

    def get(self, request):
        return render(request, 'admin/pages/dashboard.html', {'hello': 'World'})

Similarly, the following is the base admin view which I'll be extending for almost all of the related view classes.

class BaseAdminView(View):
    loggedInUser = None

    def __init__(self, request):
        if (request.session['loggedInAdministrator'] is None):
            return redirect('adminlogin')
        else:
            loggedInUser = request.session['loggedInAdministrator']

My issue is that I am getting __init__() missing 1 required positional argument: 'request' error in the console when I try to access the dashboard.

Isn't self then request the order of variables here or am I missing something. Also I removed self still the issue was the same. If I opt out of using request variable, the constructor works fine though, but using sessions is my only need for which I am doing this in the first place.

How do I make this work?? How do I access request in the parent class??

Also I see that

def __init__(self, request):
    super().__init__(request)

this code in the dashboard is unnecessary, as the base constructor gets called even if I remove this. Is this how it works? Fairy new to the language and framework..

Addition: urls.py in concerned app

urlpatterns = [
    path('', views.Dashboard.as_view(), name='adminhome'),
    path('login', views.Login.as_view(), name='adminlogin'),
    path('logout', views.Logout.as_view(), name='adminlogout')
]

According to your question you need to implement some action related to request object Before you entered to request method handler like GET/POST in your view , behind the scene, at the __init__() method you don't received any request object , as you can see if you add *args and **kwargs to init , there was empty in your view classes.

You shouldn't be overriding __init__() . The request object is first available in the dispatch() method, which is called immediately after __init__() , but you shouldn't need to override that method either. Its primary purpose is to call the get() , post() or other relevant method handlers. Generally speaking, however, it's not necessary to override those either.

If you really absolutely must catch the request at the earliest point possible though, then the dispatch method is your best bet.

Your BaseAdminView class seems like this:

class BaseAdminView(View):
    loggedInUser = None

    def dispatch(self, request, *args, **kwargs):
        if request.session['loggedInAdministrator'] is None:
            return redirect('adminlogin')
        else:
            self.loggedInUser = request.session['loggedInAdministrator']
        return super().dispatch(request, *args, **kwargs)

and Dashboard class seems like this:

class Dashboard(BaseAdminView):

    def get(self, request):
        return render(request, 'admin/pages/dashboard.html', {'hello': 'World'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM