且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django 静态文件应用程序帮助

更新时间:2022-11-04 19:40:32

我恳请您在这里阅读 howto 文档:http://docs.djangoproject.com/en/dev/howto/static-files/

I implore you to read the howto docs here: http://docs.djangoproject.com/en/dev/howto/static-files/

简而言之:STATIC_ROOT 仅在您调用 collectstatic 管理命令时使用.无需将目录添加到 STATICFILES_DIRS 设置中即可为您的静态文件提供服务!

In short: STATIC_ROOT is only used if you call the collectstatic manangement command. It's not needed to add the directory to the STATICFILES_DIRS setting to serve your static files!

在开发过程中(当使用自动服务视图时)staticfiles 会自动在不同位置寻找静态文件(因为你用静态文件的路径调用它的服务"视图).对于此搜索,它将使用所谓的查找程序​​"(在 STATICFILES_FINDERS 设置中定义).

During development (when the automatic serving view is used) staticfiles will automatically look for static files in various locations (because you call its "serve" view with a path to a static file). For this search it'll use the so called "finders" (as defined in the STATICFILES_FINDERS setting).

  1. 默认查找器之一是 AppDirectoriesFinder,它将在您的 INSTALLED_APPS 设置的每个应用程序的/static/"目录中查找.

  1. One of the default finders is the AppDirectoriesFinder, which will look in the "/static/" directory of each of the apps of yours INSTALLED_APPS setting.

另一个默认查找器是 FileSystemFinder,它将查找您在 STATICFILES_DIRS 设置中指定的目录.

Another default finder is the FileSystemFinder, which will look in directories you specify in the STATICFILES_DIRS setting.

顺便说一句,这两种搜索模式都类似于模板加载的工作方式.

BTW, both these search patterns are similar to how template loading works.

运行 collectstatic 命令时将使用相同的技术,不同之处在于它现在将从各个位置收集文件(使用与上述相同的查找器),将找到的文件放入 STATIC_ROOT 中,准备部署.

The same technique will be used when running the collectstatic command, except that it now will collect the files from the various locations (using the same finders as above), putting the found files into STATIC_ROOT, ready for deployment.