本文介绍了消息的Django 1.9单元测试错误,但我看不到1.10.4和1.8的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试失败,并在Django 1.9 env中出现错误,,但测试在1.10.4和1.8中运行得很好.有人可以在这里放点灯光吗!

The test fails with the error in Django 1.9 env, but tests run perfectly well in 1.10.4 and 1.8. Can someone throw some light here !

这是错误

检查 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':'[email protected]', 'text': 'im good' })
    self.assertIn('Operation Successful', res.content)

def test_contactform_invalid(self):
    res = self.client.post(reverse('contact'), {'name':'Ravi','email':'[email protected]', 'text': '' })
    self.assertIn('Message not sent', res.content)

推荐答案

显然,

MIDDLEWARE在1.10.4中使用.

MIDDLEWARE is used in 1.10.4.

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

To make the middleware compatible for both, I used the following code in 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',
]

这篇关于消息的Django 1.9单元测试错误,但我看不到1.10.4和1.8的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 04:44