1

我创建了一个 twitter.Twitter 对象,但无法将其存储到会话中。以下是我的代码。

t = twitter.Twitter(auth=twitter.OAuth(consumer_key, consumer_secret, 
    oauth_token, oauth_secret))    
request.session['t'] = t

但是如果我存储一个字符串

request.session['t'] = 't'

它有效,为什么?因为 twitter#Twitter 对象不是可序列化的对象?当我需要 twitter#Twitter 对象时,如何避免每次都进行身份验证?以下是 django 跟踪。

Django Version: 1.3.1
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'myproj.myapp']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  178.                 response = middleware_method(request, response)
File "/usr/lib/python2.6/site-packages/django/contrib/sessions/middleware.py" in process_response
  36.                 request.session.save()
File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/file.py" in save
  121.                     os.write(output_file_fd, self.encode(session_data))
File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/base.py" in encode
  93.         pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
File "/usr/lib/python2.6/site-packages/twitter-1.9.0-py2.6.egg/twitter/api.py" in __call__
  173.         return self._handle_response(req, uri, arg_data)
File "/usr/lib/python2.6/site-packages/twitter-1.9.0-py2.6.egg/twitter/api.py" in _handle_response
  198.                 raise TwitterHTTPError(e, uri, self.format, arg_data)

Exception Type: TwitterHTTPError at /tool/log_in/
Exception Value: 
4

1 回答 1

0

正如您所猜测的那样,您只能存储可腌制对象。

文档

会话字典应该接受任何可腌制的 Python 对象。有关更多信息,请参阅 pickle 模块。

于 2012-12-19T02:31:14.023 回答