May 14, 2021 Django
You can also take a look
For an introduction to using django.contrib.staticfiles, see Manage static files (for example, images, JavaScript, CSS).
The basic overview of putting static files into production consists of two steps: run commands when collectstatic changes static files, and then schedule the collection of static file directories (STATIC_ROOT) to be moved to static file servers and serviced. Depending STATICFILES_STORAGE, you may need to manually move the file to a new location, otherwise the class post_process method Storage might resolve the issue.
Of course, as with all deployment missions, the details lie with the devil. E ach production setting will be different, so you'll need to adjust the basic contours to suit your needs. Here are some common patterns that might be useful.
If you want to provide static files from the same server that already serves your site, the process might look something like this:
You may want to automate this process, especially if you have multiple Web servers.
Most larger Django sites use separate Web servers (that is, Web servers that do not run Django at the same time) to provide static files. T he server typically runs other types of Web servers - faster, but less functional. Some common options are:
Configuring these servers is outside the scope of this document. Check each server's own documentation for instructions.
Because your static file server does not run Django, you need to modify the deployment policy to look like this:
Another common strategy is to service static files from cloud storage providers such as Amazon S3 and/or CDN (Content Delivery Network). This allows you to ignore the problem of providing static files, and can often make web pages load faster, especially when using CDNs.
With these services, the basic workflow looks similar to the one above, except that rsync needs to transfer static files to a storage provider or CDN instead of being used to transfer static files to the server.
You can do this in a number of ways, but if the provider has an API, you can use the custom file store backend to integrate the CDN with the Django project. If you've written or are using a custom storage backend from a third party, you can be told to use it by setting up STATICFILES_STORAGE storage engine.
For example, if you have already written an S3 storage backend, myproject.storage.S3Storage can use it for:
STATICFILES_STORAGE = 'myproject.storage.S3Storage'
When you're done, all you need to do is run colectstatic, and your static files are pushed to S3 through the storage package. If you need to switch to another storage provider later, you only need to change STATICFILES_STORAGE settings.
For more information on how to write one of these backends, see Write a custom storage system. T here are available third-party applications that provide storage backends for many common file storage APIs. djangopackages.org overview of the work is a good starting point.
For full details about all the settings, commands, template tags, and other sections contained there, see staticfiles.