我想在 django 的任何模板中都有一个变量。此变量包含所有推文 twittter,我将其显示在页脚中。这是你要做的: 1)我 custom_context 文件,其中包含以下代码:
from TwitterAPI import TwitterAPI
from codebase import settings
def get_last_tweet(request):
api = TwitterAPI(settings.CONSUMERKEY, settings.CONSUMERSECRET, settings.ACCESSTOKEN, settings.ACCESSTOKENSECRET)
r = api.request('statuses/user_timeline', {'count': 10})
tweets = [result['text'] for result in r.get_iterator()]
return {'tweets': tweets}
这是它的样子和 TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
'django.core.context_processors.static',
'django.core.context_processors.media',
'django.core.context_processors.i18n',
'django.core.context_processors.tz',
'django.core.context_processors.debug',
'django.contrib.messages.context_processors.messages',
'home.custom_context.get_last_tweet',
)
当我运行模板时出现以下错误
HTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out.