101👍
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.
61👍
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.
- [Django]-Filtering using viewsets in django rest framework
- [Django]-STATIC_ROOT vs STATIC_URL in Django
- [Django]-How do I perform HTML decoding/encoding using Python/Django?
27👍
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.
- [Django]-How to do SELECT MAX in Django?
- [Django]-How to concatenate strings in django templates?
- [Django]-How to resize the new uploaded images using PIL before saving?
16👍
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.
- [Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist
- [Django]-Itertools.groupby in a django template
- [Django]-Django self-referential foreign key