Testing a Django view cause "AttributeError: 'NoneType' object has no attribute 'handler500'" error
I just wanted to start testing a Django view using the code below:
from django.test.client import Client
c = Client()
response = c.get('/search/keyword')
print response.content
It just throws out following error message:
"/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 286, in get
response = self.request(**r)
File "/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 230, in request
response = self.handler(environ)
File "/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 74, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 143, in get_response
return self.handle_uncaught_exception(request, resolver, exc_info)
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 178, in handle_uncaught_exception
callback, param_dict = resolver.resolve500()
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 268, in resolve500
return self._resolve_special('500')
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 258, in _resolve_special
callback = getattr(self.urlconf_module, 'handler%s' % view_type)
AttributeError: 'NoneType' object has no attribute 'han开发者_JAVA百科dler500'
The view works in browser. What's wrong with above code?
This is a bug in Django trunk that was recently fixed.
You will also see this if you have:
'django.contrib.auth',
in your apps
but don't have:
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
in your middleware
精彩评论