Reputation: 651
I'm new to Django and Python.
When I move my code from my local dev box to my site on Webfaction it seems to break my code. If I then remove (context_instance=RequestContext(request)) from my main application views.py it seems to fix the problem. If later I put the line back in it works fine.
return render_to_response('template.html', {"var": var},**context_instance=RequestContext(request)**)
Error email from 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}
THANK YOU!!
Upvotes: 0
Views: 1170
Reputation: 651
Once I expanded the excepts to include the following it fixed my issue. Thanks so much!
except (IOError, urlopen, URLError, HTTPError):
pass
Upvotes: 0
Reputation: 4589
It's not RequestContext
- context only triggers tweeter lookup which actually fails. If you put the line back in it works? That could only mean that your twitter lookup problem is inconsistent, as is possible with any HTTP lookups, so you should take care of possible network problems by also handling IOError
(which is a parent of HTTPError
and URLError
which are thrown by urllib2
).
try:
tweet = twitter.Api().GetUserTimeline( settings.TWITTER_USER )[0]
except IOError, e:
logging.error(e) #or just print, dont know how you log
Analyze exactly how and when it fails and try to find the problem. Looking at the actual URL which urllib2 tries to access may help, too.
Upvotes: 1
Reputation: 87095
Your traceback clearly indicates that the problem is in the GetUserTimeline
of twitter.API()
Do you have TWITTER_USER
defined in your settings
?
Upvotes: 1