私は自分のプロジェクトに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)
ワット誰かが私はクラスをimplemetedているかの方法が、私はあなたのフィードバックを得るために喜んでいるだろう喜ば方法に同意しない場合、私は私のviews.pyからつぶやきたいhenever私はこのライン
__update_twitter('I am tweeting')
を追加します。
質問は何であるかはっきりしていないので、 "質問ではありません"と投票してください... –
@David:私には有効な質問のようです...彼は別のファイルにコードを分ける際のDjangoの規則彼。 – cha0site