使用django-storages / boto在Heroku上收集静态工作但不在Heroku上收集

时间:2015-03-01 15:40:15

标签: python django heroku amazon-s3 boto

当我在Heroku上运行collectstatic时,我得到以下错误,因为我认为collect static正在尝试写入本地文件而不是使用S3。

当我使用相同的AWS凭证在本地运行collectstatic时,我的静态文件被正确地传输到我的S3存储桶。

另一个奇怪的是我在Heroku上的媒体上传得到了正确的保存。所以我知道我的Django应用程序正好连接到S3。

我已经进入了django shell,我已导入DEFAULT_FILE_STORAGE& STATICFILES_STORAGE并验证他们指向storages.backends.s3boto.S3BotoStorage

我也能够使用boto查看我的存储桶中的文件,

from storages.backends import s3boto
h = s3boto.S3BotoStorage()
h.bucket  # Shows me my bucket name

通过django的交互式shell,我可以列出我的存储桶中的文件和其他内容。

我无法弄清楚为什么在本地运行collectstatic工作但是当我在heroku上运行时,使用相同的凭据,应用程序,环境等并不起作用。但我的媒体上传到S3确实在heroku上工作......?

这是我在heroku上运行collectstatic的错误,

Running `python manage.py collectstatic --noinput` attached to terminal... up, run.2485
/app/.heroku/python/lib/python3.3/site-packages/dotenv.py:53: UserWarning: Not reading .env - it doesn't exist.
  warnings.warn("Not reading {0} - it doesn't exist.".format(dotenv))
Traceback (most recent call last):
  File "manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/management/base.py", line 533, in handle
    return self.handle_noargs(**options)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle_noargs
    collected = self.collect()
  File "/app/.heroku/python/lib/python3.3/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
    for path, storage in finder.list(self.ignore_patterns):
  File "/app/.heroku/python/lib/python3.3/site-packages/django/contrib/staticfiles/finders.py", line 111, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "/app/.heroku/python/lib/python3.3/site-packages/django/contrib/staticfiles/utils.py", line 27, in get_files
    directories, files = storage.listdir(location)
  File "/app/.heroku/python/lib/python3.3/site-packages/django/core/files/storage.py", line 270, in listdir
    for entry in os.listdir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/app/static'

每次我推到heroku时,我都会看到,

remote: -----> Preparing static assets
remote:        Collectstatic configuration error. To debug, run:
remote:        $ heroku run python ./manage.py collectstatic --noinput
remote:

1 个答案:

答案 0 :(得分:4)

我发现了我的错误,这是愚蠢的。事实证明我的gitignore和我的slugignore文件中都忽略了我的静态文件目录。

经验教训,不要盲目地复制其他的gitignore和slugignore文件...