A-A+
FastAPI 静态目录 静态文件 StaticFiles 加载详解

【注意:此文章为博主原创文章!转载需注意,请带原文链接,至少也要是txt格式!】
最近在详细研究FastAPI,其中设计加载静态文件,但是今天加载的过程中不想按照官网的样式规规矩矩的,想着可以不可以自定义路径,显示伪静态什么的,然后就各种尝试,但是始终报错,弄的一脸懵逼。不过最终这个问题还是解决了,关于这个问题我放在文章最后了。
下面开始讲解
app.mount('/aaaaa', StaticFiles(directory='bbbbbbb'), name='static')
详解app.mout()函数的几个参数:
第一个“/aaaaa”和网址有关,如“http://127.0.0.1/aaaaa/”再加文件子路径。
第二个 StaticFiles(directory="bbbbbbb"),指的是你真实的静态文件存放的目录,是相对路径,可以有多级子路径
第三个 name="static",python3.9 这个不可以省略,这各主要是模板中调用,如下面的代码,url_for括号内的第一个static就是这里的名字。后面的路径加上第二个参数的路径就是文件的完整相对路径。
<link href="{{%20url_for('static',%20path='/css/bootstrap.min.css')%20}}" rel="stylesheet">
这里再说一些,学习这个问题的过程中爆出的异常问题。
最后换成官网的页报错,报错如下:
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "F:\hk\python\tools\Python39\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 369, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "F:\hk\python\tools\Python39\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 59, in __call__
return await self.app(scope, receive, send)
File "F:\hk\python\tools\Python39\lib\site-packages\fastapi\applications.py", line 199, in __call__
await super().__call__(scope, receive, send)
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc from None
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc from None
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\routing.py", line 580, in __call__
await route.handle(scope, receive, send)
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\routing.py", line 241, in handle
await self.app(scope, receive, send)
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\routing.py", line 52, in app
response = await func(request)
File "F:\hk\python\tools\Python39\lib\site-packages\fastapi\routing.py", line 216, in app
raw_response = await run_endpoint_function(
File "F:\hk\python\tools\Python39\lib\site-packages\fastapi\routing.py", line 149, in run_endpoint_function
return await dependant.call(**values)
File "F:\hk\python\fastapi_xuexi\FastAPI_6_bootstrap\main.py", line 30, in main
return templates.TemplateResponse('login.html', {'request': Request})
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\templating.py", line 81, in TemplateResponse
return _TemplateResponse(
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\templating.py", line 27, in __init__
content = template.render(context)
File "F:\hk\python\tools\Python39\lib\site-packages\jinja2\environment.py", line 1304, in render
self.environment.handle_exception()
File "F:\hk\python\tools\Python39\lib\site-packages\jinja2\environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "templates\login.html", line 10, in top-level template code
<link rel="icon" href="{{%20url_for('static',%20path='/imgs/favicon.ico')%20}}">
File "F:\hk\python\tools\Python39\lib\site-packages\starlette\templating.py", line 59, in url_for
return request.url_for(name, **path_params)
TypeError: url_for() missing 1 required positional argument: 'name'
就这个原因,排查了好久,怎么都没搞懂,始终以为是我模板有问题,最后发现不是,是我Python代码有问题,代码如下:
1 2 3 4 5 6 7 8 9 | @app.get("/") async def main(request: Request): return templates.TemplateResponse('login.html', {'request': <span style="color: #ff0000;">Request</span>}) ##正确应该小写request if __name__ == "__main__": import uvicorn uvicorn.run(app, host='127.0.0.1', port=80) |
没错,问题就出在Request,正确应该小写。
文章参考:
https://fastapi.tiangolo.com/zh/tutorial/static-files/
https://www.starlette.io/staticfiles/
布施恩德可便相知重
微信扫一扫打赏
支付宝扫一扫打赏