消息的 Django 1.9 单元测试错误,但我没有看到 1.10.4 和 1.8 的错误

Django 1.9 unittest error for messages but i dont see the error for 1.10.4 and 1.8

在 Django 1.9 env 中测试失败并出现错误,但在 1.10.4 和 1.8 中测试 运行 非常好。有人可以在这里亮灯吗!

这里是错误

add_message raise MessageFailure('You cannot add messages without installing ' MessageFailure: You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware

检查 https://travis-ci.org/invaana/Hydronium/builds/182190626 是否存在完整错误

views.py

def contact(request):
    if request.method == "POST":
        form = ContactForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'Operation Successful! This is demo. Im not sending any messages')
        else:
            form.errors
            messages.error(request, 'Message not sent, Please fix the errors!.' )
    else:
        form = ContactForm()
    return render(request, 'pages/contact.html', {'form': form})

tests.py

def test_contactform_valid(self):
    res = self.client.post(reverse('contact'), {'name':'Ravi','email':'rrmerugu@gmail.com', 'text': 'im good' })
    self.assertIn('Operation Successful', res.content)

def test_contactform_invalid(self):
    res = self.client.post(reverse('contact'), {'name':'Ravi','email':'rrmerugu@gmail.com', 'text': '' })
    self.assertIn('Message not sent', res.content)

显然,

MIDDLEWARE_CLASSES is used in 1.9, 1.8

MIDDLEWARE is used in 1.10.4.

为了使中间件兼容两者,我在 settings.py

中使用了以下代码
MIDDLEWARE = MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]