非调试模式下的Django Whitenoise 500服务器错误 [英] Django Whitenoise 500 server error in non debug mode

查看:62
本文介绍了非调试模式下的Django Whitenoise 500服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地计算机上使用django.为了提供静态文件,我将WhiteNoise与其一起使用.当 DEBUG = True 时,将正确提供所有静态文件.但是,当我更改 DEBUG = False 并设置 ALLOWED_HOSTS = ['*'] 时,出现500个服务器错误.但是,管理站点加载没有任何错误.另外,当我注释掉 STATICFILES_STORAGE ='whitenoise.storage.CompressedManifestStaticFilesStorage'时,我没有得到500错误.

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.

我遵循了 http://whitenoise.evans.io/en中提供的文档/stable/django.html 连接白噪声.我没有对 wsgi.py 文件进行任何更改.我运行了 python manage.py collecststatic ,并且运行时没有任何错误.

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.

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'

推荐答案

我遇到了类似的错误,日志显示ValueError:缺少静态文件的清单条目 ...

I had a similar error and the logs said ValueError: Missing staticfiles manifest entry for...

在settings.py中更改STATICFILES_STORAGE来自: STATICFILES_STORAGE ='whitenoise.storage.CompressedManifestStaticFilesStorage'
转至:
STATICFILES_STORAGE ='whitenoise.storage.CompressedStaticFilesStorage'
本节whitenoise文档中所述为我修复了该问题.

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.

另外,您可能想更改SECRET_KEY,因为它已公开共享.

Also you probably want to change your SECRET_KEY now that it's shared publicly.

这篇关于非调试模式下的Django Whitenoise 500服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