Reputation: 2464
I followed the top answers here and here and everything works as expected when I collectstatic locally but not when I collectstatic from my heroku instance.
My settings.py
and s3utils.py
files both live in the myproject
folder which is adjacent to the main 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/'
When I run collectstatic locally it successfully copies all my files to s3. Here it is again and you can see it worked:
(venv)robbie@ubuntu:~/git/myproject$ ./manage.py collectstatic --noinput
0 static files copied, 5357 unmodified.
However whenever I deploy and visit my site or try to run collectstatic through heroku I get the following:
(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"
Any help would be greatly appreciated.
Upvotes: 1
Views: 1241
Reputation: 2464
Nevermind, it was a stupid mistake but I'll leave it here in case anyone else stumbles across this, ready...?
Be sure to add myproject/s3utils.py to your git repository after you create it!
I am not a smart man..
Upvotes: 3