What's the point of Django's collectstatic?

PythonDjangoDjango StaticfilesStatic FilesCollectstatic

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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 - Python

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 - Python

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 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser5732453View Question on Stackoverflow
Solution 1 - PythonbakkalView Answer on Stackoverflow
Solution 2 - PythonKosView Answer on Stackoverflow
Solution 3 - PythonMikko OhtamaaView Answer on Stackoverflow
Solution 4 - Pythonaa333View Answer on Stackoverflow