Jason Hasselkus

A whole lot of something for nothing

Jason Hasselkus header image 2

Django and RSS

November 14th, 2007 · No Comments

Now that I have my blog built and a few posts under my belt, I want to be able to notify readers (as if there are some), about my updates via RSS. Django makes this very easy to accomplish, so lets get right to it. Here are the things I want to accomplish:

  • Create an RSS feed for my blog posts
  • Allow Feedburner to manage my feeds

So the first thing I need to do is create a url for the feeds. I’m going to add the following to my urlpatterns in urls.py

(r’^feeds/(?P.*)/$’, ‘django.contrib.syndication.views.feed’, {’feed_dict’: feeds}),

So this pattern associates the Django Syndication view with Urls that come in the form of /feeds/.

Next we need to add the “feed_dict” part to the urls.py file.

from myproject.blog.feeds import LatestEntries

feeds = {
  'latest': LatestEntries,
}

The “feed_dict”, is an dictionary that maps from feed name (the url) to the class that generates the feed content. This dictionary associates the /feeds/latest URL with the LatestEntries feed class. Now, the real magic is in writing the LatestEntries class, which we’ll create a feeds.py file and place it in the myproject/blog directory. I then put the following in my feeds.py file.

from django.contrib.syndication.feeds import Feed
from myproject.blog.models import Post

class LatestEntries(Feed):
  title = "Latest Worthless Stuff"
  link = "feeds/latest"
  description="The most recent blog entries"

  def items(self):
    return Post.objects.order_by('-pub_date')[:25]

Once that is saved, the real work is behind us. However, we still need to view the feeds, which means we have to create some templates to view them. So, to do that I created a “feeds” directory under my django_templates directory. From there I had to create two files with one line of code each. The first one is going to be called latest_title.html and looks like so:

{{ obj.title }}

The second file is called Latest_description.html and looks like the following:

{{ obj.body|truncatewords:50 }}

In the above I choose to truncate the body at 50 works, had I wanted to display the whole thing, I could have left the filter off. However, I didn’t to be long winded at time and nobody wants to see all of that from just the feed, they can read the whole post if they want something like that. At this point your ready to roll with your own RSS feeds inside Django, pretty darn easy huh. make sure you’ve run your syncdb command so that the feeds model is validated. After that browse to http:///feeds/latest, and see what you get.

So at this point we have RSS working and your users are happy as clams that they can be notified of content via your feed. But we said that we wanted feedburner to manage our feeds. Well just like everything else, Django made this pretty easy for us. This, like the feed templates takes very little code. Here’s what we need to do to get our feeds working with Django.

First head on over to feedburner and create a new feed, make sure when you enter the feed url that you enter it as http://. Once your done with that, follow through with the steps it takes to get all setup, and remember the address that you needs feeds are going to accessible at. For worthless-stuff.com it looks like http://feeds.feedburner.com/WorthlessStuff, and yours should look similar.
Now head on over to your urls.py file in your myproject directory. First thing we want to do is add the libraries that are essential for this to work. Add the following to your urls.py file.

from django.views.generic.simple import redirect_to

Once that’s added we’ll comment out our existing feed regular expression, which will look like:
# (r'^feeds/(?P.*)/$’, ‘django.contrib.syndication.views.feed’, {’feed_dict’: feeds}),
And then add the new expression, which should look like:
(r'^feeds/latest/$', redirect_to, {'url': 'http://feeds.feedburner.com/WorthlessStuff'}),
Basically we are doing exactly what the code says we are doing, redirecting a request that comes in to /feeds/latest, to feedburner. Feedburner then takes care of the rest.
So that’s it, now we have our feeds working and being monitored through feed burner. The very last step is to include the link of our feeds in the base templates. In the head of the base template where the links are for the css, add the following.

That’s it in a nutshell. If things don’t work right away make sure to restart your webserver to pick up the changes. Now enjoy some good feed management with Feedburner.

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Categories: Django

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

You must log in to post a comment.