2012-03-25 38 views
0

我對此感到困惑;而不是編寫類似tweepy的代碼 -Django Tweepy文件

auth=tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 

api=tweepy.API(auth) 

在新的python文件(IDLE)中。我們不能把它寫在我們在django的views.py中嗎?或者我們應該在django應用程序中爲代碼創建一個新文件?

+1

投票結束爲「不是問題」,因爲它根本不清楚問題是什麼...... – 2012-03-25 23:11:19

+0

@David:看起來像是一個有效的問題...他需要Django約定將代碼分離到不同的文件他。 – cha0site 2012-03-26 09:30:37

回答

0

我添加了一個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一個類或方法,請我會很高興收到您的反饋不同意。