Django 1.2 and the new syndication API

While mainaining backward compatibility, Django 1.2 has introduced a new Feed API which is more in tune with the rest of the framework. This post will  to explain how to migrate from the previous Django syndication API  to the new one.

The old way

In Django 1.1, an  RSS feed was something like this:

from django.contrib.syndication.feeds import Feed
from models import Entry

class LatestEntries(Feed):

You had to associate the feed to a URL with a dictionary which mapped the feed slug to the class. This made /updates.rss return our LatestEntries feed:

The syntax was a bit too verbose, and the feed URL was split between the regular expression and the dictionary key.

In addition, just to display the title and description of feed items, you had to create two templates named feeds/(feed_slug)_title.html and feeds/(feed_slug)_description.html. So your template directory would get filled with files containing in most cases almost no code.

What’s new in 1.2

In Django 1.2, Feeds are regular views, so to use the new syndication API, change the import statement to read:

The method to map a feed to a url is now much less verbose. No need for a dictionary; just pass an instance of your Feed subclass:

Finally, to display the title and description, instead of templates, you can just define two new methods in your Feed subclass. Supposing that the Entry class has a title and content attribute,  and that you want to use those for your RSS item title and content, you would write:

The new syndication API in Django 1.2 allows you to use much simpler syntax for url mapping, and cuts back on superfluous templates. But as you are not forced to change your Feed subclasses declaration in any way, it is also easy to switch from the old API.