我得到 'module' object is not callable in Django Rest Framework
I'm getting 'module' object is not callable in Django Rest Framework
我正在尝试学习 api 的 django rest 框架。我正在按照文档检查所有导入,但出现类型错误:'module' object is not callable
Views.py
from rest_framework import viewsets
from .serializer import CategorySerializer
from .models import CategoryModel
class FirstView(viewsets.ModelViewSet):
queryset = CategoryModel.objects.all().order_by('name')
serializer_class = CategorySerializer
serializers.py
from rest_framework import serializers
from .models import CategoryModel
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = CategoryModel
field = ['name', 'description']
urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register(r'', views.FirstView)
urlpatterns = [
path('', include(router.urls))
]
错误
Internal Server Error: /api/category/
Traceback (most recent call last):
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\views.py", line 492, in dispatch
request = self.initialize_request(request, *args, **kwargs)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\viewsets.py", line 146, in initialize_request
request = super().initialize_request(request, *args, **kwargs)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\views.py", line 394, in initialize_request
authenticators=self.get_authenticators(),
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\views.py", line 272, in get_authenticators
return [auth() for auth in self.authentication_classes]
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\views.py", line 272, in <listcomp>
return [auth() for auth in self.authentication_classes]
TypeError: 'module' object is not callable
这是用于 DjangoRestFramework
的 settings.py
settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authtoken',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
您的设置配置错误。 The manual says:
TokenAuthentication
..
To use the TokenAuthentication
scheme you'll need to configure the authentication classes to include TokenAuthentication
, and additionally include rest_framework.authtoken
in your INSTALLED_APPS
setting:
INSTALLED_APPS = [
...
'rest_framework.authtoken'
]
rest_framework.authtoken
将被放入 INSTALLED_APPS
,而不是 DEFAULT_AUTHENTICATION_CLASSES
。不过,我不完全确定要将文档暗示的内容放入 DEFAULT_AUTHENTICATION_CLASSES
中;最有可能:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
...,
'rest_framework.authentication.TokenAuthentication'
],
...
}
我正在尝试学习 api 的 django rest 框架。我正在按照文档检查所有导入,但出现类型错误:'module' object is not callable
Views.py
from rest_framework import viewsets
from .serializer import CategorySerializer
from .models import CategoryModel
class FirstView(viewsets.ModelViewSet):
queryset = CategoryModel.objects.all().order_by('name')
serializer_class = CategorySerializer
serializers.py
from rest_framework import serializers
from .models import CategoryModel
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = CategoryModel
field = ['name', 'description']
urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register(r'', views.FirstView)
urlpatterns = [
path('', include(router.urls))
]
错误
Internal Server Error: /api/category/
Traceback (most recent call last):
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\views.py", line 492, in dispatch
request = self.initialize_request(request, *args, **kwargs)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\viewsets.py", line 146, in initialize_request
request = super().initialize_request(request, *args, **kwargs)
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\views.py", line 394, in initialize_request
authenticators=self.get_authenticators(),
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\views.py", line 272, in get_authenticators
return [auth() for auth in self.authentication_classes]
File "C:\Users\aashu\.virtualenvs\lcodev-xFWoZVV6\lib\site-packages\rest_framework\views.py", line 272, in <listcomp>
return [auth() for auth in self.authentication_classes]
TypeError: 'module' object is not callable
这是用于 DjangoRestFramework
的 settings.pysettings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authtoken',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}
您的设置配置错误。 The manual says:
TokenAuthentication
..
To use the
TokenAuthentication
scheme you'll need to configure the authentication classes to includeTokenAuthentication
, and additionally includerest_framework.authtoken
in yourINSTALLED_APPS
setting:INSTALLED_APPS = [ ... 'rest_framework.authtoken' ]
rest_framework.authtoken
将被放入 INSTALLED_APPS
,而不是 DEFAULT_AUTHENTICATION_CLASSES
。不过,我不完全确定要将文档暗示的内容放入 DEFAULT_AUTHENTICATION_CLASSES
中;最有可能:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
...,
'rest_framework.authentication.TokenAuthentication'
],
...
}