'What's the point of Django's collectstatic?

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?



Solution 1:[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:[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 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:

  • 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:[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:[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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tzot
Solution 2
Solution 3
Solution 4 aa333