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 as myapp/
)
What's the point of this? Isn't it just creating a copy of all my static files?
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.
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 ads 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.