When we run $ python manage.py runserver --settings=project.settings.local , there are 4 different possible combinations:
- Debug = True && & DB = local => Works fine
- Debug = True && & DB = production => Works fine
- Debug = False && & DB = local => Works fine
- Debug = False && & DB Error = Production => Server 500
Fourth at the same time: the most important, the most difficult to debug, and the only one that fails.
Our django settings are configured using this structure:
settings βββ base.py βββ __init__.py βββ local.py βββ production.py
For this test, we used only 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, the local.py file is local.py :
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 using Debug = True and DB = production, as well as Debug = False and DB = local, both of which worked.
The database settings were copied directly from the Heroku configuration, and the database connection works fine if the Debug parameter is set to True, so we are sure that this is not a database schema or a connection problem. We simply cannot understand how it is possible that the production database works when Debug is True, and it works with the database set to False from the local database, but for some reason, when these two are combined, they fail. We also deployed the code to Heroku and confirmed that it works with the Debug parameter equal to True, but does not work with the same Server 500 error when the Debug parameter 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:
Our googling came up with a lot of people who incorrectly set the ALLOWED_HOSTS variable and end up with similar symptoms, but this does not seem to be our problem. Does anyone know what might cause 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'