Heroku - Application error - Heroku crashes when DEBUG = False

492 views Asked by At

I just figured out how to host my static files on Whitenoise and they are being hosted perfectly when DEBUG = False. I had to host them this way because in production I kept getting 500 errors because Heroku couldn't host my static files. This works fine now. I am now running into another problem, however. When DEBUG = False in settings.py now my whole Heroku app crashes and shows the page below. This is only when DEBUG = False. I originally thought it was allowed host but I set that value equal to '*' which should allow all just for testing and I still got the same problem. How do I stop Heroku from crashing when DEBUG = False?

enter image description here

settings.py

import django_heroku
from pathlib import Path
import os
from django_quill import quill
from inspect_list.security import *

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ROOT_DIR = os.path.dirname(BASE_DIR)
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
#MEDIA_ROOT = os.path.join(BAS_DIR, 'media')
TIME_INPUT_FORMATS = ['%I:%M %p',]

#Media_URL = '/signup/front_page/sheets/'


# 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 = 'HERE BUT SECURED IN A DIFFERENT FILE'


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

ALLOWED_HOSTS = ['*']



EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Application definition

INSTALLED_APPS = [
    'whitenoise.runserver_nostatic',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'my_app',
    'django_quill',
    'tinymce',
    'ckeditor',

    #'django_extensions',

    'storages',
    #'django-storages',

    'django_filters',
]

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 = 'inspect_list.urls'

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

WSGI_APPLICATION = 'inspect_list.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# 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',
    },
]

#AWS_ACCESS_KEY_ID = 'HERE BUT SECURED IN A DIFFERENT FILE'
#AWS_SECRET_ACCESS_KEY = 'HERE BUT SECURED IN A DIFFERENT FILE'
#AWS_STORAGE_BUCKET_NAME = 'HERE BUT SECURED IN A DIFFERENT FILE'






STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    os.path.join(BASE_DIR, "static\css"),
    os.path.join(BASE_DIR, "static\png"),
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'











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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Cancun'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

MEDIA_URL = '/mediafiles/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
AUTH_USER_MODEL = 'my_app.CustomUser'

LOGIN_REDIRECT_URL = 'front_page'
LOGOUT_REDIRECT_URL = 'login'

django_heroku.settings(locals())

NEW HEROKU LOGS

