之前有效,但现在它给了我一个错误。我使用内置消息来显示错误,信息或成功消息。当导入数据失败时,系统必须执行message.error,但会产生错误。
例外:
Types: MessageFailure
Value: You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
当我在设置中间件中跟踪我的代码时,消息就在那里。
MIDDLEWARE_CLASSES = [
'django.middleware.gzip.GZipMiddleware',
'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',
]
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
)
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.humanize',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.markup',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
]
以下是捕获错误的代码:
try:
.......
except Exception, e:
code = '000'
if hasattr(e, 'code'):
code = e.code
msg = 'No message returned from the server'
if hasattr(e, 'msg'):
msg = e.msg
messages.error(
request,
'Error connecting to OFX server. URL: {0} ERROR: '
'{1} {2}'.format(self.account.bank.ofx_url, code, msg))
return ''
return req.text