Django rest framework 无法使用Django rest swagger运行Django rest framework应用程序
我已经用Django rest框架创建了一个后端api。这是我的观点-Django rest framework 无法使用Django rest swagger运行Django rest framework应用程序,django-rest-framework,swagger,Django Rest Framework,Swagger,我已经用Django rest框架创建了一个后端api。这是我的观点- from django.shortcuts import render from rest_framework import viewsets from rest_framework.response import Response from rest_framework import generics from .models import table1, table2 from .serializers import t
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework import generics
from .models import table1, table2
from .serializers import table_1_Serializer, table_2_Serializer
from rest_framework.permissions import IsAuthenticated #new token
class table_1_View(viewsets.ModelViewSet):
queryset = table1.objects.all()
serializer_class = table_1_Serializer
http_method_names = ['get']
class table_2_View(viewsets.ModelViewSet):
queryset = table2.objects.all()
serializer_class = table_2_Serializer
http_method_names = ['get']
这是我的serialisers.py
from rest_framework import serializers
from .models import table1, table2
class table_1_Serializer(serializers.ModelSerializer):
class Meta:
model = TasteCluster
fields = ('id', 'name', 'dept_id')
class table_2_Serializer(serializers.ModelSerializer):
class Meta:
model = TasteCluster
fields = ('dept_id', 'dept_name', 'dept_strength')
这是我的URL.py
from django.urls import path, include
from . import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register('table_1', views.table_1_View)
router.register('table_2', views.table_2_View, basename='table_2')
urlpatterns = [
path('', include(router.urls))
]
这对我来说很好。现在我正试图将Django Rest Swagger集成到我的项目中
我已经在虚拟环境中安装了Django rest swagger
我已将其添加到已安装的应用程序中。这是我的settings.py-
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_app',
'rest_framework',
'rest_framework_swagger',
#'rest_framework.authtoken',
]
这是我的新URL.py-
from django.urls import path, include
from . import views
from rest_framework import routers
from rest_framework_swagger.views import get_swagger_view
from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer
schema_view = get_swagger_view(title='Test API Documentation')
router = routers.DefaultRouter(
schema_title='Pastebin API',
schema_renderers=[OpenAPIRenderer, SwaggerUIRenderer]
)
router.register('table_1', views.table_1_View)
router.register('table_2', views.table_2_View, basename='table_2')
urlpatterns = [
path('', include(router.urls)),
path('api_documentation', include('rest_framework.urls', namespace='rest_framework'))
]
但当我这样做的时候,我会出错。
当我试着跑的时候
python3 manage.py runserver
我得到这个错误-
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 398, in check
for pattern in self.url_patterns:
File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 579, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 572, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/rahman/Desktop/django_exercise/04project/testapi/testapi/urls.py", line 22, in <module>
path('', include('getapi.urls')),
File "/usr/local/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/rahman/Desktop/django_exercise/04project/testapi/getapi/urls.py", line 14, in <module>
schema_renderers=[OpenAPIRenderer, SwaggerUIRenderer]
File "/usr/local/lib/python3.7/site-packages/rest_framework/routers.py", line 341, in __init__
super().__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'schema_title'
线程django主线程中的异常:
回溯(最近一次呼叫最后一次):
文件“/usr/local/cillar/python/3.7.4/Frameworks/python.framework/Versions/3.7/lib/python3.7/threading.py”,第926行,在bootstrap\u-inner中
self.run()
文件“/usr/local/ceral/python/3.7.4/Frameworks/python.framework/Versions/3.7/lib/python3.7/threading.py”,第870行,正在运行
自我目标(*自我参数,**自我参数)
包装器中的文件“/usr/local/lib/python3.7/site packages/django/utils/autoreload.py”,第54行
fn(*args,**kwargs)
文件“/usr/local/lib/python3.7/site packages/django/core/management/commands/runserver.py”,第117行,在内部运行
self.check(display\u num\u errors=True)
文件“/usr/local/lib/python3.7/site packages/django/core/management/base.py”,第390行,选中
包括部署检查=包括部署检查,
文件“/usr/local/lib/python3.7/site packages/django/core/management/base.py”,第377行,运行检查
返回检查。运行检查(**kwargs)
文件“/usr/local/lib/python3.7/site packages/django/core/checks/registry.py”,运行检查中的第72行
新建错误=检查(应用程序配置=应用程序配置)
文件“/usr/local/lib/python3.7/site packages/django/core/checks/url.py”,第13行,在check\u url\u config中
返回检查\u分解器(分解器)
文件“/usr/local/lib/python3.7/site packages/django/core/checks/url.py”,第23行,在check\u解析器中
返回检查方法()
文件“/usr/local/lib/python3.7/site packages/django/url/resolvers.py”,第398行,选中
对于self.url_模式中的模式:
文件“/usr/local/lib/python3.7/site packages/django/utils/functional.py”,第80行,在__
res=instance.\uuuu dict\uuuu[self.name]=self.func(实例)
文件“/usr/local/lib/python3.7/site packages/django/url/resolvers.py”,第579行,url_模式
patterns=getattr(self.urlconf_模块,“urlpatterns”,self.urlconf_模块)
文件“/usr/local/lib/python3.7/site packages/django/utils/functional.py”,第80行,在__
res=instance.\uuuu dict\uuuu[self.name]=self.func(实例)
文件“/usr/local/lib/python3.7/site packages/django/url/resolvers.py”,第572行,在urlconf_模块中
返回导入_模块(self.urlconf_名称)
文件“/usr/local/ceral/python/3.7.4/Frameworks/python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py”,第127行,在导入模块中
return _bootstrap._gcd_import(名称[级别:],包,级别)
文件“”,第1006行,在\u gcd\u导入中
文件“”,第983行,在_find_和_load中
文件“”,第967行,在“查找”和“加载”中解锁
文件“”,第677行,在\u加载\u解锁
exec_模块中第728行的文件“”
文件“”,第219行,在“调用”中,删除了“帧”
文件“/Users/rahman/Desktop/django_exercise/04project/testapi/testapi/url.py”,第22行,在
路径(“”,include('getapi.url')),
文件“/usr/local/lib/python3.7/site packages/django/url/conf.py”,第34行,include中
urlconf_模块=导入_模块(urlconf_模块)
文件“/usr/local/ceral/python/3.7.4/Frameworks/python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py”,第127行,在导入模块中
return _bootstrap._gcd_import(名称[级别:],包,级别)
文件“”,第1006行,在\u gcd\u导入中
文件“”,第983行,在_find_和_load中
文件“”,第967行,在“查找”和“加载”中解锁
文件“”,第677行,在\u加载\u解锁
exec_模块中第728行的文件“”
文件“”,第219行,在“调用”中,删除了“帧”
文件“/Users/rahman/Desktop/django_exercise/04project/testapi/getapi/url.py”,第14行,在
schema_renderers=[openapirederer,SwaggerUIRenderer]
文件“/usr/local/lib/python3.7/site packages/rest\u framework/routers.py”,第341行,在__
super()
TypeError:\uuuuu init\uuuuuuuuuuuuuuuuu()获取了意外的关键字参数“schema\u title”
如何将swagger与现有的Django rest framework应用程序集成?1。如果你有一个错误,请考虑把它附加到这个问题上,这将有助于弄清到底出了什么问题:我在django rest swagger上也遇到了一些问题,切换到上面的版本,没有遇到任何问题。嘿@IgorBelkov我在我的问题中添加了这个错误。可能是这里描述的问题:由于DRF和DRS版本不兼容。嘿,这有帮助吗?
change you urls.py for rest framework swagger:-
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='API')
urlpatterns = [
path('docs/', schema_view),
]
Then in settings.py file, add the below lines:-
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'
}