Home > Enterprise >  Heroku & AWS - Error while running '$ python manage.py collectstatic --noinput'
Heroku & AWS - Error while running '$ python manage.py collectstatic --noinput'

Time:11-03

Trying to push my code from github to Heroku after setting up S3 for statics. I am using Django.

I tried different solutions I found on here, but no success.

Here are the different things I did.

  1. Removed whitenoise which apparently is incompatible with django-storages;

  2. I also tested the following combowhich returned no error.

    python manage.py collectstatic

    python manage.py test

I did manage to push the code to heroku with $ heroku config:set DISABLE_COLLECTSTATIC=1 but obviousy statics are not there.

Finally, a post suggested to run heroku run 'bower install --config.interactive=false;grunt prep;python manage.py collectstatic --noinput', but this only results in an error message for me C:\Program' is not recognized as an internal or external command,operable program or batch file.

Code wise, I have a file called Settings, in which I have base.py, prod.py and dev.py.

base.py

import os
from pathlib import Path
import django_heroku
import dj_database_url
from decouple import config
from dotenv import load_dotenv, find_dotenv


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent



# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "crispy_forms",
    'main.apps.MainConfig',
    
    'django_extensions',
    'storages',
    
    'import_export',
    'django.contrib.sites',
    'fontawesomefree',


]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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 = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
                'django.template.context_processors.request',
            ],
            'libraries' : {
                'staticfiles': 'django.templatetags.static', 
                }
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'

load_dotenv(find_dotenv())

DATABASES = {'default': dj_database_url.config(default='sqlite:///db.sqlite3',conn_max_age=600)}


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

DATA_UPLOAD_MAX_NUMBER_FIELDS = 10240 # higher than the count of fields

#storing static files
VENV_PATH = os.path.dirname(BASE_DIR)

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

django_heroku.settings(locals())

AUTHENTICATION_BACKENDS = [

]

dev.py

from .base import *

SECRET_KEY = XXX

DEBUG = True

ALLOWED_HOSTS = ['*' ]

STATIC_URL = 'static/'

STATIC_ROOT = BASE_DIR / 'staticfiles'

MEDIA_URL = 'media/'

MEDIA_ROOT = BASE_DIR / 'media'

prod.py

from .base import *
from decouple import config
import django_on_heroku 

SECRET_KEY = config('SECRET_KEY')

DEBUG = False

ALLOWED_HOSTS = ['mysite.herokuapp.com', ]

# Amazon AWS S3 Settings
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')

AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')

AWS_STORAGE_BUCKET_NAME =   ('AWS_STORAGE_BUCKET_NAME')

AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'

AWS_DEFAULT_ACL = 'public-read'

AWS_S3_OBJECT_PARAMETERS ={
    'CacheControl':'max-age=86400'
}

AWS_LOCATION = 'static'

AWS_QUERYSTRING_AUTH = False

AWS_HEADERS = {'Access-Control-Allow-Origin':'*'}

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'

STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/static/'
MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/media/'

# Heroku Logging

DEBUG_PROPAGATE_EXCEPTIONS = True

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'MYAPP': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    }
}

# Heroku Settings
django_on_heroku.settings(locals(), staticfiles=False)
del DATABASES['default']['OPTIONS']['sslmode']

traceback

-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> Using Python version specified in runtime.txt
-----> No change in requirements detected, installing from cache
-----> Using cached install of python-3.10.8
-----> Installing pip 22.2.2, setuptools 63.4.3 and wheel 0.37.1
-----> Installing SQLite3
-----> Installing requirements with pip
-----> $ python manage.py collectstatic --noinput
       Traceback (most recent call last):
         File "/tmp/build_a1a18ada/manage.py", line 22, in <module>
           main()
         File "/tmp/build_a1a18ada/manage.py", line 18, in main
           execute_from_command_line(sys.argv)
         File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
           utility.execute()
         File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
           self.fetch_command(subcommand).run_from_argv(self.argv)
         File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 414, in run_from_argv
           self.execute(*args, **cmd_options)
         File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute
           output = self.handle(*args, **options)
         File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle
           collected = self.collect()
         File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 135, in collect
           handler(path, prefixed_path, storage)
         File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 378, in copy_file
           self.storage.save(prefixed_path, source_file)
         File "/app/.heroku/python/lib/python3.10/site-packages/django/core/files/storage.py", line 57, in save
           name = self._save(name, content)
         File "/app/.heroku/python/lib/python3.10/site-packages/storages/backends/s3boto3.py", line 457, in _save
           obj.upload_fileobj(content, ExtraArgs=params, Config=self._transfer_config)
         File "/app/.heroku/python/lib/python3.10/site-packages/boto3/s3/inject.py", line 725, in object_upload_fileobj
           return self.meta.client.upload_fileobj(
         File "/app/.heroku/python/lib/python3.10/site-packages/boto3/s3/inject.py", line 636, in upload_fileobj
           return future.result()
         File "/app/.heroku/python/lib/python3.10/site-packages/s3transfer/futures.py", line 103, in result
           return self._coordinator.result()
         File "/app/.heroku/python/lib/python3.10/site-packages/s3transfer/futures.py", line 266, in result
           raise self._exception
         File "/app/.heroku/python/lib/python3.10/site-packages/s3transfer/tasks.py", line 139, in __call__
           return self._execute_main(kwargs)
         File "/app/.heroku/python/lib/python3.10/site-packages/s3transfer/tasks.py", line 162, in _execute_main
           return_value = self._main(**kwargs)
         File "/app/.heroku/python/lib/python3.10/site-packages/s3transfer/upload.py", line 758, in _main
           client.put_object(Bucket=bucket, Key=key, Body=body, **extra_args)
         File "/app/.heroku/python/lib/python3.10/site-packages/botocore/client.py", line 507, in _api_call
           return self._make_api_call(operation_name, kwargs)
         File "/app/.heroku/python/lib/python3.10/site-packages/botocore/client.py", line 943, in _make_api_call
           raise error_class(parsed_response, operation_name)
       botocore.errorfactory.NoSuchBucket: An error occurred (NoSuchBucket) when calling the PutObject operation: The specified bucket does not exist
 !     Error while running '$ python manage.py collectstatic --noinput'.
       See traceback above for details.
       You may need to update application code to resolve this error.
       Or, you can disable collectstatic for this application:
          $ heroku config:set DISABLE_COLLECTSTATIC=1
       https://devcenter.heroku.com/articles/django-assets
 !     Push rejected, failed to compile Python app.
 !     Push failed

CodePudding user response:

resolved my problem.

in my case I just forgot config in of AWS_STORAGE_BUCKET_NAME = ('AWS_STORAGE_BUCKET_NAME').

Should have been AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME').

  • Related