Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Deploy static articles


May 14, 2021 Django


Table of contents


Deploy static files

You can also take a look

For an introduction to using django.contrib.staticfiles, see Manage static files (for example, images, JavaScript, CSS).

Provide static files in production

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.

Provide the site and your static files from the same server

If you want to provide static files from the same server that already serves your site, the process might look something like this:

  • Push your code to the deployment server.
  • On the server, run colectstatic to copy all static files to the STATIC_ROOT.
  • Configure your Web server to STATIC_ROOT files under the URL STATIC_URL. For example, this is a way to use apache mod_wsgi methods.

You may want to automate this process, especially if you have multiple Web servers.

Provide static files from a dedicated server

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:

  • Nginx's
  • Lite version of Apache

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:

  • When your static file changes, run locally in colectstatic.
  • Push your local STATIC_ROOT to the static file server to enter the directory you are serving. rsync is a common choice for this step because it only needs to transfer bits of changed static files.

Static files from a cloud service or CDN service

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.

Learn more

For full details about all the settings, commands, template tags, and other sections contained there, see staticfiles.