Django Heroku Debug = False, Static files cause Internal Server Error

heroku django debug=false 500
heroku django static files
heroku static assets
static files not loading django heroku
django heroku assets
django static files not working
static files on heroku with django and whitenoise
heroku css not working django

RESOLVED: I'm not sure what combination of things I did to ultimately fix it, but the final piece was fixing my static file hierarchy. Before, I simply had /<base>/static/main.css but I changed this to /<base>/static/<project-name>/main.css and that somehow fixed the issue.

Original problem: I am currently trying to deploy a django app with Heroku. When I try to deploy with Debug=False however, I get an Internal Server Error, because the server cannot find the main.css style sheet. Note that when Debug=True everything works fine.

When I try python manage.py findstatic --verbosity 2 main.css in the commandline, I get the error message "No matching file found for 'main.css'. Looking in the following locations: /Users/Kyle/.local/share/virtualenvs/hostable-Ij2AFM0k/lib/python3.6/site-packages/django/contrib/admin/static", but I have no clue why it's looking in here.

Below I've provided my settings.py file. My file hierarchy is a mess, because doing collectstatic generated a whole bunch of static files into a staticfiles folder, but I don't know where they came from or what their purpose is. I have the style sheet in a static folder, but this doesn't seem to be being called anymore.

What I would like to know is this: what am I supposed to change either in my settings or in my file hierarchy so that main.css is found?

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

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'crispy_forms',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'menus.apps.MenusConfig',
    'events.apps.EventsConfig',
    'users.apps.UsersConfig',
]

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            os.path.join(BASE_DIR, 'users/templates'),
            os.path.join(BASE_DIR, 'events/templates'),
            os.path.join(BASE_DIR, 'menus/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 = 'project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.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/2.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/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

DEBUG_PROPAGATE_EXCEPTIONS = True

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')

P.S. I have tried using whitenoise rather unsuccessfully as well.

I should also mention, when I look at the log files, I get the ValueError: Missing staticfiles manifest entry for … message referenced here, http://whitenoise.evans.io/en/stable/django.html#why-do-i-get-valueerror-missing-staticfiles-manifest-entry-for .

Working version of settings.py:

DEBUG = False

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'crispy_forms',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'menus.apps.MenusConfig',
    'events.apps.EventsConfig',
    'users.apps.UsersConfig',
]

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            os.path.join(BASE_DIR, 'users/templates'),
            os.path.join(BASE_DIR, 'events/templates'),
            os.path.join(BASE_DIR, 'menus/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 = 'project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.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/2.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/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

DEBUG_PROPAGATE_EXCEPTIONS = True

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

In Debug = False, try adding urls for static files in your project dir's url.py file:

from django.contrib import admin
from django.urls import path, re_path, include
from django.conf.urls.static import static
from django.conf import settings
from django.views.static import serve

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^', include('app.urls')),

    re_path(r'^static/(?P<path>.*)$', serve,
            {'document_root': settings.STATIC_ROOT}),
] 

Update:

By adding above in project urls.py file, Django will inspect your STATIC_URL setting and wire up the view to serve static files accordingly.

Further, as stated in the docs, when in production mode (i.e., DEBUG = False), you should be using your web server to serve static files, not django. For this reason, staticfiles will refuse to serve your assets if DEBUG = False.

500 error when Debug=False with Heroku and Django, However, this was resulting in the error: 2018-09-13T13:13:49.905187+00:00 app[web.1]: "When using Django Compressor together with staticfiles,� Django does not support serving static files in production. However, the fantastic WhiteNoise project can integrate into your Django application, and was designed with exactly this purpose in mind. See the WhiteNoise documentation for more details.

In my app on Heroku I used instructions from https://docs.djangoproject.com/en/1.11/howto/static-files/. In settings.py I have configured:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/' 
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] 
STATIC_ROOT = os.path.join(BASE_DIR, 'collectstatic') 
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

I have also used this command to copy all files from your static folders into the STATIC_ROOT directory: python manage.py collectstatic

Cannot make it work with DEBUG=False on Heroku � Issue #11 , I'm trying to serve statics on heroku with DEBUG=False. changed, the file were copied but when I try to access a static file I get a 404 error. With debug=True both the vs Code/Python/Django web app and Heroku web app worked. I had this same issue, realized whitenoise was causing the issue. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Why am I getting a 500 server error when debug=False in , When debug=True the Heroku app works fine, however, when I … works fine, however, when I change debug to =False I always get a 500 server error. Images) # https://docs.djangoproject.com/en/2.0/howto/static-files/ STATIC_ROOT ROOT CAUSE: Django widget does not recognise the date format so ignores it and� Python Django not loading static files when DEBUG is False With debug turned off Django won't handle static files for you any more your production web server (Apache or something) should take care

Setting DEBUG = False causes 500 Error, I work on my django site locally, and pull from the repository remotely. On my local machine, False, the server presents a 500 error, and there's nothing to be found in the Apache logs. server or mod_wsgi etc? How do you serve static files? I'm trying to deploy my django site to heroku, unfortunately none of the static css files were showing. I've been looking through a bunch of solutions but to no avail. Really frustrating because the rest of the site is working perfectly! By the way the bootstrap classes are loading, which is pretty obvious, just thought I'd mention it.

django debug false and internal server error - django - iOS, django debug false and internal server error - django. i doing wrong? cause i can't figure it out? also, what should i put in allowed host for heroku site? thanks true is no the same when debug is false heroku uses whitenoise for static files. I was having the same issues. With debug=True both the vs Code/Python/Django web app and Heroku web app worked. With debug=False, neither of them worked. The below suggestion helped fixed the issue in my case. link

Django Tutorial Part 11: Deploying Django to production, Choose an environment for hosting any static files. DEBUG . This should be set as False in production ( DEBUG = False ). Heroku internally uses a load balancer to distribute web traffic to all "web" dynos. For Django this will usually be the Gunicorn web application server (with a .wsgi script). wsgi.py:� I have a Django app that I'm trying to deploy to Heroku. I've followed the Heroku documentation closely with no real issues. However, after pushing to Heroku, I'm seeing an "internal server error" when I visit the app online. I'm not seeing any obvious errors in the Terminal (below).

Comments
  • This does not work, and gives me the same error. Can you explain what the intent of the added line is?
  • Thanks so much for the help. I ended up using this as well in my working version. I'm not sure how integral this piece was, though, so if you can offer any further explanation I would still love that.
  • When I have made these changes and when I run python manage.py collectstatic locally I am given the message 0 static files copied to <path>. When I run heroku run python manage.py collectstatic I also get 0 static files copied to '/app/staticfiles'. Adding those lines into settings.py did not work.
  • On another note, locally at least, when I run python manage.py findstatic --verbosity 2 main.css it does find main.css now! Remotely, though, from the error logs I still get the message ValueError: Missing staticfiles manifest entry for '/main.css'
  • This does not work. Like I mentioned in my post, I have tried using whitenoise without success. I tried that line above again just to be sure, but without success.
  • I ended up including this in the working version. Could you point me to why you use this storage space in particular?
  • I have it working now (see the edits in my post), and I ended up using this line in the end.