我找不到将 BASE URL 添加到django
rest swagger
. 我尝试添加
SWAGGER_SETTINGS = {
"base_path": 'localhost:62090/',
}
到settings.py
. 但它不起作用。
我找不到将 BASE URL 添加到django
rest swagger
. 我尝试添加
SWAGGER_SETTINGS = {
"base_path": 'localhost:62090/',
}
到settings.py
. 但它不起作用。
我找不到在 swagger ui 中显示基本 url 的方法,但我可以像这样添加基本 urlurls.py
schema_view = get_swagger_view(title='Pastebin API',url='/pastebin/')
此代码在主机和您指定的 url 之间添加基本 url。
本质上,您需要设置basePath
OpenAPI JSON 响应。要做到这一点django_rest_swagger
:
渲染器.py:
from __future__ import absolute_import, division, print_function, unicode_literals
from django.core.urlresolvers import reverse
from rest_framework_swagger import renderers
class OpenAPIRenderer(renderers.OpenAPIRenderer):
def get_customizations(self):
data = super(OpenAPIRenderer, self).get_customizations()
data['basePath'] = reverse('api-root') # your base url here
return data
视图.py:
from rest_framework import exceptions
from rest_framework.permissions import AllowAny
from rest_framework.renderers import CoreJSONRenderer
from rest_framework.response import Response
from rest_framework.schemas import SchemaGenerator
from rest_framework.views import APIView
from rest_framework_swagger import renderers
from .renderers import OpenAPIRenderer
class SwaggerSchemaView(APIView):
_ignore_model_permissions = True
exclude_from_schema = True
permission_classes = [AllowAny]
renderer_classes = [
CoreJSONRenderer,
OpenAPIRenderer, # your OpenAPIRenderer here
renderers.SwaggerUIRenderer
]
def get(self, request):
generator = SchemaGenerator(
title=title,
url=url,
patterns=patterns,
urlconf=urlconf
)
schema = generator.get_schema(request=request)
if not schema:
raise exceptions.ValidationError(
'The schema generator did not return a schema Document'
)
return Response(schema)
网址.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.SwaggerSchemaView.as_view(), name='api-root'), # your swagger view here
]