0

我按照 drf-yasg2 的文档设置了所有内容,但我收到了这个错误,我找不到导致它的原因。我浏览了文档,从那里没有什么可以使用的。看起来每个响应都回退到TemplateHTMLRenderer,而不是使用 drf-yasg2 ReDoc 或 SwaggerUI 渲染器。

    def render(self, swagger, accepted_media_type=None, renderer_context=None):
        if not isinstance(swagger, Swagger):  # pragma: no cover
            return TemplateHTMLRenderer().render(
                swagger, accepted_media_type, renderer_context
            )

        self.set_context(renderer_context, swagger)
        return render_to_string(
            self.template, renderer_context, renderer_context["request"]
        )

此条件永远不会返回false

if not isinstance(swagger, Swagger):

因为来自djangorestgramework (response.py) 的 Response 类使用self.data而不是作为参数呈现的调用此方法:

ret = renderer.render(self.data, accepted_media_type, context)

我不知道我在这里做错了什么。

在我的项目中,我使用:

  • drf-yasg2==1.18.5
  • djangorestframework==3.8.0
  • Django==2.0.1

网址.py

from rest_framework import permissions
from drf_yasg2.views import get_schema_view
from drf_yasg2 import openapi

schema_view = get_schema_view(
   openapi.Info(
      title="Snippets API",
      default_version='v1',
      description="Test description",
   ),
   public=True,
   permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
    
    url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),

settings.py -> INSTALLED_APPS

INSTALLED_APPS = [
    'rest_framework',
    'myappname',
    'rest_framework.authtoken',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'fcm_django',
    'storages',
    'colorfield',
    'drf_yasg2',
]

错误

Internal Server Error: /swagger/
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 158, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 156, in _get_response
    response = response.render()
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\response.py", line 106, in render
    self.content = self.rendered_content
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\rest_framework\response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\drf_yasg2\renderers.py", line 79, in render
    swagger, accepted_media_type, renderer_context
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\rest_framework\renderers.py", line 168, in render
    template_names = self.get_template_names(response, view)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\rest_framework\renderers.py", line 197, in get_template_names
    'Returned a template response with no `template_name` attribute set on either the view or response'
django.core.exceptions.ImproperlyConfigured: Returned a template response with no `template_name` attribute set on either the view or response
[16/Oct/2020 00:39:23] "GET /swagger/ HTTP/1.1" 500 112354
4

1 回答 1

0

答案可能来晚了,可能不是最好的答案,但您可以做的是放在?format=jsonURL 的末尾。

所以例如 - 127.0.0.1:8000/v1/api/user/random123?format=json

于 2021-10-07T08:37:08.937 回答