【问题标题】:How to use django-cumulus for serving Static files?如何使用 django-cumulus 提供静态文件?
【发布时间】:2012-10-29 18:08:11
【问题描述】:

我正在尝试使用 django-cumulus 从 Rackspace CloudFiles 提供文件。我目前只在本地开发服务器上使用 Django 1.4.2 进行尝试。

我可以使用 cumulus 的 syncstatic 管理命令成功上传我所有的静态资产,但我似乎无法以相同的设置在我的网站上显示它们。

如果我的相关设置是:

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'

然后当我运行syncstatic 时,我的所有应用程序的静态文件都会上传到/mycontainername/static/,正如我所料。但是当我在 admin 中加载页面时,它会忽略 STATIC_URL 并尝试从 http://uniquekey....r82.cf2.rackcdn.com/path/to/file.css 等 URL 提供资产,而不是 http://uniquekey....r82.cf2.rackcdn.com/static/path/to/file.css

另外,我看不到如何让我的公共(非管理员)页面使用 CloudFiles 上的静态文件,而不是从本地 /static/ 目录提供它们。

我是否错过了一些关键设置,或者我做错了什么?

【问题讨论】:

    标签: django rackspace-cloud django-staticfiles


    【解决方案1】:

    我遇到了同样的问题。我所做的是

    git 克隆https://github.com/richleland/django-cumulus.git

    编辑 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}
    

    完成后,使用 sudo python setup.py install 安装它

    请注意,来自 django cumulus 的 context_processors.py 实际上很慢

    【讨论】:

    • 感谢 amdstorm。大概您可以从settings 获得static_url,而不是对其进行硬编码。而且我不确定您是否打算将打印语句留在其中:)
    • 无意将打印声明留在那里!对不起!
    • 我意识到,当你编译静态资源时,静态 url 没有被使用,所以我将它用作 /,错误变量命名我的错误
    猜你喜欢
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 2020-01-29
    • 2013-06-27
    • 2010-10-18
    相关资源
    最近更新 更多