简体   繁体   中英

Django serving static files

I'm trying to configure my django setup to serve static files in debug and production mode. I want in production mode to be from S3 and in debug mode to be from the local installation. I have the settings as below:

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/'

I'm able to get everything to work except one tiny thing which I'm not able to figure out. Often times my CSS files will have a background url as /images/logo.png . The folder /images/ exists in S3 but for my local it needs to be /static/images/ . I tried to set the S3 URL as /static/ at the end but it wasn't working. Is there anything else i'm missing? How do I get the CSS image urls to redirect to /static/ ?

I read through the source code of s3boto.py . You can set the following property:

AWS_LOCATION = "static"

That variable creates a static folder in your s3. The other properties should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM