RSS Feed Is Live (Posted on February 16th, 2013)

Over the past few weeks I've been getting requests for an RSS feed. Luckily Django provides a super simple way to convert my blog posts to RSS and Atom feeds.

#urls.py
from core.feeds import rss_feed, atom_feed

urlpatterns += patterns('',
    url(r'^rss/$', rss_feed()),
    url(r'^atom/$', atom_feed()),
)

#feeds.py
from core.models import Entry

from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Atom1Feed
from django.utils import text

class rss_feed(Feed):
    title = "Max Burstein's Blog"
    link = "/blog/"
    description = "Latest posts from Max Burstein's technical blog"
    
    def items(self):
        return Entry.objects.filter(published=True)[:5]
    
    def item_description(self, item):
        return text.truncate_html_words(item.content, 83)
    
class atom_feed(rss_feed):
    feed_type = Atom1Feed
    subtitle = rss_feed.description

As usual the Django docs come through with awesome examples of how to create an RSS feed. I only had to make a few modifications to get it to work the way I wanted.

One of the cooler things I learned was how to enable auto discovery of feeds. This enables you to go to your feed reader and just type maxburstein.com to pull in my feed rather than having to know the exact location of my feed. It was as simple as adding these two lines to the top of my HTML files.

<link rel="alternate" type="application/rss+xml" href="/rss/" />
<link rel="alternate" type="application/atom+xml" href="/atom/" />

Enjoy!

Tags: Django