简体   繁体   中英

How to use django-cumulus for serving Static files?

I'm trying to use django-cumulus for serving files off Rackspace CloudFiles. I'm currently only trying it on my local dev server, using Django 1.4.2.

I can use cumulus's syncstatic management command to upload all my static assets successfully, but I can't seem to display them on my site with the same settings.

If my relevant settings are:

STATIC_URL = '/static/'
CUMULUS = {
    'USERNAME': 'myusername',
    'API_KEY': 'myapikey',
    'CONTAINER': 'mycontainername',
    'STATIC_CONTAINER': 'mycontainername',
}
DEFAULT_FILE_STORAGE = 'cumulus.storage.CloudFilesStorage'
STATICFILES_STORAGE = 'cumulus.storage.CloudFilesStaticStorage'

then when I run syncstatic all my apps' static files are uploaded into /mycontainername/static/ , as I'd expect. But when I load a page in admin it ignores STATIC_URL and tries to serve assets from URLs like http://uniquekey....r82.cf2.rackcdn.com/path/to/file.css rather than http://uniquekey....r82.cf2.rackcdn.com/static/path/to/file.css .

Also, I can't see how to have my public (non-admin) pages use the static files on CloudFiles, rather than serving them from a local /static/ directory.

Have I missed some crucial setting, or am I doing something else wrong?

I had the same problem. What i did was to

git clone https://github.com/richleland/django-cumulus.git

edit context_processors.py

from django.conf import settings

from cumulus.storage import CloudFilesStorage

def cdn_url(request):
    """
    A context processor to expose the full cdn url in templates.

    """
    cloudfiles_storage = CloudFilesStorage()
    static_url = '/'
    container_url = cloudfiles_storage._get_container_url()
    cdn_url = container_url + static_url

    print {'CDN_URL': cdn_url}

    return {'CDN_URL': cdn_url}

Once you are done, install it with sudo python setup.py install

Do note that context_processors.py from django cumulus is actually quite slow

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