What's the point of Django's collectstatic?
Solution 1
Collect static files from multiple apps into a single path
Well, a single Django project may use several apps, so while there you only have one myapp
, it may actually be myapp1
, myapp2
, etc
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.
Persistent URLs with ManifestStaticFilesStorage
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)
The MD5 hash will kick in e.g. if you set it to use ManifestStaticFilesStorage
, which adds that behavior.
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.
Solution 2
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:
-
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.
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.
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.
collectstatic
is a ready-made script that prepares this directory for you, so that you can connect it directly to your deployment script.
Solution 3
In the production installation, you want to have persistent URLs. The URL doesn't change unless the file content changes.
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.
This is usually achieved by adding MD5 hash to the filename during collectstatic
run.
Edit: Also see related answer to multiple apps.
Solution 4
It's useful when there are multiple django apps within the site.
collectstatic
will then collect static files from all the apps in one place - so that it could be served up in a production environment.
Admin
Updated on July 21, 2021Comments
-
Admin almost 3 years
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:
mysite/ manage.py mysite/ --> (settings.py, etc) myapp/ --> (models.py, views.py, etc) static/
In
mysite/settings.py
I have: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 asmyapp/
)What's the point of this? Isn't it just creating a copy of all my static files?
-
doniyor over 8 yearsGood one! didnot know it
-
Kos over 8 years"usually achieved by adding MD5 hash" you mean ManifestStaticFilesStorage? Nice, I haven't seen that
-
bakkal over 8 yearsI think by default there is no MD5 hashing going on, since
settings.STATICFILES_STORAGE
defaults toStaticFilesStorage
, so the MD5 will kick in only after you e.g. set it toManifestStaticFilesStorage
, am I correct? -
babygame0ver over 6 yearsyou want to say that it will be easy to find for the web server to serve the static content
-
lapin about 5 years@MikkoOhtamaa But then how does the frontend app know to which file name it should link since the latter always changes ?
-
Mikko Ohtamaa about 5 years@lapin Good question! Naturally there needs to exist 1) mapping to the latest versions and 2) way to communicate this. Usually it is stored in a file somewhere and then when you ask "which full URL for latest version of X" it gives you out a result.
-
Darien Marks almost 3 yearsUpdated link for how Django finds static files: come from many places