======================= The ``staticfiles`` app ======================= .. module:: django.contrib.staticfiles :synopsis: An app for handling static files. ``django.contrib.staticfiles`` collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production. .. seealso:: For an introduction to the static files app and some usage examples, see :doc:`/howto/static-files/index`. For guidelines on deploying static files, see :doc:`/howto/static-files/deployment`. .. _staticfiles-settings: Settings ======== See :ref:`staticfiles settings ` for details on the following settings: * :setting:`STORAGES` * :setting:`STATIC_ROOT` * :setting:`STATIC_URL` * :setting:`STATICFILES_DIRS` * :setting:`STATICFILES_STORAGE` * :setting:`STATICFILES_FINDERS` Management Commands =================== ``django.contrib.staticfiles`` exposes three management commands. ``collectstatic`` ----------------- .. django-admin:: collectstatic Collects the static files into :setting:`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 :djadmin:`findstatic` 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 :setting:`INSTALLED_APPS`, it's a good idea to use the :option:`collectstatic --clear` option in order to remove stale static files. Files are searched by using the :setting:`enabled finders `. The default is to look in all locations defined in :setting:`STATICFILES_DIRS` and in the ``'static'`` directory of apps specified by the :setting:`INSTALLED_APPS` setting. The :djadmin:`collectstatic` management command calls the :meth:`~django.contrib.staticfiles.storage.StaticFilesStorage.post_process` method of the ``staticfiles`` storage backend from :setting:`STORAGES` 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 :djadmin:`collectstatic`. This is used by the :class:`~django.contrib.staticfiles.storage.ManifestStaticFilesStorage` by default. By default, collected files receive permissions from :setting:`FILE_UPLOAD_PERMISSIONS` and collected directories receive permissions from :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS`. If you would like different permissions for these files and/or directories, you can subclass either of the :ref:`static files storage classes ` and specify the ``file_permissions_mode`` and/or ``directory_permissions_mode`` parameters, respectively. For example:: from django.contrib.staticfiles import storage class MyStaticFilesStorage(storage.StaticFilesStorage): def __init__(self, *args, **kwargs): kwargs["file_permissions_mode"] = 0o640 kwargs["directory_permissions_mode"] = 0o760 super().__init__(*args, **kwargs) Then set the ``staticfiles`` storage backend in :setting:`STORAGES` setting to ``'path.to.MyStaticFilesStorage'``. Some commonly used options are: .. django-admin-option:: --noinput, --no-input Do NOT prompt the user for input of any kind. .. django-admin-option:: --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. .. django-admin-option:: --dry-run, -n Do everything except modify the filesystem. .. django-admin-option:: --clear, -c Clear the existing files before trying to copy or link the original file. .. django-admin-option:: --link, -l Create a symbolic link to each file instead of copying. .. django-admin-option:: --no-post-process Don't call the :meth:`~django.contrib.staticfiles.storage.StaticFilesStorage.post_process` method of the configured ``staticfiles`` storage backend from :setting:`STORAGES`. .. django-admin-option:: --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: .. console:: $ python manage.py collectstatic --help .. _customize-staticfiles-ignore-patterns: 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:`~django.apps.AppConfig` class, override the ``ignore_patterns`` attribute of this class and replace ``'django.contrib.staticfiles'`` with that class path in your :setting:`INSTALLED_APPS` setting:: from django.contrib.staticfiles.apps import StaticFilesConfig class MyStaticFilesConfig(StaticFilesConfig): 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: .. console:: $ python manage.py findstatic css/base.css admin/js/core.js Found 'css/base.css' here: /home/special.polls.com/core/static/css/base.css /home/polls.com/core/static/css/base.css Found 'admin/js/core.js' here: /home/polls.com/src/django/contrib/admin/media/js/core.js .. django-admin-option:: findstatic --first By default, all matching locations are found. To only return the first match for each relative path, use the ``--first`` option: .. console:: $ python manage.py findstatic css/base.css --first Found 'css/base.css' here: /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: .. console:: $ python manage.py findstatic css/base.css --verbosity 0 /home/special.polls.com/core/static/css/base.css /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: .. console:: $ python manage.py findstatic css/base.css --verbosity 2 Found 'css/base.css' here: /home/special.polls.com/core/static/css/base.css /home/polls.com/core/static/css/base.css Looking in the following locations: /home/special.polls.com/core/static /home/polls.com/core/static /some/other/path/static .. _staticfiles-runserver: ``runserver`` ------------- .. django-admin:: runserver [addrport] :noindex: Overrides the core :djadmin:`runserver` command if the ``staticfiles`` app is :setting:`installed` and adds automatic serving of static files. File serving doesn't run through :setting:`MIDDLEWARE`. The command adds these options: .. django-admin-option:: --nostatic Use the ``--nostatic`` option to disable serving of static files with the :doc:`staticfiles ` app entirely. This option is only available if the :doc:`staticfiles ` app is in your project's :setting:`INSTALLED_APPS` setting. Example usage: .. console:: $ django-admin runserver --nostatic .. django-admin-option:: --insecure Use the ``--insecure`` option to force serving of static files with the :doc:`staticfiles ` app even if the :setting:`DEBUG` 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 :doc:`staticfiles ` app is in your project's :setting:`INSTALLED_APPS` setting. ``--insecure`` doesn't work with :class:`~.storage.ManifestStaticFilesStorage`. Example usage: .. console:: $ django-admin runserver --insecure .. _staticfiles-storages: Storages ======== ``StaticFilesStorage`` ---------------------- .. class:: storage.StaticFilesStorage A subclass of the :class:`~django.core.files.storage.FileSystemStorage` storage backend that uses the :setting:`STATIC_ROOT` setting as the base file system location and the :setting:`STATIC_URL` setting respectively as the base URL. .. method:: storage.StaticFilesStorage.post_process(paths, **options) If this method is defined on a storage, it's called by the :djadmin:`collectstatic` 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 :class:`~django.contrib.staticfiles.storage.ManifestStaticFilesStorage` uses this behind the scenes to replace the paths with their hashed counterparts and update the cache appropriately. ``ManifestStaticFilesStorage`` ------------------------------ .. class:: storage.ManifestStaticFilesStorage A subclass of the :class:`~django.contrib.staticfiles.storage.StaticFilesStorage` 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 :meth:`~django.contrib.staticfiles.storage.StaticFilesStorage.post_process` method). The regular expressions used to find those paths (``django.contrib.staticfiles.storage.HashedFilesMixin.patterns``) cover: * The `@import`_ rule and `url()`_ statement of `Cascading Style Sheets`_. * `Source map`_ comments in CSS and JavaScript files. Subclass ``ManifestStaticFilesStorage`` and set the ``support_js_module_import_aggregation`` attribute to ``True``, if you want to use the experimental regular expressions to cover: * The `modules import`_ in JavaScript. * The `modules aggregation`_ in JavaScript. For example, the ``'css/styles.css'`` file with this content: .. code-block:: css @import url("../admin/css/base.css"); ...would be replaced by calling the :meth:`~django.core.files.storage.Storage.url` method of the ``ManifestStaticFilesStorage`` storage backend, ultimately saving a ``'css/styles.55e7cbb9ba48.css'`` file with the following content: .. code-block:: css @import url("../admin/css/base.27e20196a850.css"); .. admonition:: Usage of the ``integrity`` HTML attribute with local files When using the optional ``integrity`` attribute within tags like ``