Syndication via Atom and RSS using Django

by mark | 13 Jul 2022, 9:28 p.m.

I noticed a bunch of web crawlers getting 404s looking for atom.xml. I'd never heard of this but it is some kind of automatic feed that can be used to push updates to people. So, joining the likes of the BBC, you can now subscribe to my rubbishy posts using a news reader. 

I knew about RSS from around 20 years ago when I used to use it to have news feeds. There was some drama and the Atom specification came out. They're kind of the same thing to most people. 

Django makes it dead easy to syndicate your app. Like very easy. You can almost literally copy and paste this snippet from the django docs into a new file (I called it feeds.py).You'll need to make a few small changes. Import your model instead of the example one, and make some appropriate text changes. 

There are two changes to the default set up I would recommend. Provide appropriate publish and edit dates. Otherwise it seems to use the current date and time which messes up feeds. If your fields are named the same as mine you need the two following methods added.

    def item_pubdate(self, item):
        return item.date

    def item_updateddate(self, item):
        return item.edited_date

You then need to define urls for your new objects. Which is easy:

    path('atom.xml',AtomSiteNewsFeed()),
    path('rss.xml',RssSiteNewsFeed()),

And now you can get feeds via Atom or RSS. Hooray!

No comments

Back to all articles