我最近将我的1.4项目升级到1.6,在解决了一些弃用问题后,我又遇到了另一个我无法解决的问题。
每当我尝试保存新对象或编辑现有对象时,都会出现list index out of range
错误。我理解错误意味着什么,但不知道django如何设置obj
的值,该值应该是obj[3]
的值。回溯将我发送到这里:django/contrib/messages/storage/cookie.py in process_messages, line 38
完整追溯:
Traceback:
File "/Users/user/Development/virtual_environments/demo/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
201. response = middleware_method(request, response)
File "/Users/user/Development/virtual_environments/demo/lib/python2.7/site-packages/django/contrib/messages/middleware.py" in process_response
23. unstored_messages = request._messages.update(response)
File "/Users/user/Development/virtual_environments/demo/lib/python2.7/site-packages/django/contrib/messages/storage/base.py" in update
140. messages = self._loaded_messages + self._queued_messages
File "/Users/user/Development/virtual_environments/demo/lib/python2.7/site-packages/django/contrib/messages/storage/base.py" in _loaded_messages
91. messages, all_retrieved = self._get()
File "/Users/user/Development/virtual_environments/demo/lib/python2.7/site-packages/django/contrib/messages/storage/fallback.py" in _get
24. messages, all_retrieved = storage._get()
File "/Users/user/Development/virtual_environments/demo/lib/python2.7/site-packages/django/contrib/messages/storage/cookie.py" in _get
70. messages = self._decode(data)
File "/Users/user/Development/virtual_environments/demo/lib/python2.7/site-packages/django/contrib/messages/storage/cookie.py" in _decode
154. return json.loads(value, cls=MessageDecoder)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py" in loads
339. return cls(encoding=encoding, **kw).decode(s)
File "/Users/user/Development/virtual_environments/demo/lib/python2.7/site-packages/django/contrib/messages/storage/cookie.py" in decode
49. return self.process_messages(decoded)
File "/Users/user/Development/virtual_environments/demo/lib/python2.7/site-packages/django/contrib/messages/storage/cookie.py" in process_messages
41. return [self.process_messages(item) for item in obj]
File "/Users/user/Development/virtual_environments/demo/lib/python2.7/site-packages/django/contrib/messages/storage/cookie.py" in process_messages
38. print 'OBJECT 3: {0}'.format(obj[3])
cookies.py的代码。问题在于尝试获取obj[3]
obj的值是:[u'__json_message', 20, u'The Player "Goodman" was changed successfully.']
class MessageDecoder(json.JSONDecoder):
"""
Decodes JSON that includes serialized ``Message`` instances.
"""
def process_messages(self, obj):
if isinstance(obj, list) and obj:
if obj[0] == MessageEncoder.message_key:
if obj[1]:
print obj
print 'OBJECT 3: {0}'.format(obj[3])
obj[3] = mark_safe(obj[3])
return Message(*obj[2:])
return [self.process_messages(item) for item in obj]
if isinstance(obj, dict):
return dict([(key, self.process_messages(value))
for key, value in six.iteritems(obj)])
return obj
def decode(self, s, **kwargs):
decoded = super(MessageDecoder, self).decode(s, **kwargs)
return self.process_messages(decoded)
答案 0 :(得分:1)
错误是由“messages”cookie格式的更改引起的,导致Django 1.6无法反序列化Django 1.4消息cookie。
Django 1.5包含一个修复程序,它支持这两种格式,但是这个修复程序还没有(现在)存在于Django 1.6中。
我已为此报告issue。
同时,您可以使用以下中间件删除旧格式的Cookie:
from django.contrib.messages.storage.cookie import CookieStorage
class FixMessageMiddleware(object):
"""
The message cookie format has changed from Django 1.4 to
Django 1.5. Django 1.5 probably supported both formats,
but if you move from 1.4.x to 1.6.x directly, you will run into
an IndexError:
Exception Type: IndexError
Exception Value:
list index out of range
Exception Location: (...) django/contrib/messages/storage/cookie.py in process_messages, line 37
This small piece of middleware will track those cookies and destroy
them (leaving new-style in tact)
https://code.djangoproject.com/ticket/22426
DISCLAIMER:
This middleware will not attempt to rewrite the messages! You may
miss important notifications because of this!
"""
def process_request(self, request):
data = request.COOKIES.get("messages")
storage = CookieStorage(request)
try:
storage._decode(data)
except IndexError:
del request.COOKIES['messages']
我还blogged关于这个问题(这是上面的中间件来自哪里)