django:为什么我不能将twitter.Twitter对象存入会话

时间:2012-12-19 02:21:25

标签: django session object twitter

我创建了一个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: 

1 个答案:

答案 0 :(得分:0)

正如您猜测的那样,您只能存储pickleable个对象。

来自the docs

  

会话字典应该接受任何pickleable Python对象。有关详细信息,请参阅pickle模块。