Posted 29-Dec-2020 | 257 | Django
What Django collectstatic command does?
I am stuck with what exactly the use of collectstatic command. According to the documentation of Django, we have to put every static asset like CSS, js, and images. So the structure would look like below.
website/ manage.py website/ --> (settings.py, etc) app1/ --> (models.py, views.py, etc) static/
In website/settings.py I have:
STATIC_ROOT = 'staticfiles'
So when I run the command:
$ python manage.py collectstatic
After the command is executed it creates a new folder at the root level called staticfiles and that contains all the files inside app1/static. Why we need to do this when we are able to access the files directly from with static folder.
According to the Django documentation, I am attaching the details of 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 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 INSTALLED_APPS, it’s a good idea to use the collectstatic --clear 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 STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.
The collectstatic management command calls the post_process() method of the STATICFILES_STORAGE 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 ManifestStaticFilesStorage by default.
By default, collected files receive permissions from FILE_UPLOAD_PERMISSIONS and collected directories receive permissions from FILE_UPLOAD_DIRECTORY_PERMISSIONS. 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:
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 setting to 'path.to.MyStaticFilesStorage'.
Some commonly used options are:
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.
Do everything except modify the filesystem.
Clear the existing files before trying to copy or link the original file.
Create a symbolic link to each file instead of copying.
Don’t call the post_process() method of the configured STATICFILES_STORAGE storage backend.
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:
$ python manage.py collectstatic --help
So basically collectstatic will collect all the static files from all the apps you have within your project and store them into the STATICFILES_STORAGE folder. This way your server can have access to all the static files from within a single directory for your entire project.