相关疑难解决方法(0)

加载index.html的最小fastapi示例

在我的项目文件夹中,我有一个基本index.html文件加上静态文件(js、css)以及我的main.py

from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi import Request

app = FastAPI()

templates = Jinja2Templates(directory="/")
app.mount("/", StaticFiles(directory="/"))

@app.get("/")
def serve_home(request: Request):
    return templates.TemplateResponse("index.html", context= {"request": request}) 
Run Code Online (Sandbox Code Playgroud)

我怎样才能让fastapi在这里工作?我只想index.html在本地主机上提供我的静态文件。没有staticortemplates文件夹有问题吗?

fastapi

35
推荐指数
2
解决办法
3万
查看次数

标签 统计

fastapi ×1