我在 Django 项目中使用 django-split-settings(版本 1.3.0)和 drf-spectaulous(版本 0.27.1)时遇到错误。尝试访问 api/schema/swagger-ui/ 处的 Swagger UI 端点时会发生错误。
这是错误消息:
断言错误:在视图
为了提供更多背景信息,以下是我的项目结构的细分:
my_project
|
|_apps
|
|_settings
|
|---components
|
|------drf.py
|
|---environments
|
|---__init__.py
drf.py和init.py的内容如下
REST_FRAMEWORK = {
### some settings
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}
init.py内容::
from split_settings.tools import include
from my_project.settings.components.env import Environment
_base_settings = (
# Load environment settings
"components/env.py",
# Here we should have the order because of dependencies
"components/paths.py",
"components/security.py",
"components/apps.py",
"components/middleware.py",
# Load all other settings
"components/*.py",
# Select the right env:
f"environments/{Environment}.py",
)
# Include settings:
include(*_base_settings)
需要注意的是,在我的 Django 项目中使用单个传统设置文件时不会发生此错误。这是我的 urls.py 文件的相关部分供参考(与 drf-spectaulous 包相同):
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
urlpatterns = [
# YOUR PATTERNS
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
# Optional UI:
path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
]
我尝试过的: 我尝试将 django-split-settings(版本 1.3.0)与 drf-spectaulous(版本 0.27.1)集成,以利用拆分设置,同时仍然为我的 Django 项目生成 Swagger 文档。我在 drf.py 中专门配置了 DEFAULT_SCHEMA_CLASS 以指向 drf_spectaulous.openapi.AutoSchema。
预期结果: 我希望能够访问 api/schema/swagger-ui/ 处的 Swagger UI 端点,并查看为我的项目生成的 API 文档。
实际结果: 但是,在尝试访问 Swagger UI 端点时,我遇到了上述 AssertionError。
restructuring your __init__.py like this to give
_base_settings = (
# Load environment settings
"components/env.py",
# Prioritize DRF-Specific settings
"components/drf.py",
# Then load others
"components/paths.py",
"components/security.py",
"components/apps.py",
"components/middleware.py",
# ... other components
# Select the right env:
f"environments/{Environment}.py",
)
include(*_base_settings)