我遵循了最高答案here和here,当我在本地收集静态时,一切都按预期工作,但当我从我的heroku实例中收集静态时,一切都没有。
我的settings.py
和s3utils.py
个文件都位于与myproject
主文件相邻的manage.py
文件夹中。
s3utils.py:
from storages.backends.s3boto import S3BotoStorage
MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media')
StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
settings.py
DEFAULT_FILE_STORAGE = 'myproject.s3utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'myproject.s3utils.StaticRootS3BotoStorage'
AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXX'
AWS_STORAGE_BUCKET_NAME = 'myproject'
AWS_PRELOAD_METADATA = True # necessary to fix manage.py collectstatic command to only upload changed files instead of all files
STATIC_URL = 'https://s3.amazonaws.com/myproject/static/'
ADMIN_MEDIA_PREFIX = 'https://s3.amazonaws.com/myproject/static/admin/'
MEDIA_URL = 'https://s3.amazonaws.com/myproject/media/'
当我在本地运行collectstatic时,它成功地将我的所有文件复制到s3。在这里,它再一次,你可以看到它的工作:
(venv)robbie@ubuntu:~/git/myproject$ ./manage.py collectstatic --noinput
0 static files copied, 5357 unmodified.
但是,每当我部署和访问我的网站或尝试通过heroku运行collectstatic时,我都会得到以下信息:
(venv)robbie@ubuntu:~/git/myproject$ heroku run ./manage.py collectstatic --noinput
Running `./manage.py collectstatic --noinput` attached to terminal... up, run.5714
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 453, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 78, in load_command_class
return module.Command()
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 58, in __init__
self.storage.path('')
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/functional.py", line 202, in inner
self._setup()
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 307, in _setup
self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/files/storage.py", line 290, in get_storage_class
raise ImproperlyConfigured('Error importing storage module %s: "%s"' % (module, e))
django.core.exceptions.ImproperlyConfigured: Error importing storage module myproject.s3utils: "No module named s3utils"
非常感谢任何帮助。
答案 0 :(得分:3)
没关系,这是一个愚蠢的错误,但我会把它留在这里以防万一其他人偶然发现,准备好......?
请务必在创建后将myproject / s3utils.py添加到您的git存储库中!
我不是个聪明人..