8

我在本地机器上使用 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'
4

3 回答 3

14

我有一个类似的错误,日志说 ValueError: Missing staticfiles manifest entry for...

将 settings.py 中的 STATICFILES_STORAGE 从:更改
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
为:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
whitenoise 文档的本节所述,为我修复了它。

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

于 2019-05-17T18:26:34.327 回答
1

更改快速解决方案:

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. 享受!
于 2021-01-24T20:55:02.113 回答
1

上面的大多数答案都会让您的应用程序恢复运行,但您最终可能无法达到预期的结果,特别是如果您想利用静态文件缓存策略。

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

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

此处的 Django 文档应该会给您更多的可见性。

于 2021-09-06T12:47:10.027 回答