From the Django Docs:
Deployment django.contrib.staticfiles provides a convenience management command for gathering static files in a single directory so you can serve them easily.
Set the STATIC_ROOT setting to the directory from which you’d like to serve these files, for example:
STATIC_ROOT = "/var/www/example.com/static/"
Run the collectstatic management command:
$ python manage.py collectstatic
This will copy all files from your static folders into the STATIC_ROOT directory.
Use a web server of your choice to serve the files. Deploying static files covers some common deployment strategies for static files.
What's the purpose of copying the files, why not just serve them from the directory they live in within the app?
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.
Y manage.py collectstatic. This command (don't forget to replace "X.Y" with the version of Python your website uses) collects up all your static files from each of your app folders (including the static files for the admin app) and from any other folders you specify in settings.py, and copies them into STATIC_ROOT .
Django also provides a mechanism for collecting static files into one place so that they can be served easily. Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT .
Why not just serve your static
directory? You might use more than one app, and some of your apps may not be under your control. Before the staticfiles
app existed, you then had to either manually copy the static files for all apps to a common directory, upload them to your CDN, or symlink them to the document root of your web server.
The staticfiles app established a convention: put static files for each app under a static
directory and let Django do the work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With