本文翻译自:Setting DEBUG = False causes 500 Error
Once I change the DEBUG = False
, my site will generate 500 (using wsgi & manage.py runserver), and there is no error info in Apache error log and it will run normally when I change debug
to True
. 更改DEBUG = False
,我的站点将生成500(使用wsgi&manage.py runserver),并且Apache错误日志中没有错误信息,并且在将debug
更改为True
时它将正常运行。
I'm using Django 1.5 & Python 2.7.3 here is Apache access log and without any log in apache error log 我正在使用Django 1.5和Python 2.7.3,这是Apache访问日志,并且在apache错误日志中没有任何日志
www.beta800.net:80 222.247.56.11 - - [28/Feb/2013:13:42:28 +0800] "GET / HTTP/1.1" 500 257 "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22"
www.beta800.net:80 222.247.56.11 - - [28/Feb/2013:13:42:28 +0800] "GET /favicon.ico HTTP/1.1" 500 257 "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22"
www.beta800.net:80 222.247.56.11 - - [28/Feb/2013:13:42:28 +0800] "GET /favicon.ico HTTP/1.1" 500 257 "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22"
Here is my settings file: 这是我的设置文件:
import os.path
DEBUG = False
#TEMPLATE_DEBUG = DEBUG
HERE = os.path.dirname(__file__)
ADMINS = (
('admin', 'xyzadmin@qq.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'zdm', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'passwd', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True
# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
#STATIC_ROOT = os.path.join(HERE, 'static').replace('\\','/')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
#STATIC_ROOT = os.path.join(HERE, 'static').replace('\\','/')
S= os.path.join(HERE, 'static').replace('\\','/')
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'/home/zdm/static',
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = '9a7!^gp8ojyk-^^d@*whuw!0rml+r+uaie4ur$(do9zz_6!hy0'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'zdm.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'zdm.wsgi.application'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'/home/zdm/templates',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'zdm',
'portal',
'admin',
'tagging',
)
#1楼
参考:https://stackoom.com/question/11twb/设置DEBUG-False会导致-错误
#2楼
Django 1.5 introduced the allowed hosts setting that is required for security reasons. Django 1.5引入了出于安全原因所需的允许主机设置 。 A settings file created with Django 1.5 has this new section which you need to add: 使用Django 1.5创建的设置文件具有此新部分,您需要添加该部分:
# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.9/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []
Add your host here like ['www.beta800.net']
or ['*']
for a quick test, but don't use ['*']
for production . 在此处添加您的主机,例如['www.beta800.net']
或['*']
,以进行快速测试, 但请勿将['*']
用于生产 。
#3楼
Right, in Django 1.5 if DEBUG = False, configure ALLOWED_HOSTS, adding domains without the port number. 正确,在Django 1.5中,如果DEBUG = False,则配置ALLOWED_HOSTS,添加不带端口号的域。 example: 例:
ALLOWED_HOSTS = ['localhost']
#4楼
You must also check your URLs all over the place. 您还必须检查所有地方的URL。 When the DEBUG
is set to False
, all URLs without trailing /
are treated as a bug, unlike when you have DEBUG = True
, in which case Django will append /
everywhere it is missing. 当DEBUG
设置为False
,所有不带尾部/
URL都将被视为一个错误,这与当您设置DEBUG = True
,在这种情况下,Django将在/
丢失的所有位置附加/
。 So, in short, make sure all links end with a slash EVERYWHERE. 因此,简而言之,请确保所有链接均以“。”开头。
#5楼
I have a hilarious story for all. 我有一个有趣的故事。 After reaching this page I said "Eureka! I'm saved. That MUST be my problem." 到达此页面后,我说:“尤里卡!我已保存。那一定是我的问题。” So I inserted the required ALLOWED_HOSTS
list in setting.py and... nothing. 因此,我在setting.py中插入了所需的ALLOWED_HOSTS
列表,然后…什么也没有。 Same old 500 error. 相同的旧500错误。 And no, it wasn't for lack of a 404.html file. 不,不是因为缺少404.html文件。
So for 2 days I busied myself with wild theories, such as that it had something to do with serving static files (understand that I am a noob and noobs don't know what they're doing). 因此,有两天我忙于野蛮的理论,例如它与提供静态文件有关(请理解我是菜鸟,而菜鸟不知道他们在做什么)。
So what was it? 那是什么 It is now Mr. Moderator that we come to a useful tip. 现在是主持人先生,我们提供了一个有用的提示。 Whereas my development Django is version 1.5.something, my production server version is 1.5.something+1... or maybe plus 2. Whatever. 我的开发Django版本是1.5.something,而我的生产服务器版本是1.5.something + 1 ...或加2。 And so after I added the ALLOWED_HOSTS
to the desktop version of settings.py , which lacked what hwjp requested--- a "default value in settings.py, perhaps with an explanatory comment"--- I did the same on the production server with the proper domain for it. 因此,当我将ALLOWED_HOSTS
添加到桌面版本的settings.py时 ,该版本缺少hwjp要求的内容-“ settings.py中的默认值,可能带有解释性注释”-我在生产服务器上做了相同的操作具有适当的域。
But I failed to notice that on the production server with the later version of Django there WAS a default value in settings.py with an explanatory comment. 但是我没有注意到在带有更高版本Django的生产服务器上,settings.py中有一个默认值,带有解释性注释。 It was well below where I made my entry, out of sight on the monitor. 在监视器的视线范围内,它远低于我进入的位置。 And of course the list was empty. 当然,列表是空的。 Hence my waste of time. 因此,我浪费时间。
#6楼
I think it could also be the http server settings. 我认为也可能是http服务器设置。 Mine is still broken and had ALLOWED_HOSTS the entire time. 我的仍然很破损,整个时间都保持ALLOWED_HOSTS。 I can access it locally (i use gunicorn), but not via the domain name when DEBUG=False. 我可以在本地访问它(我使用gunicorn),但是当DEBUG = False时不能通过域名访问。 when I try using the domain name it then gives me the error, so makes me think its a nginx related issue. 当我尝试使用域名时,它给了我错误提示,因此让我认为这是与Nginx相关的问题。
Here is my conf file for nginx: 这是我的nginx的conf文件:
server {
listen 80;
server_name localhost myproject.ca www.myproject.ca;
root /var/web/myproject/deli_cms;
# serve directly - analogous for static/staticfiles
location /media/ {
# if asset versioning is used
if ($query_string) {
expires max;
}
}
location /admin/media/ {
# this changes depending on your python version
root /var/web/myproject/lib/python2.6/site-packages/django/contrib;
}
location /static/ {
alias /var/web/myproject/deli_cms/static_root/;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://localhost:8000/;
}
# what to serve if upstream is not available or crashes
error_page 500 502 503 504 /media/50x.html;
}