Why don't my Django unittests know that MessageMiddleware is installed?

前端 未结 9 715
傲寒
傲寒 2020-12-14 05:54

I\'m working on a Django project and am writing unittests for it. However, in a test, when I try and log a user in, I get this error:

MessageFailure: You can         


        
9条回答
  •  既然无缘
    2020-12-14 06:27

    Django 1.4 has a bug when you create the request with RequestFactory.

    To resolve this issue, create your request with RequestFactory and do this:

    from django.contrib.messages.storage.fallback import FallbackStorage
    setattr(request, 'session', 'session')
    messages = FallbackStorage(request)
    setattr(request, '_messages', messages)
    

    Works for me!

提交回复
热议问题