我添加了一个twitter_status.py文件到我的项目,并从views.py我所说的update_twitter_status()方法
twitter_status.py:
"""
Adds a tweet to the twitter account in settings.
Login to dev.twitter.com and add a desktop application
Add the keys and secrets for the added application to the settings file
Requires tweepy to be installed
https://github.com/joshthecoder/tweepy
"""
from django.conf import settings
from tweepy import *
class TwitterManager:
def __get_api_handle(self):
#Create OAuth object
auth = OAuthHandler(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET)
#Set access tokens
auth.set_access_token(settings.TWITTER_ACCESS_TOKEN, settings.TWITTER_ACCESS_TOKEN_SECRET)
#Create API handle
api = API(auth)
return api
def update_twitter_status(self, message):
api = self.__get_api_handle()
#Send update
api.update_status(message)
然后在我的views.py我只是打电话所述update_twitter_status(消息)方法
views.py:
from myproject.twitter_status import TwitterManager
def __update_twitter(message):
twit_mgr = TwitterManager()
twit_mgr.update_twitter_status(message)
然后瓦特henever我想从我的views.py鸣叫我加入这行
__update_twitter('I am tweeting')
如果有人对我是怎样Implemeted一个类或方法,请我会很高兴收到您的反馈不同意。
投票结束为“不是问题”,因为它根本不清楚问题是什么...... – 2012-03-25 23:11:19
@David:看起来像是一个有效的问题...他需要Django约定将代码分离到不同的文件他。 – cha0site 2012-03-26 09:30:37