我使用 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',
...
]
这是因为您的静态文件尚未加载到服务器上尝试收集静态文件并检查页面检查是否加载了CSS
你必须了解Django中的静态
当您应用它时,所有 UI 相关文件都将在静态文件夹中创建
然后 swagger UI 将在 pythonanywhere 上运行
静态文件似乎未加载到实时服务器上:
第1步:
LOCAL= False
步骤2:
python manage.py collectstatic
步骤3:
重置服务器
nginx: sudo systemctl restart nginx
gunicorn: sudo systemctl restart gunicorn
以下内容对我有用。