When we run $ python manage.py runserver --settings=project.settings.local
there are 4 different possible combinations:
The fourth one is simultaneously: the most important, the hardest to debug, and the only one that fails.
Our django settings are setup with this structure:
settings
├── base.py
├── __init__.py
├── local.py
└── production.py
For this test we only used local.py
and modified the contents for each run.
For Debug=True and DB=local this is the local.py
file:
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',
}
}
For Debug=False and DB=production this is the local.py
file used:
from project.settings.base import *
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': ***,
'USER': ***,
'PASSWORD': ***,
'HOST': '***.amazonaws.com',
'PORT': '5432',
}
}
We also ran it with Debug=True and DB=production as well as Debug=False and DB=local, both of which worked.
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.
For reference, this is the contents of our base.py
:
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'), )
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?
As requested, here is production.py, although it should be noted that this settings file was never used in this experiment.
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'
When DEBUG is False , Django will email the users listed in the ADMINS setting whenever your code raises an unhandled exception and results in an internal server error (strictly speaking, for any response with an HTTP status code of 500 or greater). This gives the administrators immediate notification of any errors.
One of the main features of debug mode is the display of detailed error pages. If your app raises an exception when DEBUG is True, Django will display a detailed traceback, including a lot of metadata about your environment, such as all the currently defined Django settings (from settings.py).
Open your settings.py file (or settings_local.py ) and set DEBUG = False (just add that line if necessary).
Debug the Containerized Django AppSet a breakpoint somewhere in your code. Then in VS Code open the "Run" view again and make sure the Run Django configuration that we previously created is selected. Click the play button to start the debugging session.
I have had the same issue. But then I removed this row in settings.py
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
Now I dont have 500 error when DEBUG=False. But probably, I guess gzip functionality doesnt work anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With