Python错误:此方法仅适用于类,不适用于实例

Jor*_*ani 1 python django view

我正在开发Django应用程序,并在urls.py中定义了以下行

path('organizations/', views.OrganizationListView().as_view(), name='organizations'),
Run Code Online (Sandbox Code Playgroud)

运行服务器时,出现以下错误

lib\site-packages\django\utils\decorators.py", line 11, in __get__
    raise AttributeError("This method is available only on the class, not on instances.")
AttributeError: This method is available only on the class, not on instances.
Run Code Online (Sandbox Code Playgroud)

我知道这一定是因为在对象vs类上调用方法的差异,但是不确定是什么原因导致的以及如何解决。

Dan*_*man 5

您要在此处实例化类,而不应该这样做。它应该是:

path('organizations/', views.OrganizationListView.as_view(), name='organizations'),
Run Code Online (Sandbox Code Playgroud)

  • @Jorjani以防万一您错过了它,他在OrganizationListView之后删除了括号。立刻看不出来对我来说是什么变化。 (2认同)