【问题标题】:Django error 500 when Debug=False - allowed hosts set to *. nothing in console当 Debug=False 时 Django 错误 500 - 允许的主机设置为 *.控制台中没有任何内容
【发布时间】:2018-09-18 19:25:30
【问题描述】:

当我将 Django 设置为 Debug=False 时,我在通过 gunicorn 或通过 manage.py 运行时收到错误 500。通过管理运行时,我在控制台中没有收到任何错误。

我的设置文件将允许的主机设置为 ['*']。一旦我更改 Debug=True 一切都会再次运行。

在运行 manage.py 时,如果不是来自控制台,我将在哪里获得 500 的输出?

这是我的设置文件:

'''
Django settings for itapp project.

For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
'''

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import ldap

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ***********

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'home.apps.HomeConfig',
    'oncall.apps.OncallConfig',
    'tools.apps.ToolsConfig',
    'sites.apps.SitesConfig',
    'maintenance.apps.MaintenanceConfig',
    'service.apps.ServiceConfig',
    'monitoring.apps.MonitoringConfig',
    'mgmt.apps.MgmtConfig',
    'config.apps.ConfigConfig',
    'circuits.apps.CircuitsConfig',
    'storages',
    'imagekit',
    'django_celery_results',
    'debug_toolbar',
    'simple_history',
    'crispy_forms',
)

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware', 
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'simple_history.middleware.HistoryRequestMiddleware', 
]

ROOT_URLCONF = 'itapp.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR + '/templates/',
            ],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug' : DEBUG,
            'context_processors': [
                'django.template.context_processors.debug',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'itapp.context_processors.SiteLinks',
                'itapp.context_processors.QuickJumpLinks',
            ],
        },
    },
]
CRISPY_TEMPLATE_PACK = 'bootstrap3'
WSGI_APPLICATION = 'itapp.wsgi.application'

# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
        'NAME': 'it_app_db_v2',
        'USER': '*****',
        'PASSWORD': '*****',
        'HOST': '****',
        'PORT': '3306',
    }
}

# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

INTERNAL_IPS = ('127.0.0.1',)

def show_toolbar(request):
    return True

DEBUG_TOOLBAR_CONFIG = {
    "SHOW_TOOLBAR_CALLBACK" : show_toolbar,
}

# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/

LANGUAGE_CODE = 'en-gb'

TIME_ZONE = 'Europe/London'

USE_I18N = True

USE_L10N = True

USE_TZ = False

【问题讨论】:

  • 请指定哪个 url 出现此错误
  • 浏览器的页面中没有可以复制/粘贴的错误信息?
  • 所有 url 都会产生错误,除了 [09/Apr/2018 13:01:49] "GET / HTTP/1.1" 200 1038

标签: python django


【解决方案1】:

在运行生产服务器时,根据Django docs,您可以通过电子邮件将错误发​​送给您,前提是您在settings.py 中设置以下内容(将值更改为有意义的值):

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'my.mail.server'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 25
EMAIL_USE_SSL = False
DEFAULT_FROM_EMAIL = 'mydjango@example.com'
SERVER_EMAIL = 'mydjango@example.com'

ADMINS = (
    ('Your name Here', 'your.email@example.com'),
)

MANAGERS = (
    ('Your name Here', 'your.email@example.com'),
)

【讨论】:

  • 我想说类似的话,也链接到error reporting
  • 错误是调试工具栏我需要将设置更改为 DEBUG_TOOLBAR_​​CONFIG = { 'SHOW_TOOLBAR_​​CALLBACK': lambda request: not request.is_ajax() and request.META.get('REMOTE_ADDR', None) in INTERNAL_IPS }
猜你喜欢
  • 2021-08-13
  • 2021-11-05
  • 2020-09-12
  • 2018-11-24
  • 2019-11-04
  • 2019-09-21
  • 1970-01-01
  • 2010-09-12
  • 2013-02-14
相关资源
最近更新 更多