The staticfiles app

    See also

    For an introduction to the static files app and some usage examples, see How to manage static files (e.g. images, JavaScript, CSS). For guidelines on deploying static files, see .

    See staticfiles settings for details on the following settings:

    django.contrib.staticfiles exposes three management commands.

    django-admin 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 command can help show you which files are found.

    On subsequent collectstatic runs (if STATIC_ROOT isn’t empty), files are copied only if they have a modified timestamp greater than the timestamp of the file in STATIC_ROOT. Therefore if you remove an application from INSTALLED_APPS, it’s a good idea to use the option in order to remove stale static files.

    Files are searched by using the enabled finders. The default is to look in all locations defined in and in the 'static' directory of apps specified by the INSTALLED_APPS setting.

    The management command calls the post_process() method of the after each run and passes a list of paths that have been found by the management command. It also receives all command line options of collectstatic. This is used by the by default.

    By default, collected files receive permissions from FILE_UPLOAD_PERMISSIONS and collected directories receive permissions from . If you would like different permissions for these files and/or directories, you can subclass either of the static files storage classes and specify the file_permissions_mode and/or directory_permissions_mode parameters, respectively. For example:

    Then set the setting to 'path.to.MyStaticFilesStorage'.

    Some commonly used options are:

    --noinput , --no-input

    Do NOT prompt the user for input of any kind.

    --ignore PATTERN , -i PATTERN

    Ignore files, directories, or paths matching this glob-style pattern. Use multiple times to ignore more. When specifying a path, always use forward slashes, even on Windows.

    --dry-run , -n

    Do everything except modify the filesystem.

    --clear , -c

    Clear the existing files before trying to copy or link the original file.

    --link , -l

    Create a symbolic link to each file instead of copying.

    --no-post-process

    Don’t call the post_process() method of the configured storage backend.

    --no-default-ignore

    Don’t ignore the common private glob-style patterns 'CVS', '.*' and '*~'.

    For a full list of options, refer to the commands own help by running:

    Linux/MacOS     Windows

    1. $ python manage.py collectstatic --help
    1. ...\> py manage.py collectstatic --help

    Customizing the ignored pattern list

    The default ignored pattern list, ['CVS', '.*', '*~'], can be customized in a more persistent way than providing the --ignore command option at each collectstatic invocation. Provide a custom class, override the ignore_patterns attribute of this class and replace 'django.contrib.staticfiles' with that class path in your INSTALLED_APPS setting:

    1. from django.contrib.staticfiles.apps import StaticFilesConfig
    2. class MyStaticFilesConfig(StaticFilesConfig):
    3. ignore_patterns = [...] # your custom ignore list

    findstatic

    django-admin findstatic staticfile [staticfile ...]

    Searches for one or more relative paths with the enabled finders.

    For example:

    Linux/MacOS     Windows

    1. $ python manage.py findstatic css/base.css admin/js/core.js
    2. Found 'css/base.css' here:
    3. /home/special.polls.com/core/static/css/base.css
    4. /home/polls.com/core/static/css/base.css
    5. Found 'admin/js/core.js' here:
    6. /home/polls.com/src/django/contrib/admin/media/js/core.js
    1. Found 'css/base.css' here:
    2. /home/polls.com/core/static/css/base.css
    3. Found 'admin/js/core.js' here:
    4. /home/polls.com/src/django/contrib/admin/media/js/core.js

    findstatic --first

    Linux/MacOS     Windows

    1. $ python manage.py findstatic css/base.css --first
    2. Found 'css/base.css' here:
    3. /home/special.polls.com/core/static/css/base.css

    This is a debugging aid; it’ll show you exactly which static file will be collected for a given path.

    By setting the --verbosity flag to 0, you can suppress the extra output and just get the path names:

    Linux/MacOS     Windows

    1. $ python manage.py findstatic css/base.css --verbosity 0
    2. /home/special.polls.com/core/static/css/base.css
    3. /home/polls.com/core/static/css/base.css
    1. ...\> py manage.py findstatic css\base.css --verbosity 0
    2. /home/special.polls.com/core/static/css/base.css
    3. /home/polls.com/core/static/css/base.css

    On the other hand, by setting the --verbosity flag to 2, you can get all the directories which were searched:

    Linux/MacOS     Windows

    1. $ python manage.py findstatic css/base.css --verbosity 2
    2. Found 'css/base.css' here:
    3. /home/special.polls.com/core/static/css/base.css
    4. /home/polls.com/core/static/css/base.css
    5. Looking in the following locations:
    6. /home/special.polls.com/core/static
    7. /home/polls.com/core/static
    8. /some/other/path/static
    1. ...\> py manage.py findstatic css\base.css --verbosity 2
    2. Found 'css/base.css' here:
    3. /home/special.polls.com/core/static/css/base.css
    4. /home/polls.com/core/static/css/base.css
    5. Looking in the following locations:
    6. /home/special.polls.com/core/static
    7. /home/polls.com/core/static
    8. /some/other/path/static

    django-admin runserver [addrport]

    Overrides the core runserver command if the staticfiles app is and adds automatic serving of static files. File serving doesn’t run through MIDDLEWARE.

    The command adds these options:

    --nostatic

    Use the --nostatic option to disable serving of static files with the app entirely. This option is only available if the staticfiles app is in your project’s setting.

    Example usage:

    Linux/MacOS     Windows

    1. $ django-admin runserver --nostatic

      --insecure

      Use the --insecure option to force serving of static files with the staticfiles app even if the setting is False. By using this you acknowledge the fact that it’s grossly inefficient and probably insecure. This is only intended for local development, should never be used in production and is only available if the staticfiles app is in your project’s setting.

      --insecure doesn’t work with ManifestStaticFilesStorage.

      Example usage:

      Linux/MacOS     Windows

      1. ...\> django-admin runserver --insecure

      StaticFilesStorage

      class storage.StaticFilesStorage

      A subclass of the FileSystemStorage storage backend that uses the setting as the base file system location and the STATIC_URL setting respectively as the base URL.

      storage.StaticFilesStorage.post_process(paths, \*options*)

      If this method is defined on a storage, it’s called by the management command after each run and gets passed the local storages and paths of found files as a dictionary, as well as the command line options. It yields tuples of three values: original_path, processed_path, processed. The path values are strings and processed is a boolean indicating whether or not the value was post-processed, or an exception if post-processing failed.

      The ManifestStaticFilesStorage uses this behind the scenes to replace the paths with their hashed counterparts and update the cache appropriately.

      class storage.ManifestStaticFilesStorage

      A subclass of the storage backend which stores the file names it handles by appending the MD5 hash of the file’s content to the filename. For example, the file css/styles.css would also be saved as css/styles.55e7cbb9ba48.css.

      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.

      The storage backend automatically replaces the paths found in the saved files matching other saved files with the path of the cached copy (using the method). The regular expressions used to find those paths (django.contrib.staticfiles.storage.HashedFilesMixin.patterns) cover:

      For example, the 'css/styles.css' file with this content:

      1. @import url("../admin/css/base.css");

      …would be replaced by calling the url() method of the ManifestStaticFilesStorage storage backend, ultimately saving a 'css/styles.55e7cbb9ba48.css' file with the following content:

      1. @import url("../admin/css/base.27e20196a850.css");

      You can change the location of the manifest file by using a custom ManifestStaticFilesStorage subclass that sets the argument. For example:

      1. from django.conf import settings
      2. from django.contrib.staticfiles.storage import (
      3. ManifestStaticFilesStorage, StaticFilesStorage,
      4. )
      5. class MyManifestStaticFilesStorage(ManifestStaticFilesStorage):
      6. def __init__(self, *args, **kwargs):
      7. manifest_storage = StaticFilesStorage(location=settings.BASE_DIR)
      8. super().__init__(*args, manifest_storage=manifest_storage, **kwargs)

      Changed in Django 4.0:

      Support for finding paths in JavaScript source map comments was added.

      The manifest_storage argument was added.

      Changed in Django 4.1:

      Support for finding paths in CSS source map comments was added.

      Since static files might reference other static files that need to have their paths replaced, multiple passes of replacing paths may be needed until the file hashes converge. To prevent an infinite loop due to hashes not converging (for example, if 'foo.css' references 'bar.css' which references 'foo.css') there is a maximum number of passes before post-processing is abandoned. In cases with a large number of references, a higher number of passes might be needed. Increase the maximum number of passes by subclassing ManifestStaticFilesStorage and setting the max_post_process_passes attribute. It defaults to 5.

      To enable the ManifestStaticFilesStorage you have to make sure the following requirements are met:

      • the setting is set to 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
      • the DEBUG setting is set to False
      • you’ve collected all your static files by using the management command

      Since creating the MD5 hash can be a performance burden to your website during runtime, staticfiles will automatically store the mapping with hashed names for all processed files in a file called staticfiles.json. This happens once when you run the collectstatic management command.

      storage.ManifestStaticFilesStorage.manifest_strict

      If a file isn’t found in the staticfiles.json manifest at runtime, a ValueError is raised. This behavior can be disabled by subclassing ManifestStaticFilesStorage and setting the manifest_strict attribute to False – nonexistent paths will remain unchanged.

      Due to the requirement of running , this storage typically shouldn’t be used when running tests as collectstatic isn’t run as part of the normal test setup. During testing, ensure that the STATICFILES_STORAGE setting is set to something else like 'django.contrib.staticfiles.storage.StaticFilesStorage' (the default).

      storage.ManifestStaticFilesStorage.file_hash(name, content=None)

      The method that is used when creating the hashed name of a file. Needs to return a hash for the given file name and content. By default it calculates a MD5 hash from the content’s chunks as mentioned above. Feel free to override this method to use your own hashing algorithm.

      ManifestFilesMixin

      class storage.ManifestFilesMixin

      Use this mixin with a custom storage to append the MD5 hash of the file’s content to the filename as ManifestStaticFilesStorage does.

      Changed in Django 4.0:

      The manifest_storage argument was added.

      staticfiles finders has a searched_locations attribute which is a list of directory paths in which the finders searched. Example usage:

      1. from django.contrib.staticfiles import finders
      2. result = finders.find('css/base.css')
      3. searched_locations = finders.searched_locations

      There are a few other helpers outside of the app to work with static files:

      The static files tools are mostly designed to help with getting static files successfully deployed into production. This usually means a separate, dedicated static file server, which is a lot of overhead to mess with when developing locally. Thus, the staticfiles app ships with a quick and dirty helper view that you can use to serve files locally in development.

      views.serve(request, path)

      This view function serves static files in development.

      Warning

      This view will only work if is True.

      That’s because this view is grossly inefficient and probably insecure. This is only intended for local development, and should never be used in production.

      Note

      To guess the served files’ content types, this view relies on the mimetypes module from the Python standard library, which itself relies on the underlying platform’s map files. If you find that this view doesn’t return proper content types for certain files, it is most likely that the platform’s map files are incorrect or need to be updated. This can be achieved, for example, by installing or updating the mailcap package on a Red Hat distribution, mime-support on a Debian distribution, or by editing the keys under HKEY_CLASSES_ROOT in the Windows registry.

      This view is automatically enabled by (with a DEBUG setting set to True). To use the view with a different local development server, add the following snippet to the end of your primary URL configuration:

      1. from django.conf import settings
      2. from django.contrib.staticfiles import views
      3. from django.urls import re_path
      4. if settings.DEBUG:
      5. urlpatterns += [
      6. ]

      Note, the beginning of the pattern (r'^static/') should be your setting.

      Since this is a bit finicky, there’s also a helper function that’ll do this for you:

      urls.staticfiles_urlpatterns()

      This will return the proper URL pattern for serving static files to your already defined pattern list. Use it like this:

      This will inspect your STATIC_URL setting and wire up the view to serve static files accordingly. Don’t forget to set the setting appropriately to let django.contrib.staticfiles know where to look for files in addition to files in app directories.

      Warning

      This helper function will only work if DEBUG is True and your setting is neither empty nor a full URL such as http://static.example.com/.

      That’s because this view is grossly inefficient and probably insecure. This is only intended for local development, and should never be used in production.

      Specialized test case to support ‘live testing’

      class

      This unittest TestCase subclass extends .

      Just like its parent, you can use it to write tests that involve running the code under test and consuming it with testing tools through HTTP (e.g. Selenium, PhantomJS, etc.), because of which it’s needed that the static assets are also published.

      But given the fact that it makes use of the django.contrib.staticfiles.views.serve() view described above, it can transparently overlay at test execution-time the assets provided by the staticfiles finders. This means you don’t need to run before or as a part of your tests setup.