Django Atom Feeds
November 22, 2009
Django has built in support for exposing Atom feeds
feeds.py
Create a feeds.py in the app you want to export feeds from.
from django.utils.feedgenerator import Atom1Feed from django.contrib.sites.models import Site from django.contrib.syndication.feeds import Feed from site.blog.models import Blog current_site = Site.objects.get_current() class LatestEntriesFeed(Feed): author_name = "Title of the Feed" description = "Latest Entries posted to %s" % current_site.name feed_type = Atom1Feed link = "/feeds/entries/" title = "%s : Latest Entries" % current_site.name def items(self): return Blog.objects.all()[:15] def item_pubdate(self, pItem): return pItem.publication_date def item_guid(self, pItem): return "tag:%s,%s:%s" % (current_site.domain, pItem.publication_date.strftime('%Y-%m-%d'), pItem.get_absolute_url())
urls.py
In the global urls.py, import that class and then refer to it from a url
from site.home.feeds import LatestEntriesFeed feeds = {'entries' : LatestEntriesFeed} urlpatterns += patterns('', (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict':feeds}), )
Note that the url defined in the LatestEntriesFeed class is /feeds/entries/, and that the url spec shows /feeds/ whilst the name of the feed in the dictionary is entries.