I'm trying to traverse the session variable to print out all of its contents.

for s in request.session:
    print str(s)

The resulting error is as follows

KeyError at /<app name>/searchResults/
0

With the following traceback.

/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response
    115. response = callback(request, *callback_args, **callback_kwargs)

/<path to django app>/views.py
    106. for s in request.session:

/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py in __getitem__
    46 return self.__session[key]

Any idea what '0' means as an error? I've never seen this kind of thing before.

有帮助吗?

解决方案

The correct way to iterate over the session's values is request.session.itervalues() - the base session class exposes the same key/value/item options as a standard dictionary. I am unsure so far where it's getting the values your for loop is finding, but it's not the values.

其他提示

You class is missing the __iter__ member... thats all.

You can probably subclass it to fix the bug.

e.g.

class IterableRequest(Request):
  def __iter__(self): return self.iterkeys()

Maybe this next hack might fix the problem:

Request.__iter__=Request.iterkeys

See also:

I'd be tempted to check the source and confirm it is a bug, and then make a bug report.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top