2011-05-12 79 views
0

我試圖讓我的網站上我的Twitter時間線,我在Django。爲此,我在http://www.pixeldonor.com/blog/2010/aug/15/create-simple-twitter-app-django/上找到了一段很好的代碼,我在本地網站的開發副本上實現了該代碼。本地一切都完美無缺!顯示的推文,我從來沒有得到任何錯誤。來自python-oauth2的錯誤:「TypeError渲染時:必須是字符串或緩衝區,而不是無」

既然我已經在網上部署了我的網站,我似乎遇到了一個我無法解決的錯誤。

這是我的錯誤:

Template error

In template /home/mstrijker/webapps/django/vorm4ufolio/templates/index.html, error at line 85

Caught TypeError while rendering: must be string or buffer, not None

<div id="twitter"> 
    <a href="http://twitter.com/#!/Octopixell"><img src="{{ STATIC_URL }}images/twitter_icon.png" alt="twitter_logo"></a> 
    <div id="tweetcontainer"> 
     {% get_tweet_list 5 as tweets %} 
     {% for tweet in tweets %} 
      <div class="tweettop"></div> 
       <div class="tweet"> 
        {{ tweet.text|safe|twitterize }} <br /> {{ tweet.created_at|date:"M d, Y" }} 
       </div> 
      <div class="tweetbottom"></div> 
     {% endfor %} 
    </div> 
</div> 

{% get_tweet_list 5 as tweets %}是線85


TemplateSyntaxError at/
Caught TypeError while rendering: must be string or buffer, not None 

Request Method: GET 
Request URL: http://marcostrijker.com/ 
Django Version: 1.3 
Exception Type: TemplateSyntaxError 
Exception Value: Caught TypeError while rendering: must be string or buffer, not None 
Exception Location: /home/mstrijker/lib/python2.7/oauth2-1.5.170py2.7.egg/oauth2/__init__.py in sign_request, line 493 
Python Executable: /usr/local/bin/python 
Python Version: 2.7.1 
Python Path:  
['/home/mstrijker/webapps/django/vorm4ufolio', 
'/home/mstrijker/webapps/django', 
'/home/mstrijker/webapps/django/lib/python2.7', 
'/home/mstrijker/lib/python2.7/oauth2-1.5.170-py2.7.egg', 
'/home/mstrijker/lib/python2.7/httplib2-0.6.0-py2.7.egg', 
'/home/mstrijker/lib/python2.7/pip-1.0.1-py2.7.egg', 
'/home/mstrijker/lib/python2.7', 
'/usr/local/lib/python27.zip', 
'/usr/local/lib/python2.7', 
'/usr/local/lib/python2.7/plat-linux2', 
'/usr/local/lib/python2.7/lib-tk', 
'/usr/local/lib/python2.7/lib-old', 
'/usr/local/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/site-packages', 
'/usr/local/lib/python2.7/site-packages/PIL'] 
Server time: Thu, 12 May 2011 21:05:17 +0200 

,它顯示以下後


而且,這裏是錯誤的回溯:

Traceback: 
File "/home/mstrijker/webapps/django/lib/python2.7/django/core/handlers/base.py" in get_response 
    111.       response = callback(request, *callback_args, **callback_kwargs) 
File "/home/mstrijker/webapps/django/vorm4ufolio/portfolio/views.py" in index 
    17.  return render_to_response('index.html', {'new_list': new_list,}, context_instance=RequestContext(request)) 
File "/home/mstrijker/webapps/django/lib/python2.7/django/shortcuts/__init__.py" in render_to_response 
    20.  return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) 
File "/home/mstrijker/webapps/django/lib/python2.7/django/template/loader.py" in render_to_string 
    188.   return t.render(context_instance) 
File "/home/mstrijker/webapps/django/lib/python2.7/django/template/base.py" in render 
    123.    return self._render(context) 
File "/home/mstrijker/webapps/django/lib/python2.7/django/template/base.py" in _render 
    117.   return self.nodelist.render(context) 
File "/home/mstrijker/webapps/django/lib/python2.7/django/template/base.py" in render 
    744.     bits.append(self.render_node(node, context)) 
File "/home/mstrijker/webapps/django/lib/python2.7/django/template/debug.py" in render_node 
    73.    result = node.render(context) 
File "/home/mstrijker/lib/python2.7/oauth2-1.5.170-py2.7.egg/oauth2/__init__.py" in request 
    662.   req.sign_request(self.method, self.consumer, self.token) 
File "/home/mstrijker/lib/python2.7/oauth2-1.5.170-py2.7.egg/oauth2/__init__.py" in sign_request 
    493.    self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest()) 

Exception Type: TemplateSyntaxError at/
Exception Value: Caught TypeError while rendering: must be string or buffer, not None 

我希望這會爲你提供幫助我解決這個問題所需的必要信息傢伙。如果你們需要更多的信息或代碼,請問我,我會提供。

回答

1

我瀏覽了oauth2的代碼,我懷疑oauth_req的簽名中post_body的默認值應該是'',而不是None。

def oauth_req(url, http_method="GET", post_body=None, http_headers=None): 

試試吧。

+0

現在我已經試過這個(希望這是你的意思): '高清oauth_req(URL,http_method = 「GET」,post_body = '',http_headers = ''): 消費= oauth.Consumer (key = CONSUMER_KEY,secret = CONSUMER_SECRET)token = oauth.Token(key = OAUTH_TOKEN,secret = OAUTH_SECRET) client = oauth。客戶(消費者,令牌)' 雖然沒有區別。 – 2011-05-12 22:32:51

+0

你重新啓動了Apache嗎?有時當你修改代碼時,你需要重新啓動apache來使其工作。 – ablmf 2011-05-12 22:48:54

+0

你是最棒的!我已經改變爲像你說的代碼,重新啓動apache,它完全解決了問題!太感謝了! – 2011-05-13 00:00:05

0

這個問題是,當請求(或後續請求)的主體是None時,在引發TypeError的python-oauth2模塊中存在一個錯誤。這裏是oauth2/\_\_init\_\_.py的sign_request功能:

def sign_request(self, signature_method, consumer, token): 
    """Set the signature parameter to the result of sign.""" 

    if not self.is_form_encoded:    
     # according to 
     # http://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/oauth-bodyhash.html 
     # section 4.1.1 "OAuth Consumers MUST NOT include an 
     # oauth_body_hash parameter on requests with form-encoded 
     # request bodies." 
     self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest()) 

    if 'oauth_consumer_key' not in self: 
     self['oauth_consumer_key'] = consumer.key 

    if token and 'oauth_token' not in self: 
     self['oauth_token'] = token.key 

    self['oauth_signature_method'] = signature_method.name 
    self['oauth_signature'] = signature_method.sign(self, consumer, token) 

493線是:

self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest()) 

所以不知何故身體被設置爲None。由於httplib2在重定向後將請求主體設置爲None,我的猜測是,當您在線部署時,httplib2所在的地方會有一個新的重定向,然後將主體設置爲None並引發此錯誤。

好消息是,這是我修復的一個bug和issued a pull request。在被接受之前,您可以編輯自己的oauth2/\_\_init\_\_.py文件來處理身體爲Nonedownload and use我的python-oauth版本的情況。

相關問題