Python Django 的 collectstatic 有什么意义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34586114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the point of Django's collectstatic?
提问by
This is probably a stupid question, but it's just not clicking in my head.
这可能是一个愚蠢的问题,但它并没有在我的脑海中响起。
In Django, the convention is to put all of your static files (i.e css, js) specific to your app into a folder called static. So the structure would look like this:
在 Django 中,约定是将所有特定于您的应用程序的静态文件(即 css、js)放入一个名为static的文件夹中。所以结构看起来像这样:
mysite/
manage.py
mysite/ --> (settings.py, etc)
myapp/ --> (models.py, views.py, etc)
static/
In mysite/settings.py
I have:
在mysite/settings.py
我有:
STATIC_ROOT = 'staticfiles'
So when I run the command:
所以当我运行命令时:
python manage.py collectstatic
It creates a folder called staticfiles
at the root level (so same directory as myapp/
)
它创建一个staticfiles
在根级别调用的文件夹(与 相同的目录myapp/
)
What's the point of this? Isn't it just creating a copy of all my static files?
这有什么意义?它不只是创建我所有静态文件的副本吗?
采纳答案by bakkal
Collect static files from multiple apps into a single path
将多个应用程序中的静态文件收集到一个路径中
Well, a single Django projectmay use several apps, so while there you only have one myapp
, it may actually be myapp1
, myapp2
, etc
那么,一个Django的项目可以使用多个应用程序,因此虽然你只有一个myapp
,它实际上可能是myapp1
,myapp2
等
By copying them from inside the individual apps into a single folder, you can point your frontend web server (e.g. nginx) to that single folder STATIC_ROOT
and serve static files from a single location, rather than configure your web server to serve static files from multiple paths.
通过将它们从单个应用程序内部复制到单个文件夹中,您可以将前端 Web 服务器(例如 nginx)指向该单个文件夹STATIC_ROOT
并从单个位置提供静态文件,而不是将 Web 服务器配置为从多个路径提供静态文件.
Persistent URLs with ManifestStaticFilesStorage
带有ManifestStaticFilesStorage 的持久 URL
A note about the MD5 hash being appended to the filename for versioning: It's not part of the default behavior of collectstatic
, as settings.STATICFILES_STORAGE
defaults to StaticFilesStorage
(which doesn't do that)
关于将 MD5 哈希附加到文件名以进行版本控制的注释:它不是 的默认行为的一部分collectstatic
,因为settings.STATICFILES_STORAGE
默认为StaticFilesStorage
(不这样做)
The MD5 hash will kick in e.g. if you set it to use ManifestStaticFilesStorage
, which ads that behavior.
MD5 散列将启动,例如,如果您将其设置为 use ManifestStaticFilesStorage
,则广告该行为。
The purpose of this storage is to keep serving the old files in case some pages still refer to those files, e.g. because they are cached by you or a 3rd party proxy server. Additionally, it's very helpful if you want to apply far future Expires headers to the deployed files to speed up the load time for subsequent page visits.
此存储的目的是继续为旧文件提供服务,以防某些页面仍然引用这些文件,例如因为它们被您或第 3 方代理服务器缓存。此外,如果您想将远期的 Expires 标头应用于已部署的文件以加快后续页面访问的加载时间,这将非常有用。
回答by aa333
It's useful when there are multiple django apps within the site.
当站点中有多个 django 应用程序时,它很有用。
collectstatic
will then collect static files from all the apps in one place - so that it could be served up in a production environment.
collectstatic
然后将在一个地方从所有应用程序收集静态文件 - 以便它可以在生产环境中提供。
回答by Mikko Ohtamaa
In the production installation, you want to have persistent URLs. The URL doesn't change unless the file content changes.
在生产安装中,您希望拥有持久的 URL。除非文件内容更改,否则 URL 不会更改。
This is to prevent having clients to have wrong version of CSS or JS file on their computer when opening a web page from Django. Django staticfiles detects file changes and updates URLs accordingly, so that if CSS or JS file changes the web browser downloads the new version.
这是为了防止客户端在从 Django 打开网页时在他们的计算机上有错误版本的 CSS 或 JS 文件。Django staticfiles 检测文件更改并相应地更新 URL,因此如果 CSS 或 JS 文件更改,Web 浏览器会下载新版本。
This is usually achieved by adding MD5 hash to the filename during collectstatic
run.
这通常是通过在collectstatic
运行期间向文件名添加 MD5 哈希来实现的。
Edit: Also see related answer to multiple apps.
编辑:另请参阅多个应用程序的相关答案。
回答by Kos
Django static files can be in many places. A file that is served as /static/img/icon.png
could come from many places. By default:
Django 静态文件可以在很多地方。提供的文件/static/img/icon.png
可能来自许多地方。默认情况下:
FileSystemFinder
will look forimg/icon.png
in each ofSTATICFILES_DIRS
,AppDirectoriesFinder
will look forimg/icon.png
in thestatic
subfolder in each of yourINSTALLED_APPS
. This allows libraries like Django Admin to add their own static files to your app.
FileSystemFinder
将img/icon.png
在每个中寻找STATICFILES_DIRS
,AppDirectoriesFinder
将img/icon.png
在static
您的每个INSTALLED_APPS
. 这允许像 Django Admin 这样的库将它们自己的静态文件添加到您的应用程序中。
Now: this only works if you run manage.py runserver
with DEBUG=1. When you go live, the Django process will no longer serve the static assets. It would be inefficient to use Django for serving these, there are more specialised tools specifically for that.
现在:这仅在您manage.py runserver
使用 DEBUG=1运行时才有效。当您上线时,Django 进程将不再为静态资产提供服务。使用 Django 来提供这些服务是低效的,有更多专门用于此的专门工具。
Instead, you should do something like this:
相反,您应该执行以下操作:
- find all of static files from every app
- build a single directory that contains all of them
- upload them somewhere (a
static
directory somewhere on your webserver or a third-party file storage) - configure your webserver (such as nginx) to serve
/static/*
directly from that directory and redirect any other requests to Django.
- 从每个应用程序中查找所有静态文件
- 构建一个包含所有这些的单个目录
- 将它们上传到某个地方(
static
您的网络服务器或第三方文件存储上的某个目录) - 配置您的网络服务器(例如 nginx)以
/static/*
直接从该目录提供服务并将任何其他请求重定向到 Django。
collectstatic
is a ready-made script that prepares this directory for you, so that you can connect it directly to your deployment script.
collectstatic
是一个现成的脚本,为您准备此目录,以便您可以将其直接连接到您的部署脚本。