fastapi启动后访问docs不显示页面
启动fastapi,运行服务器,可以正常访问接口,但无法访问docs页面,打开开发者工具发现接口调用了一个 js 脚本 和 css 脚本。
这两个脚本是部署在国外,因为这个原因导致我们没法访问了,调整请求地址,请求国内的资源。修改 fastapi 源码包模块下的 fastapi/openapi/docs.py,调整如下请求:
修改内容如下:
swagger_js_url: Annotated[ str, Doc( """ The URL to use to load the Swagger UI JavaScript. It is normally set to a CDN URL. """ ), # ] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui-bundle.js", ] = "https://petstore.swagger.io/swagger-ui-bundle.js", swagger_css_url: Annotated[ str, Doc( """ The URL to use to load the Swagger UI CSS. It is normally set to a CDN URL. """ ), # ] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui.css", ] = "https://petstore.swagger.io/swagger-ui.css",
再次访问docs页面,即可完成访问。详情如下: