简体   繁体   English

Python 3 中的 Django 问题“super() 参数 1 必须是类型,而不是 WSGIRequest”

[英]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 .虽然要使用类继承,但 Python 3 失败, super() argument 1 must be type, not WSGIRequest

I'm on Django 2.1.4 and Python 3.7.0.我在 Django 2.1.4 和 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 ).我试图不使用静态方法,检查它是否真的是 Python 3(因为这个问题在 Python 2 上很常见),在我尝试从“对象”继承的超类上,同时也从 Django 提供的“视图”继承(因为这在 Python 2 super() 中解决了参数 1 must be type 而不是 None )。

This is the super class, it inherits from the class provided by Django "View".这是超类,它继承自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.我期望基类的get函数在调用时会调用超类get函数并检查用户是否已经提交了文件。

I get:我得到:

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

You have misunderstood how to use super() .您误解了如何使用super() You'd pass in the current class and an instance or class for the second argument, not the request object.您将传入当前类和第二个参数的实例或类,而不是request对象。 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 :staticmethod上下文中,您必须将当前类作为两个参数传入:

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.我真的不知道你为什么在这里使用staticmethod 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:此时,在 Python 3 中,您可以使用不带参数的super()

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. Python 有足够的上下文来知道super()需要Estatisticasself作为参数,而无需您命名它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 A Python Inheritance 问题:TypeError:super() 参数 1 必须是类型,而不是无 - A Python Inheritance Problem: TypeError: super() argument 1 must be type, not None TypeError:super()参数1必须是类型,而不是int(Python) - TypeError: super() argument 1 must be type, not int (Python) super()参数1必须是类型,而不是无 - super() argument 1 must be type, not None class 上的 python 装饰器:TypeError:super() 参数 1 必须是类型,而不是 function - python decorator on class: TypeError: super() argument 1 must be type, not function django-admin:当覆盖保存方法时,“super()参数1必须是type,而不是None” - django-admin: “super() argument 1 must be type, not None” when overriding save method TypeError: super() 参数 1 必须是类型,而不是 MagicMock - azure-devops python package - TypeError: super() argument 1 must be type, not MagicMock - azure-devops python package Django装饰器获取WSGIRequest而不是预期的函数参数 - Django decorator getting a WSGIRequest and not the expected function argument 参数 1 必须是类型,而不是 pygame.surface on super() 调用 - Argument 1 must be type, not pygame.surface on super() call Python - TypeError: super(type, obj): obj must be an instance or subtype of type? - Python - TypeError: super(type, obj): obj must be an instance or subtype of type? Django Python str()参数2必须是str,而不是int - Django Python str() argument 2 must be str, not int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM