南通网站快照优化公司,wordpress importer,深圳建站公司设计,友情链接添加在网站中有什么用FastAPI挂载静态资源
使用场景#xff1a;前后端不分离#xff0c;后端挂载图片#xff0c;css,js等静态资源#xff0c;给客户端响应html页面
首先假设项目根目录为app#xff0c;app目录下的static为存放静态资源的目录
#app/main.py
from fastapi import FastAPIapp…FastAPI挂载静态资源
使用场景前后端不分离后端挂载图片css,js等静态资源给客户端响应html页面
首先假设项目根目录为appapp目录下的static为存放静态资源的目录
#app/main.py
from fastapi import FastAPIapp FastAPI()#挂载静态资源
app.mount(/static, StaticFiles(directoryapp/static), namestatic)如上即可成功挂载
在app目录的view为html页面资源
from fastapi import APIRouter
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponserouter APIRouter()
template Jinja2Templates(rapp/views)router.get(/index, response_classHTMLResponse)
def index(request: Request):return template.TemplateResponse(index.html, context{request: request})