blog stats
seamusc.com

RSS feeds in Django

Published on May 22, 2007

Adding RSS feeds to your site is another thing that django makes so easy for you. To start off add

(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
    {'feed_dict': feeds}),

to your urls.py file

You'll also need to define the dictionary feeds which will map the <url> part of the rule above to the class which defines that feed. You can define these classes anywhere you like but a sensible place would be in a file called feeds.py in the same directory as you urls.py file.


This file should be of the format

from django.contrib.syndication.feeds import Feed
from Blog.models import BlogPost
class LatestEntries(Feed):
    title = "Latest Entries"
    link = "/blog/"
    description = "Latest Blog Posts from mysite.com"

    def items(self):
        return BlogPost.objects.order_by('-pub_date')[:5]


Import this class into your urls.py file

from PROJECT.feeds import *


Your feeds dictionary in urls.py will then be

feeds = {
    'latest': LatestEntries,
}


The LatestEntries feed will be at the url http://mysite.com/feeds/latest/


Comments