Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Join theFastAPI Cloud waiting list 🚀
Follow@fastapi onX (Twitter) to stay updated
FollowFastAPI onLinkedIn to stay updated
Subscribe to theFastAPI and friends newsletter 🎉
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor

Static Files

You can serve static files automatically from a directory usingStaticFiles.

UseStaticFiles

  • ImportStaticFiles.
  • "Mount" aStaticFiles() instance in a specific path.
fromfastapiimportFastAPIfromfastapi.staticfilesimportStaticFilesapp=FastAPI()app.mount("/static",StaticFiles(directory="static"),name="static")

Technical Details

You could also usefrom starlette.staticfiles import StaticFiles.

FastAPI provides the samestarlette.staticfiles asfastapi.staticfiles just as a convenience for you, the developer. But it actually comes directly from Starlette.

What is "Mounting"

"Mounting" means adding a complete "independent" application in a specific path, that then takes care of handling all the sub-paths.

This is different from using anAPIRouter as a mounted application is completely independent. The OpenAPI and docs from your main application won't include anything from the mounted application, etc.

You can read more about this in theAdvanced User Guide.

Details

The first"/static" refers to the sub-path this "sub-application" will be "mounted" on. So, any path that starts with"/static" will be handled by it.

Thedirectory="static" refers to the name of the directory that contains your static files.

Thename="static" gives it a name that can be used internally byFastAPI.

All these parameters can be different than "static", adjust them with the needs and specific details of your own application.

More info

For more details and options checkStarlette's docs about Static Files.


[8]ページ先頭

©2009-2026 Movatter.jp