2022-01-24T22:03:09.914010+00:00 heroku[run.7810]: State changed from starting to up
2022-01-24T22:03:10.174512+00:00 heroku[run.7810]: Awaiting client
2022-01-24T22:03:10.209359+00:00 heroku[run.7810]: Starting process with command `python manage.py migrate`
2022-01-24T22:03:17.972179+00:00 heroku[run.7810]: Process exited with status 0
2022-01-24T22:03:18.023257+00:00 heroku[run.7810]: State changed from up to complete
2022-01-24T22:05:28.409865+00:00 app[api]: Scaled to web@1:Standard-1X by user [email protected]
2022-01-24T22:05:28.445125+00:00 app[api]: Enable allow-multiple-sni-endpoints feature by user [email protected]
2022-01-24T22:05:28.445125+00:00 app[api]: Release v7 created by user [email protected]
2022-01-24T22:05:29.325599+00:00 heroku[web.1]: Restarting
2022-01-24T22:05:29.329500+00:00 heroku[web.1]: State changed from up to starting
2022-01-24T22:05:29.388729+00:00 heroku[web.1]: State changed from starting to down
2022-01-24T22:05:30.092672+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2022-01-24T22:05:30.162282+00:00 app[web.1]: [2022-01-24 17:05:30 -0500] [9] [INFO] Worker exiting (pid: 9)
2022-01-24T22:05:30.162651+00:00 app[web.1]: [2022-01-24 22:05:30 +0000] [4] [INFO] Handling signal: term
2022-01-24T22:05:30.162653+00:00 app[web.1]: [2022-01-24 17:05:30 -0500] [10] [INFO] Worker exiting (pid: 10)
2022-01-24T22:05:30.363086+00:00 app[web.1]: [2022-01-24 22:05:30 +0000] [4] [INFO] Shutting down: Master
2022-01-24T22:05:30.534515+00:00 heroku[web.1]: Process exited with status 0
2022-01-24T22:05:36.704735+00:00 heroku[web.1]: Starting process with command `gunicorn inspect_list.wsgi`
2022-01-24T22:05:37.988598+00:00 app[web.1]: [2022-01-24 22:05:37 +0000] [4] [INFO] Starting gunicorn 20.0.4
2022-01-24T22:05:37.988860+00:00 app[web.1]: [2022-01-24 22:05:37 +0000] [4] [INFO] Listening at: http://0.0.0.0:31028 (4)
2022-01-24T22:05:37.988903+00:00 app[web.1]: [2022-01-24 22:05:37 +0000] [4] [INFO] Using worker: sync
2022-01-24T22:05:37.991874+00:00 app[web.1]: [2022-01-24 22:05:37 +0000] [9] [INFO] Booting worker with pid: 9
2022-01-24T22:05:38.015442+00:00 app[web.1]: [2022-01-24 22:05:38 +0000] [10] [INFO] Booting worker with pid: 10
2022-01-24T22:05:38.090889+00:00 heroku[web.1]: Starting process with command `gunicorn inspect_list.wsgi`
2022-01-24T22:05:39.201349+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2022-01-24T22:05:39.308647+00:00 app[web.1]: [2022-01-24 22:05:39 +0000] [4] [INFO] Starting gunicorn 20.0.4
2022-01-24T22:05:39.308946+00:00 app[web.1]: [2022-01-24 22:05:39 +0000] [4] [INFO] Listening at: http://0.0.0.0:16790 (4)
2022-01-24T22:05:39.308996+00:00 app[web.1]: [2022-01-24 22:05:39 +0000] [4] [INFO] Using worker: sync
2022-01-24T22:05:39.312495+00:00 app[web.1]: [2022-01-24 22:05:39 +0000] [9] [INFO] Booting worker with pid: 9
2022-01-24T22:05:39.338405+00:00 app[web.1]: [2022-01-24 17:05:39 -0500] [10] [INFO] Worker exiting (pid: 10)
2022-01-24T22:05:39.338419+00:00 app[web.1]: [2022-01-24 17:05:39 -0500] [9] [INFO] Worker exiting (pid: 9)
2022-01-24T22:05:39.338617+00:00 app[web.1]: [2022-01-24 22:05:39 +0000] [4] [INFO] Handling signal: term
2022-01-24T22:05:39.410935+00:00 app[web.1]: [2022-01-24 22:05:39 +0000] [10] [INFO] Booting worker with pid: 10
2022-01-24T22:05:39.539223+00:00 app[web.1]: [2022-01-24 22:05:39 +0000] [4] [INFO] Shutting down: Master
2022-01-24T22:05:39.678861+00:00 heroku[web.1]: Process exited with status 0
2022-01-24T22:05:39.861935+00:00 heroku[web.1]: State changed from starting to up
2022-01-24T22:10:13.366591+00:00 app[web.1]: 10.1.16.96 - - [24/Jan/2022:17:10:13 -0500] "GET / HTTP/1.1" 500 145 "-" "Mozilla
/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
2022-01-24T22:10:13.366636+00:00 heroku[router]: at=info method=GET path="/" host=www.scanspectt.com request_id=158aed91-428e-
4222-a31f-d4f82222d583 fwd="71.232.82.43" dyno=web.1 connect=0ms service=404ms status=500 bytes=508 protocol=https
2022-01-24T22:10:13.698085+00:00 app[web.1]: 10.1.16.96 - - [24/Jan/2022:17:10:13 -0500] "GET /favicon.ico HTTP/1.1" 404 179 "
https://www.scanspectt.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.469
2.71 Safari/537.36"
2022-01-24T22:10:13.698323+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=www.scanspectt.com request_id=444
0ae23-a686-40fa-80ee-24a8d2eee0fe fwd="71.232.82.43" dyno=web.1 connect=0ms service=140ms status=404 bytes=418 protocol=https
2022-01-24T22:10:14.470552+00:00 app[web.1]: 10.1.16.96 - - [24/Jan/2022:17:10:14 -0500] "GET / HTTP/1.1" 500 145 "-" "Mozilla
/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
2022-01-24T22:10:14.470642+00:00 heroku[router]: at=info method=GET path="/" host=www.scanspectt.com request_id=7fd86c40-f215-
4b8d-84aa-1d053859212c fwd="71.232.82.43" dyno=web.1 connect=0ms service=22ms status=500 bytes=410 protocol=https
2022-01-24T22:10:14.568688+00:00 app[web.1]: 10.1.16.96 - - [24/Jan/2022:17:10:14 -0500] "GET /favicon.ico HTTP/1.1" 404 179 "
https://www.scanspectt.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.469
2.71 Safari/537.36"
2022-01-24T22:10:14.568836+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=www.scanspectt.com request_id=baa
83716-35a4-4a8b-b416-56c94874cd95 fwd="71.232.82.43" dyno=web.1 connect=0ms service=2ms status=404 bytes=418 protocol=https
2022-01-24T22:10:15.153837+00:00 heroku[router]: at=info method=GET path="/" host=www.scanspectt.com request_id=adde1700-3b4f-
4485-b99d-2e7b18f4afb4 fwd="71.232.82.43" dyno=web.1 connect=0ms service=27ms status=500 bytes=410 protocol=https
2022-01-24T22:10:15.155184+00:00 app[web.1]: 10.1.16.96 - - [24/Jan/2022:17:10:15 -0500] "GET / HTTP/1.1" 500 145 "-" "Mozilla
/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
2022-01-24T22:10:15.237711+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=www.scanspectt.com request_id=01f
8d125-d304-4d47-81a0-6e6e53cf2cc5 fwd="71.232.82.43" dyno=web.1 connect=0ms service=2ms status=404 bytes=418 protocol=https
2022-01-24T22:10:15.238766+00:00 app[web.1]: 10.1.16.96 - - [24/Jan/2022:17:10:15 -0500] "GET /favicon.ico HTTP/1.1" 404 179 "
https://www.scanspectt.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.469
2.71 Safari/537.36"
2022-01-24T22:11:52.334171+00:00 app[web.1]: 10.1.56.77 - - [24/Jan/2022:17:11:52 -0500] "GET / HTTP/1.1" 500 145 "-" "Mozilla
/5.0 (iPhone; CPU iPhone OS 15_1_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Mobile/15E148 Safari/6
04.1"
2022-01-24T22:11:52.334890+00:00 heroku[router]: at=info method=GET path="/" host=www.scanspectt.com request_id=7e37bd10-9fcb-
41a4-8803-9257a455838a fwd="51.81.167.146" dyno=web.1 connect=0ms service=23ms status=500 bytes=410 protocol=http
2022-01-24T22:12:06.634371+00:00 app[api]: Starting process with command `python manage.py migrate` by user hertelrussell@gmai
l.com
2022-01-24T22:12:15.454061+00:00 heroku[run.8023]: State changed from starting to up
2022-01-24T22:12:15.788666+00:00 heroku[run.8023]: Awaiting client
2022-01-24T22:12:15.817476+00:00 heroku[run.8023]: Starting process with command `python manage.py migrate`
2022-01-24T22:12:21.843444+00:00 heroku[run.8023]: Process exited with status 0
2022-01-24T22:12:21.844902+00:00 heroku[run.8023]: Client connection closed. Sending SIGHUP to all processes
2022-01-24T22:12:21.905352+00:00 heroku[run.8023]: State changed from up to complete
2022-01-24T22:12:29.833731+00:00 app[web.1]: 10.1.59.23 - - [24/Jan/2022:17:12:29 -0500] "GET / HTTP/1.1" 500 145 "-" "Mozilla
/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
2022-01-24T22:12:29.834075+00:00 heroku[router]: at=info method=GET path="/" host=www.scanspectt.com request_id=a4bb4a76-9b7e-
4156-84fb-965c80cb04ab fwd="71.232.82.43" dyno=web.1 connect=0ms service=21ms status=500 bytes=410 protocol=https
2022-01-24T22:12:29.993774+00:00 app[web.1]: 10.1.59.23 - - [24/Jan/2022:17:12:29 -0500] "GET /favicon.ico HTTP/1.1" 404 179 "
https://www.scanspectt.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.469
2.71 Safari/537.36"
2022-01-24T22:12:29.994193+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=www.scanspectt.com request_id=217
565fd-856d-4b24-9e22-c2d662850fbf fwd="71.232.82.43" dyno=web.1 connect=0ms service=2ms status=404 bytes=418 protocol=https
2022-01-24T22:12:30.863314+00:00 app[web.1]: 10.1.59.23 - - [24/Jan/2022:17:12:30 -0500] "GET / HTTP/1.1" 500 145 "-" "Mozilla
/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
2022-01-24T22:12:30.863760+00:00 heroku[router]: at=info method=GET path="/" host=www.scanspectt.com request_id=f9c5b64b-a7bd-
46ca-b371-6946788f0c53 fwd="71.232.82.43" dyno=web.1 connect=0ms service=17ms status=500 bytes=410 protocol=https
2022-01-24T22:12:30.955447+00:00 app[web.1]: 10.1.59.23 - - [24/Jan/2022:17:12:30 -0500] "GET /favicon.ico HTTP/1.1" 404 179 "
https://www.scanspectt.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.469
2.71 Safari/537.36"
2022-01-24T22:12:30.955917+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=www.scanspectt.com request_id=434
727bc-a522-498a-b7f6-7715c224e3e9 fwd="71.232.82.43" dyno=web.1 connect=0ms service=2ms status=404 bytes=418 protocol=https
2022-01-24T22:12:31.439899+00:00 app[web.1]: 10.1.59.23 - - [24/Jan/2022:17:12:31 -0500] "GET / HTTP/1.1" 500 145 "-" "Mozilla
/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
2022-01-24T22:12:31.440412+00:00 heroku[router]: at=info method=GET path="/" host=www.scanspectt.com request_id=a07a9c70-4b96-
435b-8fe7-58533ce5dccd fwd="71.232.82.43" dyno=web.1 connect=0ms service=14ms status=500 bytes=410 protocol=https
2022-01-24T22:12:31.551516+00:00 app[web.1]: 10.1.59.23 - - [24/Jan/2022:17:12:31 -0500] "GET /favicon.ico HTTP/1.1" 404 179 "
https://www.scanspectt.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.469
2.71 Safari/537.36"
2022-01-24T22:12:31.553580+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=www.scanspectt.com request_id=47b
f4c34-a9e3-4ba9-a46d-0181250641bc fwd="71.232.82.43" dyno=web.1 connect=0ms service=4ms status=404 bytes=418 protocol=https
2022-01-24T22:12:32.021759+00:00 app[web.1]: 10.1.59.23 - - [24/Jan/2022:17:12:32 -0500] "GET / HTTP/1.1" 500 145 "-" "Mozilla
/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
2022-01-24T22:12:32.022024+00:00 heroku[router]: at=info method=GET path="/" host=www.scanspectt.com request_id=dc58e826-456d-
44c1-99f8-a95d4213841c fwd="71.232.82.43" dyno=web.1 connect=0ms service=77ms status=500 bytes=410 protocol=https
2022-01-24T22:12:32.108050+00:00 app[web.1]: 10.1.59.23 - - [24/Jan/2022:17:12:32 -0500] "GET /favicon.ico HTTP/1.1" 404 179 "
https://www.scanspectt.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.469
2.71 Safari/537.36"
2022-01-24T22:12:32.108386+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=www.scanspectt.com request_id=bb4
84ffd-771d-4735-84ee-a17e95d3e567 fwd="71.232.82.43" dyno=web.1 connect=0ms service=3ms status=404 bytes=418 protocol=https
1

There are 1 answers

0
jhisham On

Although there may be other reasons, usually it has to do with static files.

Per the documentation on Heroku that can be found here:

Django won’t automatically create the target directory (STATIC_ROOT) that collectstatic uses, if it isn’t available. You may need to create this directory in your codebase, so it will be available when collectstatic is run. Git does not support empty file directories, so you will have to create a file inside that directory as well.

During deployment, Heroku will look for a static folder in the root of your project (same directory where your manage.py file lives) and/or in whichever directory you've specified in settings.py via the STATICFILES_DIRS variable which could look something like:

STATICFILES_DIRS = ['BASE_DIR / 'static']

If Django can't find these folders or if the static folders are empty, you might have a hard time during deployment. Although the build process might complete successfully, you'll get these 500 errors when DEBUG is set to False and be left wondering what went wrong.

One way to verify if the static folders exist is by checking going into your Heroku server by running the following command from your terminal:

heroku run bash

Once you're connected to your server you can then list out the directories in the root of your app.

ls

heroku run bash

Now depending on where and how you define your static and staticfiles directory settings, you'll want to verify that these 2 folders are available in your app. The example shown here is based on these 2 settings:

// specifies the root directory of your static files when app is deployed.
// the output will be stored in this directory after running 
// `python manage.py collectstatic`
STATIC_ROOT = BASE_DIR / 'staticfiles'

// specifies which directories to use for static files during development 
// and which directories to look in when 
// `python manage.py collectstatic` is run
STATICFILES_DIR = [BASE_DIR / 'static']

You'll also need to make sure that your static directory is not empty; it needs to have a file inside in order for Git to pick it up and for python manage.py collecstatic to successfully.

Per the Heroku documents:

When a Django application is deployed to Heroku, $ python manage.py collectstatic --noinput is run automatically during the build. A build will fail if the collectstatic step is not successful.

Although you can explicitly tell Heroku not to run collectstatic during the build process by running heroku config:set DISABLE_COLLECTSTATIC=1 and your build might complete successfully, this will most likely and eventually cause errors down the road (like the 500 error when setting DEBUG=False).

So even if you're not using or don't have static files, it's safer to create a static folder in the root of your app and dump a dummy file (any image, js, or css file) inside it so that it gets picked up by Heroku during deployment.

Once this is done, that will hopefully resolve any 500 errors and your app can run when you set DEBUG=False.

One last thing, if you're still having issues, you can also enable debugging of collectstatic in Heroku. Per the Heroku documents:

If collectstatic failed during a build, a traceback was provided that will be helpful in diagnosing the problem. If you need additional information about the environment collectstatic was run in, use the DEBUG_COLLECTSTATIC configuration.

This can be done by typing the following in the terminal:

heroku config:set DEBUG_COLLECTSTATIC=1