小编Eri*_*rin的帖子

与 FastAPI 一起使用时无法识别 CSS

我构建了一个 FastAPI 应用程序和一个在直接打开时可以工作的索引、css 和 js 文件,但是在使用 FastAPI 提供服务时我无法显示 css 文件。如果问题是 CORS 问题,我已尝试进行故障排除,我已尝试重新排列文件以使其位于相同或不同的目录中(并相应地重新指定 href 路径),但没有任何效果。这是我此时的目录:

working_directory
??? app
|__ index.html
    ??? style.css
    ??? main.js
Run Code Online (Sandbox Code Playgroud)

像这样设置应用程序主页路线:

file_path = "index.html"

@app.get("/")
async def home():
    return FileResponse(file_path)
Run Code Online (Sandbox Code Playgroud)

我是否还需要在 file_path 变量中提供 css 文件的路径?到目前为止,我尝试过的格式还没有奏效,并且 FastAPI 的错误没有像 Flask 或 Django 那样记录得很好,例如。

我的错误如下: GET http://localhost:8000/style.css net::ERR_ABORTED 404 (Not Found)

我如何让网络应用程序识别我的 style.css 文件?

http-status-code-404 fastapi

6
推荐指数
1
解决办法
1661
查看次数

标签 统计

fastapi ×1

http-status-code-404 ×1