简体   繁体   中英

Django problem "super() argument 1 must be type, not WSGIRequest" in Python 3

While to use class inheritance, Python 3 fails with super() argument 1 must be type, not WSGIRequest .

I'm on Django 2.1.4 and Python 3.7.0. I am trying to see if a user already submitted an file to be analyzed, if not, he is directed to the submission page. I've tried to do not use static methods, check if it's really Python 3 (because this problem is common on Python 2), on the super class I tried to inherit from "object" while also inheriting from "View" provided by Django (because this solves in Python 2 super() argument 1 must be type, not None ).

This is the super class, it inherits from the class provided by Django "View".

class DatasetRequired(View):

    @staticmethod
    def get(request):
        <redirects the user>

This is the base class

class Estatisticas(DatasetRequired):

    @staticmethod
    def get(request):
        super(request)
        <do various other stuff>

I'm expecting that the base class' get function while called will call the super class get function and check if the user already submitted the file.

I get:

TypeError at /estatisticas super() argument 1 must be type, not WSGIRequest

You have misunderstood how to use super() . You'd pass in the current class and an instance or class for the second argument, not the request object. The result of that call is a special object that knows how to find and bind attributes on the parent classes by disregarding the current class.

In a staticmethod context, you would have to pass in the current class as both arguments :

class Estatisticas(DatasetRequired):
    @staticmethod
    def get(request):
        super(Estatisticas, Estatisticas).get(request)
        # <do various other stuff>

I'm really not sure why you are using a staticmethod here. When handling a request, a special instance is created for a view, so you normally use normal instance methods . At that point, in Python 3, you can use super() with no arguments:

class DatasetRequired(View):

    def get(self, request):
        # <redirects the user>

class Estatisticas(DatasetRequired):

    def get(self, request):
        super().get(request)
        # <do various other stuff>

Python has enough context to know that super() needs Estatisticas and self as arguments without you having to name those.

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