'Why is heroku not accepting my "git push heroku master"?
So I'm currently trying to deploy my django app to heroku, and I'm getting an error. On my app I serve both static and media files from an AWS S3 bucket, and locally everything is working fine, but when I try to deploy to heroku I get this error:
remote: -----> $ python manage.py collectstatic --noinput
remote: Traceback (most recent call last):
remote: File "/tmp/build_a4a811fd/manage.py", line 22, in
<module>
remote: main()
remote: File "/tmp/build_a4a811fd/manage.py", line 18, in
main
remote: execute_from_command_line(sys.argv)
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/django/core/management/__init__.py", line 419, in
execute_from_command_line
remote: utility.execute()
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/django/core/management/__init__.py", line 413, in
execute
remote:
self.fetch_command(subcommand).run_from_argv(self.argv)
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/django/core/management/base.py", line 354, in
run_from_argv
remote: self.execute(*args, **cmd_options)
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/django/core/management/base.py", line 398, in execute
remote: output = self.handle(*args, **options)
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/django/contrib/staticfiles/management/commands/
collectstatic. py", line 187, in handle
remote: collected = self.collect()
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/django/contrib/staticfiles/management/commands/
collectstatic.py", line 114, in collect
remote: handler(path, prefixed_path, storage)
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/django/contrib/staticfiles/management/commands/
collectstatic.py", line 338, in copy_file
remote: if not self.delete_file(path, prefixed_path,
source_storage):
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/django/contrib/staticfiles/management/commands/
collectstatic.py", line 248, in delete_file
remote: if self.storage.exists(prefixed_path):
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/storages/backends/s3boto3.py", line 469, in exists
remote:
self.connection.meta.client.head_object(Bucket=self.bucket_name,
Key=name)
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/botocore/client.py", line 395, in _api_call
remote: return self._make_api_call(operation_name,
kwargs)
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/botocore/client.py", line 695, in _make_api_call
remote: request_dict = self._convert_to_request_dict(
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/botocore/client.py", line 743, in
_convert_to_request_dict
remote: api_params = self._emit_api_params(
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/botocore/client.py", line 772, in _emit_api_params
remote: self.meta.events.emit(
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/botocore/hooks.py", line 357, in emit
remote: return self._emitter.emit(aliased_event_name,
**kwargs)
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/botocore/hooks.py", line 228, in emit
remote: return self._emit(event_name, kwargs)
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/botocore/hooks.py", line 211, in _emit
remote: response = handler(**kwargs)
remote: File "/app/.heroku/python/lib/python3.10/site-
packages/botocore/handlers.py", line 238, in validate_bucket_name
remote: if not VALID_BUCKET.search(bucket) and not
VALID_S3_ARN.search(bucket):
remote: TypeError: expected string or bytes-like object
remote:
remote: ! Error while running '$ python manage.py
collectstatic --noinput'.
remote: See traceback above for details.
remote:
remote: You may need to update application code to resolve
this error.
remote: Or, you can disable collectstatic for this
application:
remote:
remote: $ heroku config:set DISABLE_COLLECTSTATIC=1
remote:
remote: https://devcenter.heroku.com/articles/django-assets
remote: ! Push rejected, failed to compile Python app.
remote:
remote: ! Push failed
remote: Verifying deploy...
These are my settings.py:
PRODUCTION = True
if PRODUCTION:
# AWS S3 SETTINGS
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME')
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_DEFAULT_ACL = 'public-read'
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_HEADERS = {
'Access-Control-Allow-Origin': '*',
}
AWS_QUERYSTRING_AUTH = False
AWS_LOCATION = 'static'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'joaoLina.storage_backend.MediaStorage'
MEDIA_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, 'media')
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
I read that sometimes this problem happens because STATIC_ROOT is not set, but that is not the case. And I know I could just do:
$ heroku config:set DISABLE_COLLECTSTATIC=1
but this way the problem will still be there.
I did locally:
python3 manage.py collectstatic
python3 manage.py test
and everything went just fine with 0 issues.
So I really don't know where is the problem, if someone can help I would be grateful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
