As part of changing the layout of my blog I wanted to add my latest Twitter status to the bottom left corner. It turned out to be very straight forward. Python has an excellent Twitter API — very litle code was required.
There’s two ways you could go about doing this. The first way is to write a template tag that would fetch the latest tweet, the other to create a context processor that would provide a tweet variable to your templates. I choose the latter.
Before we can start, the Twitter API module needs to be installed. You can find both the module and installation instruction over on google code: http://code.google.com/p/python-twitter/.
Note: I had to install the SVN version as there’s a bug in python-twitter version 0.5 when using Apache — it works fine using the development server.
To set up a context processor we first need to specify where Django can find our
processors. This is done using the TEMPLATE_CONTEXT_PROCESSORS
setting.
Add the below to your project’s settings.py
file.
1 2 3 4 5 6 7 8 |
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS TEMPLATE_CONTEXT_PROCESSORS = TEMPLATE_CONTEXT_PROCESSORS + ( "mysite.blog.context_processors.latest_tweet", ) TWITTER_USER = "omh" TWITTER_TIMEOUT = 3600 |
Django has bunch of context processors already defined in its global settings. If we only specify our processor, we would remove the default ones. If you want to keep the default processors — chances are you probably do — you could either manually add them — that doesn’t feel very DRY to me, or just append them like we do above.
Since we’re already editing settings.py
we’ll go ahead and
add two Twitter settings, TWITTER_USER
to specify your Twitter login
and TWITTER_TIMEOUT
to specify how often we should ask Twitter for the
latest tweet.
We specified mysite.blog.context_processors.latest_tweet
as the context
processor location. Next up is then to create a latest_tweet
method
inside of context_processors.py
. Here’s what
context_processors.py
looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from datetime import datetime from django.conf import settings from django.core.cache import cache import twitter def latest_tweet( request ): tweet = cache.get( 'tweet' ) if tweet: return {"tweet": tweet} tweet = twitter.Api().GetUserTimeline( settings.TWITTER_USER )[0] tweet.date = datetime.strptime( tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y" ) cache.set( 'tweet', tweet, settings.TWITTER_TIMEOUT ) return {"tweet": tweet} |
Django passes a HttpRequest
object as the first and only parameter to
all context processors. This is why we need to take request
as a
parameter, however in our case we don’t use it.
The above code is checking the cache for an existing tweet
object. If
the cache is empty it is uses the Twitter API to fetch the
latest tweet, converts the date into a datetime object and stores the tweet in
the cache.
With the context processor in place you can access the tweet
object from
any template loaded by any of the generic
views. In your own views you need to use RequestContext
instead
of Context
, as your context class when rendering templates.
1 2 3 4 5 |
<div class="twitter"> {% if tweet %} <p>Latest tweet from {{ tweet.date|naturalday }}: {{ tweet.text }}<p> {% endif %} </div> |
#1 Stefan
Posted: Aug. 4, 2008
Good stuff! Congrats on the new design, was it inspired by http://daringfireball.net/ btw? ;)
#2 Ole Morten
Posted: Aug. 4, 2008
@Stefan: yes, the left menu was inspired by DF :)
#3 RJ Ryan
Posted: Aug. 17, 2008
thanks for this!