Collect static error while deploying django project to heroku

Viewed 782

I am trying to finish a youtube tutorial by Dennis Ivy and while deploying to heroku get the following error. Already hooked static and database to AWS.

remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "manage.py", line 15, in <module>
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
remote:            self.fetch_command(subcommand).run_from_argv(self.argv)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
remote:            self.execute(*args, **cmd_options)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
remote:            output = self.handle(*args, **options)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
remote:            collected = self.collect()
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 113, in collect
remote:            handler(path, prefixed_path, storage)
remote:          File "/app/.heroku/python/lib/python3.7/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.7/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.7/site-packages/storages/backends/s3boto3.py", line 562, in exists
remote:            self.connection.meta.client.head_object(Bucket=self.bucket_name, Key=name)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/botocore/client.py", line 316, in _api_call
remote:            return self._make_api_call(operation_name, kwargs)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/botocore/client.py", line 599, in _make_api_call
remote:            api_params, operation_model, context=request_context)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/botocore/client.py", line 645, in _convert_to_request_dict
remote:            api_params, operation_model, context)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/botocore/client.py", line 677, in _emit_api_params
remote:            params=api_params, model=operation_model, context=context)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/botocore/hooks.py", line 356, in emit
remote:            return self._emitter.emit(aliased_event_name, **kwargs)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/botocore/hooks.py", line 228, in emit
remote:            return self._emit(event_name, kwargs)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/botocore/hooks.py", line 211, in _emit
remote:            response = handler(**kwargs)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/botocore/handlers.py", line 223, 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...
remote:
remote: !       Push rejected to zakirzeynalov-crm1.
remote:
To https://git.heroku.com/zakirzeynalov-crm1.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/zakirzeynalov-crm1.git'

This is the whole settings file:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'cz=&%f*9(d*zo$_55p=(p)(eki#p$pb^0159-)8k^6$9c3l&_b'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['zakirzeynalov-crm1.herokuapp.com', '127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'accounts.apps.AccountsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'django_filters',

    'storages',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',

    'whitenoise.middleware.WhiteNoiseMiddleware',

    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'crm1.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'crm1.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
db_pass = os.environ.get('db_pass')


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'database_1',
        'USER': 'zakirzeynalov',
        'PASSWORD': db_pass,
        'HOST': 'database-1.csx4yzqz2jb9.eu-central-1.rds.amazonaws.com',
        'PORT': '5432',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATIC_URL = '/static/'

MEDIA_URL = '/images/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')


#SMTP Configuration

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '*********'
EMAIL_HOST_PASSWORD = '*********'


AWS_S3_HOST = 's3.eu-central-1.amazonaws.com'
AWS_S3_REGION_NAME = 'eu-central-1'

crm_aws_id = os.environ.get('crm-aws-id')
crm_aws_key = os.environ.get('crm-aws-key')
crm_bucket_name = os.environ.get('crm-bucket-name')


AWS_ACCESS_KEY_ID = crm_aws_id
AWS_SECRET_ACCESS_KEY = crm_aws_key
AWS_STORAGE_BUCKET_NAME = crm_bucket_name
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

This is the structure of my project(crm1 is main app, accounts secondary. Both contain standard thing inside.):

--crm1 --crm1
       --accounts
       --static --images
                --css
       --manage.py
       --procfile
       --requirements.txt
       --runtime

Tried to solve, but couldn't manage. Any ideas are much appreciated.

1 Answers

from docs : http://whitenoise.evans.io/en/stable/django.html you dont have STATICFILES_STORAGE =

add to your settings.py:

STATICFILES_STORAGE = 
'whitenoise.storage.CompressedStaticFilesStorage'

EDIT: it looks like you are using Whitenoise. Whitenoise allows django to serve its static files. It is different approach if you want to serve them from AWS.

So, you need to remove Whitenoise in order to utilize django-storages. Remove it from settings, middleware, wsgi.py, etc.

Related