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

当前回答

我知道这篇文章很老了,但今天仍然非常有意义。

对于它的价值-我得到了一个500与DEBUG =假在我的网站上的所有页面。

在调试时,我没有得到回溯。

我不得不在我的网站中浏览模板中的每个静态链接,并在我的图像源前面找到了一个/(正斜杠)。{%静态…%}。这导致了DEBUG = False中的500错误,但在DEBUG = True中工作得很好,没有错误。很烦人!警告!由于向前斜杠浪费了很多时间…

其他回答

当执行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来解决这个问题。最后,它开始正常工作。

如果您想允许所有主机。 使用ALLOWED_HOSTS = ['*',] 而不是 Allowed_hosts = ['*']

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

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中被突出