Why would django fail with server 500 only when Debug=False AND db is set to production database on Heroku? <p>When we run <code>$ python manage.py runserver --settings=project.settings.local</code> there are 4 different possible combinations:</p> <ol> <li>Debug=True &amp;&amp; DB=local => Runs fine</li> <li>Debug=True &amp;&amp; DB=production => Runs fine</li> <li>Debug=False &amp;&amp; DB=local => Runs fine</li> <li>Debug=False &amp;&amp; DB=Production => Server 500 error</li> </ol> <p>The fourth one is simultaneously: the most important, the hardest to debug, and the only one that fails. </p> <p>Our django settings are setup with this structure:</p> <pre><code>settings ├── base.py ├── __init__.py ├── local.py └── production.py </code></pre> <p>For this test we only used <code>local.py</code> and modified the contents for each run. </p> <p>For Debug=True and DB=local this is the <code>local.py</code> file:</p> <pre><code>from project.settings.base import * DEBUG = True TEMPLATE_DEBUG = True DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': ***, 'USER': ***, 'PASSWORD': ***, 'HOST': 'localhost', 'PORT': '5432', } } </code></pre> <p>For Debug=False and DB=production this is the <code>local.py</code> file used:</p> <pre><code>from project.settings.base import * ALLOWED_HOSTS = ['*'] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': ***, 'USER': ***, 'PASSWORD': ***, 'HOST': '***.amazonaws.com', 'PORT': '5432', } } </code></pre> <p>We also ran it with Debug=True and DB=production as well as Debug=False and DB=local, both of which worked.</p> <p>The DB settings were copied directly from the Heroku configuration and the connection to the database works great as long as Debug is set to True, so we're pretty sure it's not a DB schema or connection problem. We just can't figure out how it's possible that the production DB works when Debug is True, and it runs with DB set to False with the local DB, but for some reason when the two are combined it fails. We've also deployed the code to Heroku and confirmed that it runs with Debug set to True but fails with the same Server 500 error when Debug is set to False.</p> <p>For reference, this is the contents of our <code>base.py</code>:</p> <pre><code>import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = *** # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'appname', ) MIDDLEWARE_CLASSES = ( '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' WSGI_APPLICATION = 'project.wsgi.application' # Database # https://docs.djangoproject.com/en/1.6/ref/settings/#databases SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') # Internationalization # https://docs.djangoproject.com/en/1.6/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.6/howto/static-files/ STATIC_ROOT = 'staticfiles' STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) </code></pre> <p>Our googling has come up with a lot of people misconfiguring the ALLOWED_HOSTS variable and ending up with similar symptoms but that doesn't seem to be our problem. Does anybody have any insight as to what could be causing this problem? </p> <p>As requested, here is production.py, although it should be noted that this settings file was never used in this experiment.</p> <pre><code>from project.settings.base import * import dj_database_url DEBUG = False TEMPLATE_DEBUG = False ALLOWED_HOSTS = ['*', '.***.com', '.herokuapp.com', 'localhost', '127.0.0.1'] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': ***, 'USER': ***, 'PASSWORD': ***, 'HOST': '***.amazonaws.com', 'PORT': '5432', } } STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' </code></pre>