小编Art*_*ski的帖子

如何在 drf_yasg 中将方案从 HTTP 更改为 HTTPS?

我正在使用drf_yasgswagger 文档。当我在 AWS Application Load Balancer 后面发布我的 DRF 应用程序并将侦听器设置为侦听 443 HTTPS 并重定向到运行 DRF 的 EC2 时,swagger UI 试图将请求发送到端点http://example.com/status而不是例如https://example.com/status. 这会产生一个谷歌浏览器错误:

swagger-ui-bundle.js:71 Mixed Content: The page at 'https://example.com/swagger#/status/status_list' was loaded over HTTPS, but requested an insecure resource 'http://example.com/status'. This request has been blocked; the content must be served over HTTPS.
Run Code Online (Sandbox Code Playgroud)

所以我解决这个问题的解决方案是在drf_yasg.views.get_schema_view. 所以我的代码看起来像:

schema_view = get_schema_view(
    openapi.Info(
        title="Server Api Documentation",
        default_version="v1",
        description="",
    url="http://example.com/status"
)

# noinspection PyUnresolvedReferences
swagger_patterns = [
    path("", schema_view.with_ui("swagger", cache_timeout=0), name="schema-swagger-ui"),
Run Code Online (Sandbox Code Playgroud)

我希望能够不显式设置 …

python django django-rest-framework openapi drf-yasg

5
推荐指数
2
解决办法
2347
查看次数