将DEBUG设置为False会导致500错误。

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

是的,我已经在我的模板目录中添加了403和404 500 HTML文件。 - zhiguo.wang
1
你的项目中是否已经放置了500.html、404.html和403.html文件?我记得曾经在一个部署的项目中遇到过这样的问题,那个项目的根目录下没有这些文件。 - esse
在我的问题中添加一些访问日志。谢谢。 - zhiguo.wang
102
既然你的SECRET_KEY已经公开,你可能想要更改它了... - ashastral
1
这并不适用于所有人。如在https://dev59.com/PWUp5IYBdhLWcg3wrI7T#37218484中所示,可能会有许多原因导致此问题发生。通过一些简单的记录,您可以在没有猜测的情况下找出原因。 - Rob
显示剩余2条评论
33个回答

1
感谢@squarebear,在日志文件中,我发现了错误:ValueError: The file 'myapp/styles.css' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage ...>
我的django应用程序有一些问题。我删除了从heroku文档中找到的行STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
我还不得不添加额外的目录(感谢另一个SO答案static在django应用程序的根目录下作为myapp/static,即使我没有使用它。然后在运行服务器之前运行命令python manage.py collectstatic解决了这个问题。最终,它开始正常工作。

这个回答如何回答原始问题? - Nick

1
我遇到了这个问题。结果发现我在模板中使用static模板标签包含一个不存在的文件。查看日志后发现了问题所在。
我想这只是这种错误可能出现的许多原因之一。
故事的寓意:始终记录错误并始终检查日志。

0

需要注意的一点是,如果数组中包含None,则所有随后允许的主机都将被忽略。

ALLOWED_HOSTS = [
    "localhost",
    None,
    'example.com', # First DNS alias (set up in the app)
    #'www.example.com', # Second DNS alias (set up in the app)
]

Django版本1.8.4


0
我发现了另一个 DEBUG=False 时出现 500 错误的原因。我使用 Django 的 compressor 工具,我们的前端工程师在 Django 模板中的 compress css 块内添加了字体文件的引用 inside。就像这样:

{% 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 文件的代码移动到 endcompress 行以下。

0
我遇到了这个问题,并通过删除这行代码来解决它: STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

0

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


连CSS和JS的东西也要吗? - amchugh89

0

我有一个视图,在debug=false时抛出500错误,但在debug=true时却可以正常工作。对于任何遇到这种情况且Allowed Hosts不是问题的人,我通过更新指向错误位置的模板静态标签来修复了我的视图。

因此,我建议检查在任何使用的模板中链接和标签是否完好无损,也许在debug中某些东西会被忽略,但在生产环境中会出现错误。


0

我开始以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.serializers.ValidationError

公平地说,以前已经抛出了一个500错误,但由于debug=False,这导致了NoReverseMatch错误。


0

这是旧的,我的问题最终与问题有关,但不是针对OP,而是我的解决方案适用于任何其他尝试上述方法无效的人。

我在修改版Django中设置了一个选项来压缩CSS和JS文件,只有在DEBUG关闭时才运行。我的服务器没有安装CSS压缩器并抛出了错误。如果您正在使用Django-Mako-Plus,则可能是您的问题。


0

我曾经遇到过类似的问题,我会介绍一下我的解决方法,因为可能有人也有同样的经历。

在我的情况下,错误是由于服务器找不到主页中一些静态文件而引起的。

所以请确保该错误仅发生在index或其他页面上。如果问题仅出现在索引中,那么很可能需要检查静态文件。我建议打开Chrome预览控制台并检查是否有任何错误。

在我的情况下,服务器找不到favicon.ico和另外两个CSS文件。

为了解决这个问题,我运行了python manage.py collectstatic命令,并且问题得到了解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接