python - 我得到“模块”对象在 Django Rest Framework 中不可调用
问题描述
我正在尝试为 api 学习 django rest 框架。我正在关注文档并检查了所有导入,但我收到了 typeerror: 'module' object is not callable
视图.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
序列化程序.py
from rest_framework import serializers
from .models import CategoryModel
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = CategoryModel
field = ['name', 'description']
网址.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
设置.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
,并另外包含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'
],
...
}
推荐阅读
- python - 无法安装 pymqi 或 py3mqi
- azure-devops - 为什么 Newtonsoft.json 12.0.2 nuget 包无法使用 TFS 2017 Update3 中的 Nuget 还原任务进行还原?
- r - 使用 dplyr 一次生成多个列
- javascript - 动态插入元素内的Jquery函数不起作用
- requirejs - ESLint 为 define() 禁用 max-len 规则
- ios - 如何真正阻止音频快速播放
- regex - 如何 URL 文件夹路径正则表达式?
- ruby-on-rails - ActiveAdmin:如何使用文本字段而不是颜色选择器来输入颜色?
- xslt-2.0 - 使用 XSLT2.0 进行连接时删除重复项
- c# - 如何获取当前选定键盘布局的显示名称