在我的项目文件夹中,我有一个基本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文件夹有问题吗?
我有一个简单的 for 循环迭代项目列表。在某些时候,我知道它会破裂。我怎样才能退回剩余的物品?
for i in [a,b,c,d,e,f,g]:
try:
some_func(i)
except:
return(remaining_items) # if some_func fails i.e. for c I want to return [c,d,e,f,g]
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用我的初始列表并从每次迭代的开始逐一删除项目。但是是否有一些本机 Python 函数用于此或更优雅的功能?