将Django网站部署到Heroku和S3时出现Collectstatic配置错误

时间:2015-02-19 16:32:58

标签: django heroku amazon-s3

我正在尝试将我的Django网站部署到Heroku和Amazon S3上。但是,在输入git push heroku master之后,我得到了这个:

Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote: -----> Installing dependencies with pip
remote: 
remote: -----> Preparing static assets
remote:        Collectstatic configuration error. To debug, run:
remote:        $ heroku run python ./manage.py collectstatic --noinput**
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing... done, 52.0MB
remote: -----> Launching... done, v9
remote:        https://article-django.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/article-django.git
   4514a97..070a1af  master -> master

我输入heroku run python ./manage.py collectstatic --noinput,我得到了这个日志:

Running `python ./manage.py collectstatic --noinput` attached to terminal... up, run.4058
/app/static/
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    settings.INSTALLED_APPS
  File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
   File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 94, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/app/.heroku/python/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/app/django_test/settings.py", line 142, in <module>
AWS_STORAGE_BUCKET_NAME = os.environ['article-deanna']
  File "/app/.heroku/python/lib/python2.7/UserDict.py", line 23, in   __getitem__
    raise KeyError(key)
KeyError: 'article-deanna'

'article-deanna'是我的AWS_STORAGE_BUCKET_NAME的名称,在settings.py中的此片段中指定:

try:
    from local_settings import *
except Exception as e:
    print e.message 

if not DEBUG:
    AWS_STORAGE_BUCKET_NAME = os.environ['article-deanna']
    AWS_ACCESS_KEY_ID = os.environ['(censored)']
    STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
    DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
    S3_URL = 'http://%s.s3.amazonaws.com/assets/' % article-deanna 
    STATIC_URL = S3_URL

我也有我的Procfile:

web: gunicorn django_test.wsgi

这段脚本activate

# added for S3 deployment
export AWS_STORAGE_BUCKET_NAME=article-deanna
export AWS_ACCESS_KEY_ID=(censored)
export AWS_SECRET_ACCESS_KEY=(censored)

我有正确的AWS_STORAGE_BUCKET_NAME,为什么Heroku没有意识到这一点?

1 个答案:

答案 0 :(得分:0)

我认为你必须从名称中获取env var而不是从值。

在这里您将“article-deanna”设置为变量 AWS_STORAGE_BUCKET_NAME

export AWS_STORAGE_BUCKET_NAME='article-deanna'

在这里你必须得到变量

AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']