Python 中的 Django 问题 "super() argument 1 must be type, not WSGIRequest" 3
Django problem "super() argument 1 must be type, not WSGIRequest" in Python 3
在使用 class 继承时,Python 3 失败并显示 super() argument 1 must be type, not WSGIRequest
。
我正在使用 Django 2.1.4 和 Python 3.7.0。我试图查看用户是否已经提交了要分析的文件,如果没有,他将被定向到提交页面。我尝试过不使用静态方法,检查它是否真的 Python 3(因为这个问题在 Python 2 上很常见),在超级 class 上我尝试继承自 "object" 同时也继承了 Django 提供的 "View" (因为这解决了 Python 2 super() argument 1 must be type, not None)。
这是超级class,它继承自Django"View"提供的class。
class DatasetRequired(View):
@staticmethod
def get(request):
<redirects the user>
这是基础class
class Estatisticas(DatasetRequired):
@staticmethod
def get(request):
super(request)
<do various other stuff>
我希望基础 class' get
函数在调用时会调用超级 class get
函数并检查用户是否已经提交了文件.
我得到:
TypeError at /estatisticas super() argument 1 must be type, not WSGIRequest
您误解了super()
的使用方法。您将传入当前 class 和一个实例或 class 作为第二个参数,而不是 request
对象。该调用的结果是一个特殊对象,它知道如何通过忽略当前 class.
来查找和绑定父 classes 上的属性
在 staticmethod
上下文中,您必须将当前 class 作为 两个参数传递 :
class Estatisticas(DatasetRequired):
@staticmethod
def get(request):
super(Estatisticas, Estatisticas).get(request)
# <do various other stuff>
我真的不知道你为什么在这里使用 staticmethod
。在处理请求时,会为视图创建一个特殊的实例,因此您通常使用普通实例方法。那时,在 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 有足够的上下文知道 super()
需要 Estatisticas
和 self
作为参数,而您不必为它们命名。
在使用 class 继承时,Python 3 失败并显示 super() argument 1 must be type, not WSGIRequest
。
我正在使用 Django 2.1.4 和 Python 3.7.0。我试图查看用户是否已经提交了要分析的文件,如果没有,他将被定向到提交页面。我尝试过不使用静态方法,检查它是否真的 Python 3(因为这个问题在 Python 2 上很常见),在超级 class 上我尝试继承自 "object" 同时也继承了 Django 提供的 "View" (因为这解决了 Python 2 super() argument 1 must be type, not None)。
这是超级class,它继承自Django"View"提供的class。
class DatasetRequired(View):
@staticmethod
def get(request):
<redirects the user>
这是基础class
class Estatisticas(DatasetRequired):
@staticmethod
def get(request):
super(request)
<do various other stuff>
我希望基础 class' get
函数在调用时会调用超级 class get
函数并检查用户是否已经提交了文件.
我得到:
TypeError at /estatisticas super() argument 1 must be type, not WSGIRequest
您误解了super()
的使用方法。您将传入当前 class 和一个实例或 class 作为第二个参数,而不是 request
对象。该调用的结果是一个特殊对象,它知道如何通过忽略当前 class.
在 staticmethod
上下文中,您必须将当前 class 作为 两个参数传递 :
class Estatisticas(DatasetRequired):
@staticmethod
def get(request):
super(Estatisticas, Estatisticas).get(request)
# <do various other stuff>
我真的不知道你为什么在这里使用 staticmethod
。在处理请求时,会为视图创建一个特殊的实例,因此您通常使用普通实例方法。那时,在 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 有足够的上下文知道 super()
需要 Estatisticas
和 self
作为参数,而您不必为它们命名。