当我在 Heroku 上运行collectstatic 时,出现以下错误,因为我相信collect static 正在尝试写入本地文件而不是使用S3。
当我使用相同的 AWS 凭证在本地运行 Collectstatic 时,我的静态文件会正确传输到我的 S3 存储桶。
另一个奇怪的是我在 Heroku 上上传的媒体被正确保存在 S3 中。所以我知道我的 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:
最佳答案
我发现了我的错误,这很愚蠢。事实证明,我的静态文件目录在我的 gitignore 和 slugignore 文件中都被忽略了。
教训,不要盲目复制别人的 gitignore 和 slugignore 文件...
关于python - 使用 django-storages/boto 在本地收集静态作品,但在 Heroku 上没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28795446/