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页面,即可完成访问。详情如下:

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业