一旦我更改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',
)

当前回答

ALLOWED_HOSTS不是唯一的问题,对我来说,我必须做一个404.html,并把它放在我的模板的基础级别(不是应用程序级别)-此外,你可以做一个404视图,并添加一个404handler url,但我认为这是可选的。404.html修复了它

在mainproject.urls

handler404 = 'app.views.custom_404'

在app.views

def custom_404(request):
    return render(request, '404.html', {}, status=404)

然后创建templates/404.html模板

从另一个S/O帖子中得到的,我找不到它

EDIT

此外,当我使用白噪声服务资产时,我得到500个错误。不能为我的生活弄清楚,错误是ValueError从白噪声无法找到一个资产,我也找不到,不得不去默认django服务现在

其他回答

当DEBUG=False时,我发现了另一个导致500错误的原因。我使用Django压缩工具,我们的前端工程师在Django模板的压缩css块中添加了对字体文件的引用。是这样的:

{% compress css %}
    <link href="{% static "css/bootstrap.css" %}" rel="stylesheet">
    <link href="{% static "css/bootstrap-spinedit.css" %}" rel="stylesheet">
    <link href="{% static "djangular/css/styles.css" %}" rel="stylesheet">
    <link href="{% static "fonts/fontawesome-webfont.ttf" %}" rel="stylesheet">
{% endcompress %}

解决方案是将链接移到结束压缩行下面的ttf文件。

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

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

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

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

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

我知道这是一个非常老的问题,但也许我可以帮助其他人。如果你在设置DEBUG=False后出现了500个错误,你可以在命令行中运行manage.py runserver来查看任何不会出现在任何web错误日志中的错误。

我知道这是一个老问题,但是当DEBUG=False时,我也得到了一个500错误。几个小时后,我意识到我忘了在base.html中的一些链接后面加上斜杠。

我遇到了这个问题。结果发现,我在模板中使用静态模板标记包含了一个不再存在的文件。看了一下日志,我发现了问题。

我想这只是造成这种错误的众多可能原因之一。

这个故事的寓意是:总是记录错误,总是检查日志。