0

I deployed an app to Heroku, which uses Postgres. Anytime I set Debug to True, it works fine. When I set it back to False, it gives an Error 500, Internal Server Error. I don't know why it does that. Does it have to do with staticfiles? I am using whitenoise for serving static files.

Anytime I run heroku run python manage.py collectstatic, it gives me

Running python manage.py collectstatic on djangotestapp... up, run.9614 (Free)

300 static files copied to '/app/staticfiles', 758 post-processed.

When I then run heroku run bash, I find out that there is no staticfiles folder. Where exactly does it copy the static files to? Is this what is causing the Error 500? How can I solve it?

settings.py


import os
import dj_database_url
from decouple import config, Csv

# 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/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', cast=bool)

SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_HSTS_SECONDS = 200
SECURE_HSTS_PRELOAD = True

ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast =Csv())

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    # Third-party
    'allauth',
    'allauth.account',
    'crispy_forms',
    'widget_tweaks',
    'debug_toolbar',
    'phonenumber_field',
    'django_datatables_view',
    
    'whitenoise.runserver_nostatic',     

    # Local
    'users',
    'pages',
    'attendance',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', # WHITENOISE
    'django.middleware.common.CommonMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'djangotestapp_project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        #'DIRS': ['templates'],
        '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 = 'djangotestapp_project.wsgi.application'

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': config('DB_NAME'),
        'USER':  config('DB_USER'),
        'PASSWORD': config('DB_PASSWORD'),
        'HOST':  config('DB_HOST'),
        'PORT': '5432',
    },  
}

prod_db = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

#STATICFILES_FINDERS = [    
#    'django.contrib.staticfiles.finders.FileSystemFinder',
#    'django.contrib.staticfiles.finders.AppDirectoriesFinder',    
#]


#STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
#
# STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)

UPDATE I ran heroku logs -tail -a djangotestapp to view errors as they happen, and this is what it gave me.

2020-08-30T20:23:20.090560+00:00 app[web.1]: raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
2020-08-30T20:23:20.090560+00:00 app[web.1]: ValueError: Missing staticfiles manifest entry for 'css/font-rules-roboto.css'

The file, font-rules-roboto.css just enables me to be able to have the roboto font without any link to Google font API's.

I have run python manage.py collectstatic, but it does not do anything.

David.B
  • 370
  • 6
  • 16

2 Answers2

1

I think your problem is related to your ALLOWED_HOSTS. When you turn off the debug mode of Django's settings you must add your project host IP or domain to ALLOWED_HOSTS. So I think adding the IP of your host server to your .env file like (ALLOWED_HOSTS = localhost, ..., IP.OF.YOUR.HOST) will solve your problem.

Roham
  • 1,970
  • 2
  • 6
  • 16
  • this is what is in my ALLOWED_HOSTS, `.herokuapp.com` – David.B Aug 30 '20 at 14:52
  • 1
    For testing purpose and see if this problem comes from this part, change `ALLOWED_HOSTS` to `ALLOWED_HOSTS = '*'` (to allow all hosts to have access to your server) and reload your page and see if it is working or not. – Roham Aug 30 '20 at 15:17
  • 1
    Note: Do not forget to change `ALLOWED_HOSTS` to default setting of yours after testing in case of security. – Roham Aug 30 '20 at 15:25
  • 1
    Please change the line `ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast =Csv())` to `ALLOWED_HOSTS = ['*']` in your project's `settings.py` and see if the error code is change from 500 to 404 and tell me the results. – Roham Aug 30 '20 at 15:59
  • It still did not work. Still Error 500. I add some more information to the post. Please check the end. – David.B Aug 30 '20 at 20:39
  • 1
    Maybe `https://stackoverflow.com/questions/50658241/django-doesnt-load-static-files-valueerrormissing-staticfiles-manifest-entry` can help you. – Roham Aug 30 '20 at 22:33
  • 1
    Thanks, the link helped a lot. I solved it with an added step. Please check the answer. – David.B Aug 31 '20 at 13:04
0

Thank you, @Roham for your help. I solved it with an added step.

After I commented STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage', the site started working, but the static files did not load.

I then added this line to the settings, WHITENOISE_USE_FINDERS = True. It allows your app to run without having to collectstatic files.

After I did that, all the static files loaded without any problems.

David.B
  • 370
  • 6
  • 16