collectstatic. Collects the static files into STATIC_ROOT . Duplicate file names are by default resolved in a similar way to how template resolution works: the file that is first found in one of the specified locations will be used. If you're confused, the findstatic command can help show you which files are found.
Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.
STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.
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 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.
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 for img/icon.png
in each of STATICFILES_DIRS
,AppDirectoriesFinder
will look for img/icon.png
in the static
subfolder in each of your INSTALLED_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:
static
directory somewhere on your webserver or a third-party file storage)/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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With