[英]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()
需要Estatisticas
和self
作为参数,而无需您命名它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.