I am using django in my local machine. In order to serve the static files I used WhiteNoise along with it. When DEBUG = True
all static files are correctly served. But when I changed DEBUG = False
and set ALLOWED_HOSTS = ['*']
I'm getting 500 server error. However admin site loads without any error. Also when I comment out STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
I don't get 500 error.
I followed the documentation given in http://whitenoise.evans.io/en/stable/django.html to connect whitenoise. I haven't made any changes to wsgi.py
file. I ran python manage.py collecststatic
and it ran without any errors.
The settings.py
is given below:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'fdft&b(xb*!qq3ghjkjhg6789ih8ik!w10$0uscxcpqpmz'
DEBUG = False
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'whitenoise.runserver_nostatic', #Disable Djangos static file server during DEVELOPMENT
'gep_app.apps.GepAppConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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 = 'gep_project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'gep_project.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '*************',
'USER': '*****',
'PASSWORD': '********',
'HOST': '*****',
'PORT': '5432',
}
}
# User model
AUTH_USER_MODEL = 'gep_app.User'
# Login URL
LOGIN_URL = 'login'
# Login redirect
LOGIN_REDIRECT_URL = 'home'
# Logout redirect
LOGOUT_REDIRECT_URL = 'login'
#Authentication backends
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
I had a similar error and the logs said ValueError: Missing staticfiles manifest entry for
...
Changing STATICFILES_STORAGE in settings.py
from: STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
to: STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
as described in this section of whitenoise docs fixed it for me.
Also you probably want to change your SECRET_KEY now that it's shared publicly.
Change for fast solution:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
to:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
But! It will generate static files without unique chunk keys for correct updating in client browser (e.g. style.343a1fa2da70.css) and client won't see changes after without refreshing site cache. WhiteNoise only adds a thin wrapper around Django’s storage to add compression support, and because the compression code is very simple it generally doesn’t cause problems. So you need
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
py manage.py collectstatic --noinput
Most answers above would get your app back up and running, but you may not end up achieving the same result as you may have intended especially if you wanted to leverage the static files caching strategy.
The reason you got the internal server error is that, before adding Whitenoise you had run python manage.py collectstatic
which by default used; STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage'
Therefore to maintain your setting to STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
, just run python manage.py collectstatic
again.
The Django docs here should give you more visibility.
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