Django App Getting Server Error 500 On Local Host When Debug = False
Solution 1:
When you set DEBUG=False
Django doesn't handle your static files anymore. The idea behind is that you need to setup a proper production system. I think you are using Heroku, so I attached here a (tested) configuration for your production settings file (I suggest you to create a dedicated file for prod settings, like in the example below).
settings.py
:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# and here the rest of your local settings
# ...
WSGI_APPLICATION = 'your-app-name.wsgi.application'
# Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
AUTH_PASSWORD_VALIDATORS = []
# Static files configuration
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = os.path.join(BASE_DIR, 'static/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
# ...
# Loading test/prod settings based on ENV settings
ENV = os.environ.get('ENV')
if ENV == 'prod':
try:
from .production_settings import *
MIDDLEWARE.append('whitenoise.middleware.WhiteNoiseMiddleware',)
except ImportError:
pass
production_settings.py
(for Heroku):
import os
DEBUG = False
SECRET_KEY = os.environ.get('SECRET_KEY')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'HOST': os.environ.get('DB_HOST'),
'PORT': '5432',
}
}
# Password validation (I removed them in the local settings)
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'},
]
ALLOWED_HOSTS = (
'your-app-name.herokuapp.com',
'localhost',
)
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
Your wsgi.py file is already ok.
Make sure you have the Procfile
, the requirements.txt
file and you set your env var under the "Config Vars" section on Heroku dashboar (dashboard.heroku.com/your-app-name/axdos/settings
).
Here you need to set SECRET_KEY
, DB_NAME
, DB_USER
, DB_PASSWORD
, DB_HOST
and finally ENV
(this env needs to be equal to prod
). You can find the db credentials under the dashboard.heroku.com/your-app-name/axdos/resources
.
Solution 2:
after adding add WhiteNoise to the MIDDLEWARE
MIDDLEWARE = [
# 'django.middleware.security.SecurityMiddleware','whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]
execute this command
python manage.py collectstatic --noinput
Post a Comment for "Django App Getting Server Error 500 On Local Host When Debug = False"