I went through Django REST Swagger 2.1.2 documentation. When I tried with class based views, it was working fine.
我浏览了Django REST Swagger 2.1.2文档。当我尝试使用基于类的视图时,它工作正常。
But i did not find any reference on how to enable swagger for function based views as shown below:
但我没有找到任何关于如何为基于函数的视图启用swagger的参考,如下所示:
@api_view(['GET', 'POST'])
def app_info(request):
...
return response
Most of my views.py
is filled with function based views, just like above.
我的大多数views.py都填充了基于函数的视图,就像上面一样。
Any help on how to enable the same will greatly appreciated. Thanks!
如何启用相同的任何帮助将非常感激。谢谢!
I am using Django: 1.8; Django REST Swagger: 2.1.2; DRF: 3.6.2
我正在使用Django:1.8; Django REST Swagger:2.1.2; DRF:3.6.2
2 个解决方案
#1
11
You should be able to use @renderer_classes
decorator:
你应该可以使用@renderer_classes装饰器:
from rest_framework_swagger import renderers
from rest_framework.decorators import api_view, renderer_classes
@api_view(['GET', 'POST'])
@renderer_classes([renderers.OpenAPIRenderer, renderers.SwaggerUIRenderer])
def app_info(request):
...
return response
Also, it should be worth mentioning, that if you don't want to use this decorator on every view you can specify DEFAULT_RENDERER_CLASSES
in settings
此外,值得一提的是,如果您不想在每个视图上使用此装饰器,您可以在设置中指定DEFAULT_RENDERER_CLASSES
EDIT: It seems it's in the docs after all. Check the very bottom of this page: https://django-rest-swagger.readthedocs.io/en/latest/schema/
编辑:毕竟它似乎在文档中。请查看本页底部:https://django-rest-swagger.readthedocs.io/en/latest/schema/
#2
3
i am not fammiliar with swagger,but you may try to use the decorator in this way:
我并不熟悉招摇,但您可以尝试以这种方式使用装饰器:
class TestView(View):
@api_view(['GET', 'POST'])
def get(self, request):
....
or
要么
from django.utils.decorators import method_decorator
class TestView(View):
@method_decorator(api_view(['GET', 'POST'])
def get(self, request):
....
----------------------------------------------------------------------------
sorry, maybe i have misunderstood your question. according to the document, if you want to enable swagger in class based view. there is example:
对不起,也许我误解了你的问题。根据文档,如果你想在基于类的视图中启用swagger。有例子:
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.schemas import SchemaGenerator
from rest_framework.views import APIView
from rest_framework_swagger import renderers
class SwaggerSchemaView(APIView):
permission_classes = [AllowAny]
renderer_classes = [
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
]
def get(self, request):
generator = SchemaGenerator()
schema = generator.get_schema(request=request)
return Response(schema)
restframework will use these two renderer_classes to render Json and UI.
restframework将使用这两个renderer_classes来呈现Json和UI。
#1
11
You should be able to use @renderer_classes
decorator:
你应该可以使用@renderer_classes装饰器:
from rest_framework_swagger import renderers
from rest_framework.decorators import api_view, renderer_classes
@api_view(['GET', 'POST'])
@renderer_classes([renderers.OpenAPIRenderer, renderers.SwaggerUIRenderer])
def app_info(request):
...
return response
Also, it should be worth mentioning, that if you don't want to use this decorator on every view you can specify DEFAULT_RENDERER_CLASSES
in settings
此外,值得一提的是,如果您不想在每个视图上使用此装饰器,您可以在设置中指定DEFAULT_RENDERER_CLASSES
EDIT: It seems it's in the docs after all. Check the very bottom of this page: https://django-rest-swagger.readthedocs.io/en/latest/schema/
编辑:毕竟它似乎在文档中。请查看本页底部:https://django-rest-swagger.readthedocs.io/en/latest/schema/
#2
3
i am not fammiliar with swagger,but you may try to use the decorator in this way:
我并不熟悉招摇,但您可以尝试以这种方式使用装饰器:
class TestView(View):
@api_view(['GET', 'POST'])
def get(self, request):
....
or
要么
from django.utils.decorators import method_decorator
class TestView(View):
@method_decorator(api_view(['GET', 'POST'])
def get(self, request):
....
----------------------------------------------------------------------------
sorry, maybe i have misunderstood your question. according to the document, if you want to enable swagger in class based view. there is example:
对不起,也许我误解了你的问题。根据文档,如果你想在基于类的视图中启用swagger。有例子:
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.schemas import SchemaGenerator
from rest_framework.views import APIView
from rest_framework_swagger import renderers
class SwaggerSchemaView(APIView):
permission_classes = [AllowAny]
renderer_classes = [
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
]
def get(self, request):
generator = SchemaGenerator()
schema = generator.get_schema(request=request)
return Response(schema)
restframework will use these two renderer_classes to render Json and UI.
restframework将使用这两个renderer_classes来呈现Json和UI。