为什么 swagger 文档在 PythonAnywhere 域上不起作用?

问题描述 投票:0回答:4

我使用 swagger 在 Django Rest 框架上记录我的 API,并将其部署在 Pythonanywhere 上。 但文档 URL 没有任何响应。

这是我的 urls.py swagger 代码:

from drf_yasg.views import get_schema_view
from drf_yasg import openapi
     schema_view = get_schema_view(
       openapi.Info(
          title="BoardGame API",
          default_version='v1',
          description="Test description",
          terms_of_service="http://****.pythonanywhere.com/",
          contact=openapi.Contact(email="[email protected]"),
          license=openapi.License(name="TEST License"),
       ),
       public=True,
       permission_classes=(permissions.AllowAny,),
    )
    urlpatterns = [
        ...
        path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    ]

和我的设置.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'drf_yasg',
    'rest_framework_swagger',
    'rest_framework_simplejwt',
    'django_extensions',
   ...
]
python django django-rest-framework swagger pythonanywhere
4个回答
2
投票

这是因为您的静态文件尚未加载到服务器上尝试收集静态文件并检查页面检查是否加载了CSS


1
投票
  1. 你必须了解Django中的静态

  2. 当您应用它时,所有 UI 相关文件都将在静态文件夹中创建

  3. 然后 swagger UI 将在 pythonanywhere 上运行


0
投票

静态文件似乎未加载到实时服务器上:
第1步:

LOCAL= False

步骤2:

python manage.py collectstatic 

步骤3:
重置服务器

nginx: sudo systemctl restart nginx
gunicorn: sudo systemctl restart gunicorn

0
投票

以下内容对我有用。

  1. 运行collectstatic命令
  2. 转到网络应用程序页面
  3. 在页面的静态文件:部分中提供静态文件位置
© www.soinside.com 2019 - 2024. All rights reserved.