2017-04-22 27 views
0

編輯:更多信息:Django的本地主機提供了500服務器錯誤。當DEBUG =假,但是ALLOWED_HOSTS = [ '*']

我添加記錄作爲第一個答案的建議,並得到了這一點:

ValueError: The file 'file/name' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object 

ORIGINAL POST:

我正在本地運行我的Django站點,並且當DEBUG = True時它工作正常。但是,當我設置DEBUG = False時,它將返回一個500服務器錯誤。

ALLOWED_HOSTS已設置爲['*'],並且它仍然返回500服務器錯誤。

這裏是我的settings.py:

import os 
import dj_database_url 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = "**************************" 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = False 

# Application definition 

INSTALLED_APPS = [ 
    'my_app.apps.MyAppConfig', 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    # Disable Django's own staticfiles handling in favour of WhiteNoise, for 
    # greater consistency between gunicorn and `./manage.py runserver`. See: 
    # http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development 
    'whitenoise.runserver_nostatic', 
    '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.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'mysite.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', 
      ], 
      'debug': DEBUG, 
     }, 
    }, 
] 

WSGI_APPLICATION = 'mysite.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.postgresql', 
     'NAME': 'my_db', 
     'USER': 'admin', 
     'PASSWORD': '', 
     'HOST': 'localhost', 
     'PORT': '', 
    } 
} 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 

# Internationalization 
# https://docs.djangoproject.com/en/1.10/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 
TIME_ZONE = 'America/Los_Angeles' 
USE_I18N = True 
USE_L10N = True 
USE_TZ = True 

# Update database configuration with $DATABASE_URL. 
db_from_env = dj_database_url.config(conn_max_age=500) 
DATABASES['default'].update(db_from_env) 

# Honor the 'X-Forwarded-Proto' header for request.is_secure() 
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') 

# Allow all host headers 
ALLOWED_HOSTS = ['*'] 

# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.10/howto/static-files/ 

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') 
STATIC_URL = '/static/' 

# Extra places for collectstatic to find static files. 
STATICFILES_DIRS = [ 
    os.path.join(PROJECT_ROOT, 'static'), 
] 

# Simplified static file serving. 
# https://warehouse.python.org/project/whitenoise/ 
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' 
+1

您是否獲得了服務器上有什麼錯誤? – danielunderwood

+0

無效的'ALLOWED_HOSTS'會導致'400 Bad Request'響應,而不是'500 Server Error'。你有一個不同的錯誤,你需要提供更多的信息,才能提供幫助。 – knbk

+0

以下是錯誤:ValueError:在[hash]處找到 HLH

回答

0

解決這個正確的方法是啓用日誌記錄和Django的(通常)指出問題。這種方法的優點是它對未來的問題也很有用。這裏有一個樣本記錄設置,將記錄與DEBUG=false(從this answer借用):

# settings.py 
LOGGING = { 
    'version': 1, 
    'disable_existing_loggers': False, 
    'formatters': { 
     'verbose': { 
      'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s", 
      'datefmt' : "%d/%b/%Y %H:%M:%S" 
     }, 
     'simple': { 
      'format': '%(levelname)s %(message)s' 
     }, 
    }, 
    'handlers': { 
     'file': { 
      'level': 'DEBUG', 
      'class': 'logging.FileHandler', 
      'filename': 'mysite.log', 
      'formatter': 'verbose' 
     }, 
    }, 
    'loggers': { 
     'django': { 
      'handlers':['file'], 
      'propagate': True, 
      'level':'DEBUG', 
     }, 
     'MYAPP': { 
      'handlers': ['file'], 
      'level': 'DEBUG', 
     }, 
    } 
} 
相關問題