【发布时间】:2020-10-08 20:17:13
【问题描述】:
我正在尝试提供 package_docs 目录中的静态文件。当我在浏览器中打开时:
http://127.0.0.1:8001/packages/docs/index.html,页面正在运行。
但我想打开页面:http://127.0.0.1:8001/packages/docs/
没有源文件。并且输出是404 Not Found
app.mount("/packages/docs",
StaticFiles(directory=pkg_resources.resource_filename(__name__, 'package_docs')
),
name="package_docs")
@app.get("/packages/docs/.*", include_in_schema=False)
def root():
return HTMLResponse(pkg_resources.resource_string(__name__, "package_docs/index.html"))
app.include_router(static.router)
app.include_router(jamcam.router, prefix="/api/v1/cams", tags=["jamcam"])
如何更改我的代码?任何建议都会有所帮助。提前谢谢你。
【问题讨论】:
-
@justin-malloy 发布的答案似乎是正确的,您只需在 StaticFiles() 调用中包含 html=True 即可。
标签: python-3.x routes static-files fastapi pkg-resources