django-rest-framework - 无法使用 django rest swagger 运行 Django-rest-framework 应用程序
问题描述
我用 Django-rest-framework 创建了一个后端 api。这是我的views.py -
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')
这是我的 urls.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',
]
这是我的新 urls.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'
如何将 swagger 与现有的 Django-rest-framework 应用程序集成?
解决方案
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'
}