Django提供静态文件

时间:2014-06-05 01:14:34

标签: django amazon-s3 django-compressor

我正在尝试将我的django设置配置为在调试和生产模式下提供静态文件。我希望生产模式从S3和调试模式来自本地安装。我有如下设置:

COMPRESS_ENABLED = not DEBUG
COMPRESS_PARSER = 'compressor.parser.LxmlParser'
COMPRESS_CSS_FILTERS = ['compressor.filters.cssmin.CSSMinFilter']
COMPRESS_JS_FILTERS = ['compressor.filters.jsmin.JSMinFilter']
COMPRESS_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
DEFAULT_FILE_STORAGE = 'libs.storages.S3Storage.S3Storage'

AWS_ACCESS_KEY_ID = # Key
AWS_SECRET_ACCESS_KEY = # Secret

AWS_STORAGE_BUCKET_NAME = # Bucket

AWS_QUERYSTRING_AUTH = False

MEDIA_URL = '/media/'
if COMPRESS_ENABLED:
    from boto.s3.connection import SubdomainCallingFormat
    AWS_S3_CALLING_FORMAT = SubdomainCallingFormat()

    COMPRESS_OFFLINE = True
    STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

    # Path for all static files
    STATIC_URL = 'https://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
    STATIC_ROOT = STATIC_URL
else:
    STATIC_ROOT = path.join(PROJECT_ROOT, '..', 'assets')
    STATIC_URL = '/static/'

除了一件我无法弄清楚的小事之外,我能够把一切都搞定。我的CSS文件通常会将后台网址设为/images/logo.png。文件夹/images/存在于S3中,但对于我的本地,它必须是/static/images/。我试图在最后将S3 URL设置为/static/,但它无法正常工作。还有什么我想念的吗?如何将CSS图片网址重定向到/static/

1 个答案:

答案 0 :(得分:1)

我仔细阅读了s3boto.py的源代码。您可以设置以下属性:

AWS_LOCATION = "static"

该变量在s3中创建一个static文件夹。其他属性应该是:

STATIC_URL = 'https://%s.s3.amazonaws.com/static/' % AWS_STORAGE_BUCKET_NAME
STATIC_ROOT = STATIC_URL