0

I'm trying to upload my django app to a production server using Digital Oceans Apps. However I keep getting this error and I've tried everything. I see that in the log below, there's an $ heroku config:set DISABLE_COLLECTSTATIC=1, I registered this app as heroku git before but never put it into production and just now have removed all traces of Heroku. I'm baffled at why I'm getting this error.

Below are settings.py and my Digital Ocean log

#settings.py


"""
Django settings for stockbuckets project.

Generated by 'django-admin startproject' using Django 3.1.1.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import os
from pathlib import Path
import dj_database_url

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



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

# SECURITY WARNING: keep the secret key used in production secret!


SECRET_KEY = os.environ.get('SECRET_KEY_DJANGO_STOCKBUCKETS')


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

ALLOWED_HOSTS = ['0.0.0.0', 'localhost', '127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home.apps.HomeConfig',
    'django_plotly_dash.apps.DjangoPlotlyDashConfig',
    'dpd_static_support',
    'channels',
    'channels_redis',
    'bootstrap4',
    'blog',
]

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_plotly_dash.middleware.BaseMiddleware',
      'django_plotly_dash.middleware.ExternalRedirectionMiddleware',

      'django.middleware.clickjacking.XFrameOptionsMiddleware',

  ]

ROOT_URLCONF = 'stockbuckets.urls'



TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['stockbuckets/templates', 'home/templates', 'blog/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',
            ],
        },
    },
]

WSGI_APPLICATION = 'stockbuckets.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': BASE_DIR / 'db.sqlite3',
#     }
# }

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'sb',
        'USER' : 'postgres',
        'PASSWORD' : '0',
        'HOST' : 'localhost'
    }
}
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)

# Password validation
# https://docs.djangoproject.com/en/3.1/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.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True



CRISPY_TEMPLATE_PACK = 'bootstrap4'

ASGI_APPLICATION = 'stockbuckets.routing.application'
CHANNEL_LAYERS = {
    'default' : {
        'BACKEND': 'channels_redis_core.RedisChannelLayer',
        'CONFIG' : {
            'hosts' : [('127.0.0.1',6379),],
        }
    }

}

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

    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',

    'django_plotly_dash.finders.DashAssetFinder',
    'django_plotly_dash.finders.DashComponentFinder',
    'django_plotly_dash.finders.DashAppDirectoryFinder',
]

PLOTLY_COMPONENTS = [

    # Common components
    'dash_core_components',
    'dash_html_components',
    'dash_renderer',

    # django-plotly-dash components
    'dpd_components',
    # static support if serving local assets
    'dpd_static_support',

    # Other components, as needed
    'dash_bootstrap_components',
    
]




X_FRAME_OPTIONS = 'SAMEORIGIN'

STATICFILES_LOCATION = 'static'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'stockbuckets/static')
]


MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'

# STATIC_URL = '/static/'
# STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
# VENV_PATH = os.path.dirname(BASE_DIR)
# STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')

# Additional locations of static files
# STATICFILES_DIRS = (
#     # Put strings here, like "/home/html/static" or "C:/www/django/static".
#     # Always use forward slashes, even on Windows.
#     # Don't forget to use absolute paths, not relative paths.
#     os.path.join(BASE_DIR, 'static'),
# )

# STATICFILES_FINDERS = (
#     'django.contrib.staticfiles.finders.FileSystemFinder',
#     'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#     # django.contrib.staticfiles.finders.DefaultStorageFinder',
# )


Log:

stockbuckets  | 18:45:18 -----> $ python manage.py collectstatic --noinput
stockbuckets  | 18:45:20        OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
stockbuckets  | 18:45:20        Connecting to the PostgreSQL database...
stockbuckets  | 18:45:20        could not connect to server: Connection refused
stockbuckets  | 18:45:20                Is the server running on host "localhost" (::1) and accepting
stockbuckets  | 18:45:20                TCP/IP connections on port 5432?
stockbuckets  | 18:45:20        could not connect to server: Connection refused
stockbuckets  | 18:45:20                Is the server running on host "localhost" (127.0.0.1) and accepting
stockbuckets  | 18:45:20                TCP/IP connections on port 5432?
stockbuckets  | 18:45:21 
stockbuckets  | 18:45:21  !     Error while running '$ python manage.py collectstatic --noinput'.
stockbuckets  | 18:45:21        See traceback above for details.
stockbuckets  | 18:45:21 
stockbuckets  | 18:45:21        You may need to update application code to resolve this error.
stockbuckets  | 18:45:21        Or, you can disable collectstatic for this application:
stockbuckets  | 18:45:21 
stockbuckets  | 18:45:21           $ heroku config:set DISABLE_COLLECTSTATIC=1
stockbuckets  | 18:45:21 
stockbuckets  | 18:45:21        https://devcenter.heroku.com/articles/django-assets
stockbuckets  | 18:45:21 ERROR: failed to build: exit status 1
stockbuckets  | 18:45:21  ! Build failed (exit code 7)

I see that someone else mentioned the exact problem here, but the answer involves using Docker, (stupid question incoming) which wouldn't that be redundant because I'm using Digital Ocean's new PaaS service? Would greatly appreciate on how I can overcome this error, please let me know if you need more documentation from my side.

andres
  • 1,558
  • 7
  • 24
  • 62

3 Answers3

2

If your app doesn't have static files, you can add the following environment variable : DISABLE_COLLECTSTATIC = 1

1

It looks like your DATABASES setting is configured for local development, i.e. it points to your localhost. You need to specify HOST/PORT/PASSWORD for the database that you're planning to use in the Digital Ocean.

GProst
  • 9,229
  • 3
  • 25
  • 47
  • What if the database I plan on hosting will be in Digital Ocean. Currently waiting to host my django app first, is there any way around this? – andres Oct 17 '20 at 04:24
0

I've had a very similar error.

2022-06-09T22:09:46.838218642Z django.db.utils.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
2022-06-09T22:09:46.841036242Z          Is the server running locally and accepting connections on that socket?
2022-06-09T22:09:46.846117851Z 
2022-06-09T22:09:46.930838187Z  !     Error while running '$ python 
manage.py collectstatic --noinput'.
2022-06-09T22:09:46.930947987Z        See traceback above for details.
2022-06-09T22:09:46.930962141Z 
2022-06-09T22:09:46.931108834Z        You may need to update application code to resolve this error.
2022-06-09T22:09:46.931116378Z        Or, you can disable collectstatic for this application:
2022-06-09T22:09:46.931121208Z 
2022-06-09T22:09:46.931132289Z           $ heroku config:set DISABLE_COLLECTSTATIC=1

I've found out that the problem was not related to collectstatic or even static files. It only happens when App Platform (or heroku under the hoods?) try to run python manage.py collectstatic --noinput

In my case the problem happened because I was trying to access the database in module level. For some reason it fails.

my (simplified) code when the error was happening:

questions = list(Question.objects.all())

def get_all_questions_shuffled():
    """Return shuffled questions."""
    # Shuffle questions and return

The code that worked:

questions = []

def get_all_questions_shuffled():
    """Return shuffled questions."""

    global questions

    if not questions:
        questions = list(Question.objects.all())
    # Shuffle questions and return