Python 特威比和德扬戈
我不知道该怎么做。我想使用tweepy在我的django应用程序中传输用户的推文。我已经编写了流式代码,但我面临的问题是:我是否应该将代码粘贴到views.py中并输入-Python 特威比和德扬戈,python,django,Python,Django,我不知道该怎么做。我想使用tweepy在我的django应用程序中传输用户的推文。我已经编写了流式代码,但我面临的问题是:我是否应该将代码粘贴到views.py中并输入- return render_to_response('tweet.html',context_instance=RequestContext(request)) 在编写代码之后。就这样, import sys import tweepy
return render_to_response('tweet.html',context_instance=RequestContext(request))
在编写代码之后。就这样,
import sys
import tweepy
import webbrowser
import MySQLdb
Q= sys.argv[1:]
db=MySQLdb.connect("localhost","","","Juzme")
auth=tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
cur=db.cursor()
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
try:
print "%s\t%s\t%s\t%s" % (status.text,
status.author.screen_name,
status.created_at,
status.source,)
cur.execute("INSERT INTO tweets VALUES (%s, %s, %s, %s)", (status.text,
status.author.screen_name,
status.created_at,
status.source))
except Exception, e:
print >> sys.stderr, 'Encountered Exception:', e
pass
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True
streaming_api=tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)
streaming_api.filter(follow=[], track=Q)
return render_to_response('tweet.html',context_instance=RequestContext(request))
如果我可以这样做,模板中不会有任何代码吗?或者我能做这个手术的最好方法是什么。我希望你明白我的意思?谢谢对不起,我如何接受答案?请参见此处,使用Django模型而不是直接的数据库查询。描述你得到了什么,你想得到什么?