如何将静态文件部署到分离的计算机

时间:2013-09-27 14:33:31

标签: python django django-staticfiles

我想做这样的事情:

STATIC_ROOT = 'user@123.123.123.132:/home/static-files/'

有没有简单的方法来实现这一目标?

1 个答案:

答案 0 :(得分:3)

您可以使用Fabric收集静态文件并将其部署到远程服务器。

Django documentation中有示例代码。

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

env.roledefs['static'] = ['user@123.123.123.132',]    

# 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/static-files'

@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
    )

然后,您可以通过运行:

来部署静态文件
fab deploy_static