Django Whitenoise 500服务器在非调试模式下错误



我在本地计算机中使用django。为了服务静态文件,我将使用Whitenoise。当DEBUG = True正确提供所有静态文件时。但是,当我更改DEBUG = False并设置ALLOWED_HOSTS = ['*']时,我会遇到500服务器错误。但是,管理站点加载没有任何错误。另外,当我发表评论STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'时,我不会收到500错误。

我遵循http://whitenoise.evans.io/en/stable/django.html中给出的文档连接Whitenoise。我没有对wsgi.py文件进行任何更改。我运行了python manage.py collecststatic,它没有任何错误。

settings.py在下面给出:

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'

我有类似的错误,日志上述值: Missing staticfiles manifest entry for ...

更改staticfiles_storage in steratings.py来自:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
to:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
正如这一部分中所述的Whitenoise文档为我修复了它。

您也可能想更改秘密_KEY,因为它已公开共享。

更改快速解决方案:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

to:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

但是!它将生成静态文件,而无需唯一的块键,可在客户端浏览器(例如样式.343A1FA2DA70.CSS)中正确更新,并且客户在没有刷新站点缓存的情况下就不会看到更改。 Whitenoise 仅在Django的存储周围添加薄包装器以添加压缩支持,并且由于压缩代码非常简单,因此通常不会引起问题。所以你需要

  1. 返回STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
  2. 清除静态文件夹手动
  3. py manage.py collectstatic --noinput刷新静态资产
  4. 享受!

上面的大多数答案都会使您的应用程序恢复运行,但是您可能不会取得与您预期的相同结果,尤其是如果您想利用静态文件缓存策略。

您获得内部服务器错误的原因是,在添加Whitenoise之前,您默认使用过的python manage.py collectstaticSTATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage'

因此,要将设置保持在STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage',只需再次运行python manage.py collectstatic

这里的Django文档应该为您提供更多的可见性。

最新更新