python – Django / DRF应用程序收到AttributeError:’function’对象没有属性’get_extra_actions’

我对Web应用程序开发相对较新,正在尝试使用Django.我使用Django Rest Framework实现了一个API,它起初似乎工作正常.

我尝试添加一些引导程序来连接到我的前端(React),现在当我用“python manage.py runserver”运行Django时,我收到了一个AttributeError.我不知道如何调试此错误.

下面是完整的堆栈跟踪,我的views.py和urls.py用于上下文.

堆栈跟踪

Unhandled exception in thread started by <function check_errors.<locals>.wrapper
 at 0x000000560AAAC488>
Traceback (most recent call last):
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\utils\autore
load.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\managem
ent\commands\runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\managem
ent\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\managem
ent\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\checks\
registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\checks\
urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\core\checks\
urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\urls\resolve
rs.py", line 397, in check
    for pattern in self.url_patterns:
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\utils\functi
onal.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\urls\resolve
rs.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\utils\functi
onal.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\urls\resolve
rs.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\aidancain\envs\usaproject\lib\importlib\__init__.py", line 126,
 in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\ATC\envs\usaproject\Scripts\BaldEagle\usabaseball\urls.py", lin
e 24, in <module>
    path('api/', include('api.urls')),
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\django\urls\conf.py
", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\aidancain\envs\usaproject\lib\importlib\__init__.py", line 126,
 in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\ATC\envs\usaproject\Scripts\BaldEagle\api\urls.py", line 16, in
 <module>
    urlpatterns += router.urls
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\rest_framework\rout
ers.py", line 101, in urls
    self._urls = self.get_urls()
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\rest_framework\rout
ers.py", line 363, in get_urls
    urls = super(DefaultRouter, self).get_urls()
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\rest_framework\rout
ers.py", line 261, in get_urls
    routes = self.get_routes(viewset)
  File "C:\Users\aidancain\envs\usaproject\lib\site-packages\rest_framework\rout
ers.py", line 176, in get_routes
    extra_actions = viewset.get_extra_actions()
AttributeError: 'function' object has no attribute 'get_extra_actions'

api.views.py

from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from rest_framework.response import Response
from rest_framework import status
from rest_framework import generics, mixins
from rest_framework import viewsets
from api.models import Playerbios, Testtrackman
from api.serializers import USASerializer, TrackmanSerializer

# @method_decorator(login_required, name='dispatch')
@csrf_exempt
class USAViewset(viewsets.ModelViewSet):
    queryset = Playerbios.objects.all()
    serializer_class = USASerializer
    lookup_field = 'trackmanid'

# @method_decorator(login_required, name='dispatch')
@csrf_exempt
class TrackmanViewset(viewsets.ModelViewSet):
    queryset = Testtrackman.objects.all()
    serializer_class = TrackmanSerializer
    lookup_field = 'pitchuid'

api.urls.py

from django.conf.urls import url, include
from rest_framework import routers
#from .views import USAView, USAListView
from .views import USAViewset, TrackmanViewset

# urlpatterns = [
#     url('(?P<trackmanid>)/', USAView.as_view(), name='usa-rud-view'),
#     url('', USAListView.as_view(), name='usa-list-view'),
# ]
urlpatterns = [

]
router = routers.DefaultRouter()
router.register(r'bios', USAViewset)
router.register(r'trackman', TrackmanViewset)
urlpatterns += router.urls

解决方法:

您不能将csrf_exempt用作类装饰器.

无论如何,DRF视图都没有csrf保护.你应该删除装饰器.

上一篇:python – peewee:object没有属性_meta


下一篇:python – pip3错误 – ‘_NamespacePath’对象没有属性’sort’