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

当前回答

我开始以debug=False的形式得到500

django.urls.exceptions.NoReverseMatch: Reverse for 'home' not found.
or...
django.urls.exceptions.NoReverseMatch: Reverse for 'about' not found.

当引发django.core.exceptions.ValidationError而不是引发rest_framework.serializer . validationerror时

公平地说,它之前已经引发了一个500,但作为一个ValidationError, debug=False,这变成了NoReverseMatch。

其他回答

Its mid 2019 and I faced this error after a few years of developing with Django. Baffled me for an entire night! It wasn't allowed host (which should throw a 400), everything else checked out, finally did some error logging only to discover that some missing / or messed up static files manifest (after collectstatic) were screwing with the setup. Long story short, for those who are stumped AND SO HAPPEN ARE USING WHITENOISE OR THE DJANGO STATICFILE BACKEND WITH CACHE (manifest static files) , maybe this is for you.

确保你设置了所有的东西(就像我为白噪音后台所做的一样……Django后端(请继续阅读)http://whitenoise.evans.io/en/stable/django.html 如果错误代码500仍然使您失败,请注意您的设置。

将其设置为任意一个(用于压缩后端白噪声)

STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

或者(保留django默认值)

STATICFILES_STORAGE = django.contrib.staticfiles.storage.StaticFilesStorage

总而言之,问题似乎来自于这个白噪声缓存+压缩后端——>

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

或者django自己的缓存后端——>

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

...对我来说并不是很好,因为我的CSS引用了一些其他的源,这些源可能会在收集/后端缓存期间混淆。这个问题也可能在http://whitenoise.evans.io/en/stable/django.html#storage-troubleshoot中被突出

当执行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

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

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

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']或['*']用于快速测试,但不要在生产中使用['*']。

我给大家讲个搞笑的故事。翻到这一页后,我说:“尤里卡!我得救了。那一定是我的问题。”所以我插入所需的ALLOWED_HOSTS列表在setting.py和…什么都没有。同样的500错误。不,这并不是因为缺少404.html文件。

所以在两天的时间里,我忙于各种疯狂的理论,比如它与提供静态文件有关(要知道我是个新手,新手不知道他们在做什么)。

那是什么呢?主持人先生,现在我们来讨论一个有用的建议。而我开发的Django版本是1.5。某某,我的生产服务器版本是1.5.某某+1…或者加2。无论什么。因此,在我将ALLOWED_HOSTS添加到settings.py的桌面版本之后,它缺少hwjp请求的内容——“settings.py中的默认值,可能带有解释性注释”——我在生产服务器上使用正确的域对其进行了同样的操作。

但是我没有注意到在生产服务器上的Django的最新版本中,在settings.py中有一个默认值和一个解释性的注释。它远低于我进入的地方,在监视器上看不见。当然,这个列表是空的。因此我浪费了时间。