设置DEBUG = False会导致500错误 [英] Setting DEBUG = False causes 500 Error

查看:106
本文介绍了设置DEBUG = False会导致500错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦我更改了 DEBUG = False ,我的网站将生成500(使用wsgi& manage.py runserver),并且在Apache错误日志中没有错误信息当我将 debug 更改为 True 时,它将正常运行。



我正在使用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.1500 257 - Mozilla / 5.0(Windows NT 6.2; WOW64)AppleWebKit / 537.22(KHTML ,像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.1500 257 - Mozilla / 5.0(Windows NT 6.2; WOW64)AppleWebKit / 537.22(KHTML,像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.1500 257 - Mozilla / 5.0(Windows NT 6.2; WOW64)AppleWebKit / 537.22(KHTML ,像Gecko)Chrome / 25.0.1364.97 Safari / 537.22

这是我的设置文件: p>

  import os.path 
DEBUG = False
#TEMPLATE_DEBUG = DEBUG

这里= os.path.dirname(__ file__)
ADMINS =(
('admin', xyzadmin@qq.com'),


MANAGERS = ADMINS

DATABASES = {
'default':{
' ':'django.db.backends.mysql',#添加'postgresql_psycopg2','mysql','sqlite3'或'oracle'。
'NAME':'zdm',#或使用sqlite3的数据库文件路径。
'USER':'root',#不与sqlite3一起使用。
'PASSWORD':'passwd',#不与sqlite3一起使用。
'HOST':'',#设置为localhost的空字符串。不适用于sqlite3。
'PORT':'',#设置为空字符串为默认值。不适用于sqlite3。
}
}

#此安装的本地时区。可以在这里找到选择:
#http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
#虽然并不是所有的操作系​​统都可以选择所有选项。
#在Windows环境中,必须将其设置为系统时区。
TIME_ZONE ='美国/芝加哥'

#此安装的语言代码。所有选择都可以在这里找到:
#http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE ='en-us'

SITE_ID = 1

#如果将其设置为False,Django将进行一些优化,以便
#加载国际化机器。
USE_I18N = True

#如果将其设置为False,Django将不会根据当前语言环境格式设置日期,数字和
#日历。
USE_L10N = True

#如果将其设置为False,Django将不会使用时区感知数据时间。
USE_TZ = True

#将保存用户上传文件的目录的绝对文件系统路径。
#示例:/home/media/media.lawrence.com/media/
MEDIA_ROOT =''

#处理从MEDIA_ROOT提供的媒体的URL。确保使用
#尾部斜线。
#示例:http://media.lawrence.com/media/,http://example.com/media/
MEDIA_URL =''

#静态文件的绝对路径应该被收集到。
#不要把任何东西放在这个目录下;在应用程序的static /子目录和STATICFILES_DIRS中存储静态文件
#。
#示例:/home/media/media.lawrence.com/static/
#STATIC_ROOT = os.path.join(HERE,'static')。replace('\\' ,'/')

#静态文件的URL前缀。
#示例:http://media.lawrence.com/static/
STATIC_URL ='/ static /'
#STATIC_ROOT = os.path.join(HERE,'static' ).replace('\\','/')
S = os.path.join(HERE,'static')。replace('\\','/')

#静态文件的附加位置
STATICFILES_DIRS =(
#将字符串放在这里,如/ home / html / static或C:/ www / django / static
#总是使用正斜杠,甚至在Windows上
#不要忘记使用绝对路径,而不是相对路径
'/ home / zdm / static',


#找到类的列表,知道如何在
#中找到静态文件的各个位置。
STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles .finders.DefaultStorageFinder',


#使其独一无二,不要与任何人共享。
SECRET_KEY ='9a7!^ gp8ojyk - ^^ d @ * whuw!0rml + r + uaie4ur $(do9zz_6!hy0'

#知道如何从各种导入模板的可调用列表来源。
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'
#取消注释下一行的简单点击保护:
#'django.middleware.clickjacking.XFrameOptionsMiddleware',


ROOT_URLCONF ='zdm.urls'

#Django运行时使用的WSGI应用程序的Python虚线路径服务器
WSGI_APPLICATION ='zdm.wsgi.application'

TEMPLATE_DIRS =(
#将字符串放在这里,如/ home / html / django_templates或C:/ www / Django的/模板。
#总是使用正斜杠,甚至在Windows上。
#不要忘记使用绝对路径,而不是相对路径。
'/ home / zdm / templates',


INSTALLED_APPS =(
'django.contrib.auth',
'django.contrib。 contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles' ,
#取消注释下一行以启用admin:
'django.contrib.admin',
#取消注释下一行以启用管理员文档:
#'django.contrib ,
'zdm',
'portal',
'admin',
'tagging',


解决方案

Django 1.5介绍了允许主机设置,这是出于安全原因而需要的。使用Django 1.5创建的设置文件具有您需要添加的新部分:

 #有效的主机/域名这个网站如果DEBUG为False,则需要
#请参阅https://docs.djangoproject.com/en/1.9/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []

在这里添加您的主机,如 ['www.beta800.net'] ['*'] 进行快速测试,但不要使用 ['*'] 进行制作


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 .

I'm using Django 1.5 & Python 2.7.3 here is Apache access log and without any log in apache error log

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',
)

解决方案

Django 1.5 introduced the allowed hosts setting that is required for security reasons. A settings file created with Django 1.5 has this new section which you need to add:

# 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.

这篇关于设置DEBUG = False会导致500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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