Why does django fail with server 500 only when Debug = False And db is installed in the production database on Heroku?

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://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 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' 
+5
source share
4 answers

I had the same problem. But then I deleted this line in settings.py

 STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 

Now I don't have 500 errors when DEBUG = False. But probably I think the gzip function no longer works.

+5
source

What you should do here:

  • Open Heroku logs by running $ heroku logs --tail in one terminal window.
  • In another terminal window, run $ heroku ps:restart to restart your speakers.

Keep track of the logs and see the actual trace. It will definitely tell you what is happening. Based on your configuration, this can be a series of problems.

+1
source

Configuring the server’s email and viewing the stack trace, as mentioned in the Two-Bit Alchemist, was key. We added these lines to our settings:

 EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = '***' EMAIL_HOST_PASSWORD = '***' EMAIL_PORT = 587 EMAIL_USE_TLS = True SERVER_EMAIL = EMAIL_HOST_USER 

And I received an email with this error in the stack trace:

 ValueError: The file 'stylesheets/application.css' could not be found with <whitenoise.django.GzipManifestStaticFilesStorage object at 0x7fdcebb94550>. 

We used to have problems with static files, but we fixed them. The css file in which he complains that he cannot find, no longer exists and is not mentioned anywhere, so I’m not sure why this error is even approaching, but we fixed it by adding an empty application.css file.

0
source

Hope this ever helps someone. I had similar problems and it took me a while before I fixed it. Check out the <a></a> tags. If the href attributes point to a template / link that do not exist. You may encounter such.

0
source

Source: https://habr.com/ru/post/1212736/


All Articles