简体   繁体   English

FastAPI 未加载 static 个文件

[英]FastAPI not loading static files

So, I'm swapping my project from node.js to python FastAPI.所以,我将我的项目从 node.js 换成 python FastAPI。 Everything has been working fine with node, but here it says that my static files are not present, so here's the code:节点一切正常,但这里说我的 static 文件不存在,所以这里是代码:

from fastapi import FastAPI, Request, WebSocket
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

app = FastAPI()

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

@app.get('/')
async def index_loader(request: Request):
    return templates.TemplateResponse('index.html', {"request": request})

The project's structure looks like this:该项目的结构如下所示:
项目结构

Files clearly are where they should be, but when I connect to website the following error is raised:文件显然在它们应该在的位置,但是当我连接到网站时出现以下错误:

←[32mINFO←[0m:     connection closed
←[32mINFO←[0m:     127.0.0.1:54295 - "←[1mGET /img/separator.png HTTP/1.1←[0m" ←[31m404 Not Found←[0m
←[32mINFO←[0m:     127.0.0.1:54296 - "←[1mGET /css/rajdhani.css HTTP/1.1←[0m" ←[31m404 Not Found←[0m
←[32mINFO←[0m:     127.0.0.1:54295 - "←[1mGET /js/pixi.min.js HTTP/1.1←[0m" ←[31m404 Not Found←[0m
←[32mINFO←[0m:     127.0.0.1:54296 - "←[1mGET /js/ease.js HTTP/1.1←[0m" ←[31m404 Not Found←[0m
←[32mINFO←[0m:     127.0.0.1:54298 - "←[1mGET / HTTP/1.1←[0m" ←[32m200 OK←[0m
←[32mINFO←[0m:     127.0.0.1:54298 - "←[1mGET /img/separator.png HTTP/1.1←[0m" ←[31m404 Not Found←[0m
←[32mINFO←[0m:     127.0.0.1:54299 - "←[1mGET /css/rajdhani.css HTTP/1.1←[0m" ←[31m404 Not Found←[0m
←[32mINFO←[0m:     127.0.0.1:54298 - "←[1mGET /js/pixi.min.js HTTP/1.1←[0m" ←[31m404 Not Found←[0m
←[32mINFO←[0m:     127.0.0.1:54299 - "←[1mGET /js/ease.js HTTP/1.1←[0m" ←[31m404 Not Found←[0m

So, basically, any static file that I'm using is missing, and I have no idea what am I doing wrong.所以,基本上,我正在使用的任何 static 文件都丢失了,而且我不知道我做错了什么。 How to fix it?如何解决?

Here:这里:

app.mount("/static", StaticFiles(directory="../static"), name="static")

You mount your static directory under /static path.您将 static 目录安装在/static路径下。 That means, if you want access static files in your html you need to use static prefix, eg <img src="static/img/separator.png"/>这意味着,如果您想访问 html 中的 static 文件,您需要使用static前缀,例如<img src="static/img/separator.png"/>

To link to static files from within your HTML (Jinja2) template, you can use the following example code, as described in Starlette documentation :要从 HTML (Jinja2) 模板中链接到 static 文件,您可以使用以下示例代码,如Starlette 文档中所述:

<link href="{{ url_for('static', path='/css/rajdhani.css') }}" rel="stylesheet">
folder = os.path.dirname(__file__)
app.mount("/static", StaticFiles(directory=folder+"/../static",html=True), name="static")

works.作品。 Somehow using directory="../static" does not work, the full path is required when pointing outside of your FastAPI project.以某种方式使用directory="../static"不起作用,指向 FastAPI 项目外部时需要完整路径。 Then html=True is also needed for serving html files correctly.然后还需要html=True才能正确提供 html 个文件。 See https://www.starlette.io/staticfiles/ .请参阅https://www.starlette.io/staticfiles/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM