【问题标题】:Django Whitenoise 500 server error in non debug mode非调试模式下的 Django Whitenoise 500 服务器错误
【发布时间】:2019-05-20 11:04:25
【问题描述】:

我在本地机器上使用 django。为了提供静态文件,我使用了 WhiteNoise 和它。当DEBUG = True 正确提供所有静态文件时。但是当我更改 DEBUG = False 并设置 ALLOWED_HOSTS = ['*'] 时,我收到 500 服务器错误。但是管理站点加载没有任何错误。此外,当我注释掉 STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' 时,我没有收到 500 错误。

我按照http://whitenoise.evans.io/en/stable/django.html 中给出的文档来连接白噪声。我没有对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'

【问题讨论】:

  • 您的服务器日志显示错误是什么?
  • @markwalker_ 没有这样的错误。 Django 只返回 500。由于注释掉 STATICFILE_STORAGE 给出了正确的输出,因此错误必须与白噪声有关。
  • @markwalker_ [20/Dec/2018 05:46:03] "GET /accounts/login/ HTTP/1.1" 500 27 这是在控制台打印的

标签: django django-staticfiles django-settings collectstatic whitenoise


【解决方案1】:

我遇到了类似的错误,日志显示 ValueError: Missing staticfiles manifest entry for...

在 settings.py 中更改 STATICFILES_STORAGE 来自:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
至:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
this section of whitenoise docs 中所述,已为我修复。

您可能还想更改您的 SECRET_KEY,因为它已公开共享。

【讨论】:

【解决方案2】:

改变快速解决方案:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

到:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

但是!它将生成没有唯一块键的静态文件,以便在客户端浏览器中正确更新(例如 style.343a1fa2da70.css),并且客户端在不刷新站点缓存后将看不到更改。 WhiteNoise 只是在 Django 的存储周围添加了一个瘦包装器来添加压缩支持,并且由于压缩代码非常简单,因此通常不会引起问题。所以你需要

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

【讨论】:

    【解决方案3】:

    上面的大多数答案都可以让您的应用重新启动并运行,但您最终可能无法获得预期的结果,尤其是如果您想利用静态文件缓存策略。

    您收到内部服务器错误的原因是,在添加 Whitenoise 之前,您已经运行了默认使用的 python manage.py collectstaticSTATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage'

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

    Django 文档here 应该会给你更多的可见性。

    【讨论】:

    • 您可能需要在第二次运行python manage.py collectstatic 之前删除/清除生成的静态文件夹。
    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 2011-12-30
    • 2013-06-03
    • 1970-01-01
    • 2018-01-05
    • 2017-05-03
    • 2021-11-03
    相关资源
    最近更新 更多