Django 1.8 official document translation: 6-6-4 deployment static file

Time:2021-9-7

Deploy static files

See also

django.contrib.staticfilesFor an introduction to the usage of, seeManage static files (CSS, images)

Deploying static files in an online environment

The basic step of placing a static file into an online environment is simple: run when the static file changescollectstaticCommand, and then arrange the directory of the collected static files(STATIC_ROOT)Move to a static file server. Depending onSTATICFILES_STORAGE, these files may need to be manually moved to a new location orStorageClasspost_processMethods can help you.

Of course, like all deployment tasks, the devil is hidden in the details. The establishment of each online environment will be different, so you need to adjust the basic outline to meet your needs. Here are some common methods that may be helpful.

The web site and static files are on the same server

If your static files and website are located on the same server, the process may be like this:

  • Push your code to the deployed server.

  • On this server, runcollectstaticTo collect all static files toSTATIC_ROOT

  • Configure the web server to host the URL STATIC_URLLowerSTATIC_ROOT。 For example, this isHow to use Apache and Mod_ WSGI to complete it

You may want to automate this process, especially if you have multiple web servers. There are many ways to accomplish this automation, but many Django developers like itFabric

In the following sections, we will demonstrate some sample fabric scripts to automate the deployment of different selected files. The syntax of fabric script is quite simple, but it will not be described here; SeeDocumentation for fabricTo get a complete explanation of its syntax.

Therefore, a fabric script that deploys static files to multiple web servers will probably be:

from fabric.api import *

# Hosts to deploy onto
env.hosts = ['www1.example.com', 'www2.example.com']

# Where your project code lives on the server
env.project_root = '/home/www/myproject'

def deploy_static():
    with cd(env.project_root):
        run('./manage.py collectstatic -v0 --noinput')

Static files are located on a dedicated server

Most large Django sites use a separate web server to store static files — for example, a server that does not run Django. This kind of server usually runs a different type of server — faster but with little functionality. Some common choices are:

Configuring these servers is outside the scope of this document; Review the documentation for each server for instructions.

Since your static file server will not allow Django, you will need to modify the deployment strategy, which will be as follows:

  • Run locally when the static file changescollectstatic

  • Bring your localSTATIC_ROOTPush to the corresponding directory of the static file server. In this step, common choicesrsync, because it only transfers the changed part of the static file.

The following is an overview of the fabric script:

from fabric.api import *
from fabric.contrib import project

# Where the static files get collected locally. Your STATIC_ROOT setting.
env.local_static_root = '/tmp/static'

# Where the static files should go remotely
env.remote_static_root = '/home/www/static.example.com'

@roles('static')
def deploy_static():
    local('./manage.py collectstatic')
    project.rsync_project(
        remote_dir = env.remote_static_root,
        local_dir = env.local_static_root,
        delete = True
    )

The static file is located on a cloud service or CDN

A common strategy is to place static documents on a cloud storage provider, such as Amazon’s S3 and / or a CDN (content delivery network). This allows you to ignore the problem of saving static files and can usually speed up the loading of web pages (especially when using CDN).

When using these services, except that Rsync is not used to transfer your static files to the server, but to the storage provider or CDN, the basic workflow is similar to the above.

There are many ways to implement it, but if the provider has an API, a custom file storage backend will make the process quite simple. If you have written or are using a third-party custom storage backend, you can setSTATICFILES_STORAGETo tellcollectstaticTo use it.

For example, if you are alreadymyproject.storage.S3StorageWrite a back end of S3 storage in. You can use it as follows:

STATICFILES_STORAGE = 'myproject.storage.S3Storage'

Once this is done, all you have to do is runcollectstaticThen your static files will be pushed to S3 by your storage backend. If you need to switch to a different storage provider later, you simply need to modify yourSTATICFILES_STORAGEset up.

For details on how to write these backend, seeWrite a custom storage system。 There are third-party applications that provide storage back ends that support many common file storage APIs. A good entrance isOverview of djangopackages.com

Learn more

aboutdjango.contrib.staticfilesFor the settings, commands, template labels, and other details contained in, seeStaticfiles reference

translator:Django document collaborative translation team, Original:Deploying static files

In this paperCC BY-NC-SA 3.0Please keep the author’s signature and the source of the article.

Django document collaborative translation teamThere is a shortage of staff. Interested friends can join us. It is completely public welfare. Communication group: 467338606.