나는 api에 대한 django rest 프레임 워크를 배우려고합니다. 설명서를 따르고 모든 가져 오기를 확인했지만 typeerror : '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'
]
}
설정이 잘못 구성되었습니다. 매뉴얼은 다음과 같이 말합니다 .
TokenAuthentication
..
TokenAuthentication
체계 를 사용하려면 을 포함TokenAuthentication
하고 설정에 추가 로 포함하도록 인증 클래스를 구성rest_framework.authtoken
해야INSTALLED_APPS
합니다.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'
],
...
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다