django-rest-framework开发时报错 如下:
。。。。。。
File "/Users/user/Desktop/PythonProject/django_restful_drf/venv/lib/python3.9/site-packages/rest_framework/routers.py", line 236, in get_urls
routes = self.get_routes(viewset)
File "/Users/user/Desktop/PythonProject/django_restful_drf/venv/lib/python3.9/site-packages/rest_framework/routers.py", line 152, in get_routes
extra_actions = viewset.get_extra_actions()
AttributeError: type object 'BookAPIView' has no attribute 'get_extra_actions'
报错分析:
views.py的类视图函数不是继承自视图集ViewSet,而路由使用的是router拼接,所以不支持
解决:
方法1. 把路由改为普通格式:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^books/$', views.BooksAPIView.as_view()),
url(r'^books/(?P\d+)/$' , views.BookAPIView.as_view()),
]
方法2. 视图函数继承自视图集ViewSet:
from rest_framework import viewsets
class BooksAPIView(viewsets):
...
如有不足,欢迎指正!