一旦我更改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.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"

这是我的设置文件:

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

当前回答

我也遇到过类似的问题,我将报告我是如何解决我的问题的,因为可能有人也有同样的经历。

在我的例子中,错误是因为服务器没有从主页中找到一些静态文件而引起的。

因此,请确保错误只发生在索引中或发生在另一个页面上。如果问题只发生在索引中,很可能你需要检查静态文件。我建议打开Chrome预览控制台,检查是否有任何错误。

在我的例子中,服务器无法找到favicon.ico和其他两个CSS。

为了解决这个问题,我传递了python manage.py collectstatic,它起作用了。

其他回答

我知道这有点晚了,但我最终在这里用DEBUG=False搜索我的错误500,在我的情况下,它确实是ALLOWED_HOSTS,但我使用os. environment .get('变量')来填充主机,我没有注意到这一点,直到我启用日志记录,你可以用下面的文件记录所有错误,即使DEBUG=False也会记录:

# 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',
        },
    }
}

我也遇到过类似的问题,我将报告我是如何解决我的问题的,因为可能有人也有同样的经历。

在我的例子中,错误是因为服务器没有从主页中找到一些静态文件而引起的。

因此,请确保错误只发生在索引中或发生在另一个页面上。如果问题只发生在索引中,很可能你需要检查静态文件。我建议打开Chrome预览控制台,检查是否有任何错误。

在我的例子中,服务器无法找到favicon.ico和其他两个CSS。

为了解决这个问题,我传递了python manage.py collectstatic,它起作用了。

Django 1.5引入了允许的主机设置,这是出于安全原因所必需的。用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 = []

在这里添加您的主机,如['www.beta800.net']或['*']用于快速测试,但不要在生产中使用['*']。

当执行DEBUG = FALSE时,我也遇到了同样的问题。这里是一个统一的解决方案,分散在上面的答案和其他帖子。

默认情况下,在settings.py中我们有ALLOWED_HOSTS =[]。以下是你必须在ALLOWED_HOSTS值中做出的可能的改变,以消除错误:

1:您的域名:

ALLOWED_HOSTS = ['www.example.com'] # Your domain name here

2:你部署的服务器IP,如果你还没有域名(这是我的情况,工作就像一个魅力):

ALLOWED_HOSTS = ['123.123.198.123'] # Enter your IP here

3:如果你在本地服务器上测试,你可以编辑你的settings.py或settings_local.py为:

ALLOWED_HOSTS = ['localhost', '127.0.0.1']

4:你也可以在ALLOWED_HOSTS的值中提供'*',但出于安全考虑,不建议在生产环境中使用:

ALLOWED_HOSTS = ['*'] # Not recommended in production environment

我也在我的博客上发布了一个详细的解决方案,你可以参考一下。

多亏了@squarebear,在日志文件中,我发现了错误: 在<whitenoise.storage中找不到文件'myapp/styles.css'。CompressedManifestStaticFilesStorage…>。

我在django应用程序中遇到了一些问题。我删除了这行 STATICFILES_STORAGE = 'whitenoise.django. 'GzipManifestStaticFilesStorage',我从heroku的文档中找到。

我还必须在django应用程序的根目录中添加额外的目录(感谢另一个SO答案),作为myapp/static,即使我没有使用它。然后在运行服务器之前运行命令python manage.py collectstatic来解决这个问题。最后,它开始正常工作。