我是Django和Python的新手。
当我将我的代码从我的本地开发盒移动到Webfaction上的我的网站时,它似乎打破了我的代码。如果我然后从我的主应用程序views.py中删除( context_instance = RequestContext(request)),它似乎可以解决问题。如果以后我把线放回来就可以了。
return render_to_response('template.html', {"var": var},**context_instance=RequestContext(request)**)
来自webhost的错误电子邮件:
Traceback (most recent call last):
File "/home/hhadmin69/webapps/django/lib/python2.5/django/core/handlers/base.py", line 92, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/hhadmin69/webapps/django/hhv3/article/views.py", line 32, in post_index
return render_to_response('article/post_index.html', {"posts": posts},context_instance=RequestContext(request))
File "/home/hhadmin69/webapps/django/lib/python2.5/django/template/context.py", line 107, in __init__
self.update(processor(request))
File "/home/hhadmin69/webapps/django/hhv3/context_processors.py", line 12, in latest_tweet
tweet = twitter.Api().GetUserTimeline( settings.TWITTER_USER )[0]
File "build\bdist.win32\egg\twitter.py", line 1414, in GetUserTimeline
File "build\bdist.win32\egg\twitter.py", line 2032, in _FetchUrl
File "/usr/local/lib/python2.5/urllib2.py", line 387, in open
response = meth(req, response)
File "/usr/local/lib/python2.5/urllib2.py", line 498, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/local/lib/python2.5/urllib2.py", line 425, in error
return self._call_chain(*args)
File "/usr/local/lib/python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
File "/usr/local/lib/python2.5/urllib2.py", line 506, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 400: Bad Request
'SERVER_SOFTWARE': 'Apache/2.2.12 (Unix) mod_wsgi/2.5 Python/2.5.4',
context_processors.py
from datetime import datetime
from django.conf import settings
from django.core.cache import cache
import twitter
from django.template import Library, Node, TemplateSyntaxError
def latest_tweet( request ):
tweet = cache.get( 'tweet' )
if tweet:
return {"tweet": tweet}
try:
tweet = twitter.Api().GetUserTimeline( settings.TWITTER_USER )[0]
tweet.date = datetime.strptime( tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y" )
cache.set( 'tweet', tweet, settings.TWITTER_TIMEOUT )
except (RuntimeError, TypeError, NameError):
pass
return {"tweet": tweet}
谢谢!!
答案 0 :(得分:1)
您的追溯清楚地表明问题出在twitter {API}的GetUserTimeline
您的TWITTER_USER
settings
?
答案 1 :(得分:1)
它不是RequestContext
- 上下文只会触发实际上失败的高频查询。如果你把线放回去了吗?这可能只意味着您的Twitter查找问题不一致,因为任何HTTP查找都是如此,因此您应该通过处理IOError
(HTTPError
的父项URLError
来处理可能的网络问题。由urllib2
抛出的{1}}。
try:
tweet = twitter.Api().GetUserTimeline( settings.TWITTER_USER )[0]
except IOError, e:
logging.error(e) #or just print, dont know how you log
准确分析失败的方式和时间并尝试找出问题。查看urllib2尝试访问的实际URL也可能有所帮助。
答案 2 :(得分:0)
一旦我扩展了例外以包含以下内容,它就解决了我的问题。非常感谢!
except (IOError, urlopen, URLError, HTTPError):
pass