سؤال

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