Django comes with a high-level syndication-feed-generating framework that makes creating RSS and Atom feeds easy. RSS and Atom are both XML-based formats you can use to provide automatically updating feeds of your site’s content. Read more about RSS here, and get information on Atom here.
To create any syndication feed, all you have to do is write a short Python class. You can create as many feeds as you want. Django also comes with a lower-level feed-generating API. Use this if you want to generate feeds outside of a Web context, or in some other lower-level way.
The High-Level Framework
The high-level feed-generating framework is supplied by the Feed class. To create a feed, write a Feed class and point to an instance of it in your URLconf.
Feed Classes – A Feed class is a Python class that represents a syndication feed. A feed can be simple (e.g., a site news feed, or a basic feed displaying the latest entries of a blog) or more complex (e.g., a feed displaying all the blog entries in a particular category, where the category is variable). Feed classes subclass django.contrib.syndication.views.Feed. They can live anywhere in your codebase. Instances of Feed classes are views which can be used in your URLconf.
A Simple Example – This simple example, taken from a hypothetical police beat news site describes a feed of the latest five news items:
from django.contrib.syndication.views import Feed
from django.core.urlresolvers import reverse
from policebeat.models import NewsItem
class LatestEntriesFeed(Feed):
title = “Police beat site news”
link = “/sitenews/”
description = “Updates on changes and additions to police beat central.”
def items(self):
return NewsItem.objects.order_by(‘-pub_date’)[:5]
def item_title(self, item):
return item.title
def item_description(self, item):
return item.description
# item_link is only needed if NewsItem has no get_absolute_url method.
def item_link(self, item):
return reverse(‘news-item’, args=[item.pk])
To connect a URL to this feed, put an instance of the Feed object in your URLconf. For example:
from django.conf.urls import url
from myproject.feeds import LatestEntriesFeed
urlpatterns = [
# …
url(r’^latest/feed/$’, LatestEntriesFeed()),
# …
]
Note:
- The Feed class subclasses django.contrib.syndication.views.Feed.
- title, link and description correspond to the standard RSS <title>, <link> and <description> elements, respectively.
- items() is, simply, a method that returns a list of objects that should be included in the feed as <item> elements. Although this example returns NewsItem objects using Django’s object-relational mapper doesn’t have to return model instances. Although you get a few bits of functionality for free by using Django models, items() can return any type of object you want.
- If you’re creating an Atom feed, rather than an RSS feed, set the subtitle attribute instead of the description attribute.
One thing is left to do. In an RSS feed, each <item> has a <title>, <link> and <description>. We need to tell the framework what data to put into those elements.
For the contents of <title> and <description>, Django tries calling the methods item_title() and item_description() on the Feed class. They are passed a single parameter, item, which is the object itself. These are optional; by default, the unicode representation of the object is used for both.
If you want to do any special formatting for either the title or description, Django templates can be used instead. Their paths can be specified with the title_template and description_template attributes on the Feed class. The templates are rendered for each item and are passed two template context variables:
- {{ obj }} – The current object (one of whichever objects you returned in items()).
- {{ site }} – A Django site object representing the current site. This is useful for {{ site.domain }} or {{ site.name }}.
See “a complex example” below that uses a description template.
There is also a way to pass additional information to title and description templates, if you need to supply more than the two variables mentioned before. You can provide your implementation of get_context_data method in your Feed subclass. For example:
from mysite.models import Article
from django.contrib.syndication.views import Feed
class ArticlesFeed(Feed):
title = “My articles”
description_template = “feeds/articles.html”
def items(self):
return Article.objects.order_by(‘-pub_date’)[:5]
def get_context_data(self, **kwargs):
context = super(ArticlesFeed, self).get_context_data(**kwargs)
context[‘foo’] = ‘bar’
return context
And the template:
Something about {{ foo }}: {{ obj.description }}
- This method will be called once per item in the list returned by items() with the following keyword arguments:
item: the current item. For backward compatibility reasons, the name of this context variable is {{ obj }}. - obj: the object returned by get_object(). By default, this is not exposed to the templates to avoid confusion with {{ obj }} , but you can use it in your implementation of get_context_data().
- site: current site as described above.
- request: current request.
The behavior of get_context_data() mimics that of generic views – you’re supposed to call super() to retrieve context data from the parent class, add your data and return the modified dictionary.
To specify the contents of <link>, you have two options. For each item in items(), Django first tries calling the item_link() method on the Feed class. In a similar way to the title and description, it’s passed a single parameter – item. If that method doesn’t exist, Django tries executing a get_absolute_url() method on that object.
Both get_absolute_url() and item_link() should return the item’s URL as a normal Python string. As with get_absolute_url(), the result of item_link() will be included directly in the URL, so you are responsible for doing all necessary URL quoting and conversion to ASCII inside the method itself.
A Complex Example – The framework also supports more complex feeds, via arguments. For example, a website could offer an RSS feed of recent crimes for every police beat in a city. It’d be silly to create a separate Feed class for each police beat; that would violate the DRY principle and would couple data to programming logic.
Instead, the syndication framework lets you access the arguments passed from your URLconf so feeds can output items based on information in the feed’s URL. The police beat feeds could be accessible via URLs like this:
- /beats/613/rss/ – Returns recent crimes for beat 613.
- /beats/1424/rss/ – Returns recent crimes for beat 1424.
These can be matched with a URLconf line such as:
url(r’^beats/(?P[0-9]+)/rss/$’, BeatFeed()),
Like a view, the arguments in the URL are passed to the get_object() method along with the request object. Here’s the code for these beat-specific feeds:
from django.contrib.syndication.views import FeedDoesNotExist
from django.shortcuts import get_object_or_404
class BeatFeed(Feed):
description_template = ‘feeds/beat_description.html’
def get_object(self, request, beat_id):
return get_object_or_404(Beat, pk=beat_id)
def title(self, obj):
return “Police beat central: Crimes for beat %s” % obj.beat
def link(self, obj):
return obj.get_absolute_url()
def description(self, obj):
return “Crimes recently reported in police beat %s” % obj.beat
def items(self, obj):
return Crime.objects.filter(beat=obj).order_by(‘-crime_date’)[:30]
To generate the feed’s <title>, <link> and <description>, Django uses the title(), link() and description() methods.
In the previous example, they were simple string class attributes, but this example illustrates that they can be either strings or methods. For each of title, link and description, Django follows this algorithm:
- First, it tries to call a method, passing the obj argument, where obj is the object returned by get_object().
- Failing that, it tries to call a method with no arguments.
- Failing that, it uses the class attribute.
Also note that items() also follows the same algorithm – first, it tries items(obj), then items(), then finally an items class attribute (which should be a list). We are using a template for the item descriptions. It can be very simple:
{{ obj.description }}
However, you are free to add formatting as desired. The ExampleFeed class below gives full documentation on methods and attributes of Feed classes.
Specifying the Type of Feed – By default, feeds produced in this framework use RSS 2.0. To change that, add a feed_type attribute to your Feed class, like so:
from django.utils.feedgenerator import Atom1Feed
class MyFeed(Feed):
feed_type = Atom1Feed
Note that you set feed_type to a class object, not an instance. Currently available feed types are:
django.utils.feedgenerator.Rss201rev2Feed (RSS 2.01. Default.)
django.utils.feedgenerator.RssUserland091Feed (RSS 0.91.)
django.utils.feedgenerator.Atom1Feed (Atom 1.0.)
Enclosures – To specify enclosures, such as those used in creating podcast feeds, use the item_enclosure_url, item_enclosure_length and item_enclosure_mime_type hooks.
Language – Feeds created by the syndication framework automatically include the appropriate <language> tag (RSS 2.0) or xml:lang attribute (Atom). This comes directly from your LANGUAGE_CODE setting.
urls – The link method/attribute can return either an absolute path (e.g. /blog/) or a URL with the fully-qualified domain and protocol (e.g. http://www.example.com/blog/). If link doesn’t return the domain, the syndication framework will insert the domain of the current site, according to your SITE_ID setting. Atom feeds require a <link rel=”self”> that defines the feed’s current location. The syndication framework populates this automatically, using the domain of the current site according to the SITE_ID setting.
Publishing Atom and RSS Feeds in Tandem – Some developers like to make available both Atom and RSS versions of their feeds. That’s easy to do with Django: Just create a subclass of your Feed class and set the feed_type to something different. Then update your URLconf to add the extra versions. Here’s a full example:
from django.contrib.syndication.views import Feed
from policebeat.models import NewsItem
from django.utils.feedgenerator import Atom1Feed
class RssSiteNewsFeed(Feed):
title = “Police beat site news”
link = “/sitenews/”
description = “Updates on changes and additions to police beat central.”
def items(self):
return NewsItem.objects.order_by(‘-pub_date’)[:5]
class AtomSiteNewsFeed(RssSiteNewsFeed):
feed_type = Atom1Feed
subtitle = RssSiteNewsFeed.description
In this example, the RSS feed uses a description while the Atom feed uses a subtitle. That’s because Atom feeds don’t provide for a feed-level description, but they do provide for a subtitle. If you provide a description in your Feed class, Django will not automatically put that into the subtitle element, because a subtitle and description are not necessarily the same thing. Instead, you should define a subtitle attribute.
In the above example, we simply set the Atom feed’s subtitle to the RSS feed’s description, because it’s quite short already. And the accompanying URLconf:
from django.conf.urls import url
from myproject.feeds import RssSiteNewsFeed, AtomSiteNewsFeed
urlpatterns = [
# …
url(r’^sitenews/rss/$’, RssSiteNewsFeed()),
url(r’^sitenews/atom/$’, AtomSiteNewsFeed()),
# …
]
The Low-Level Framework
Behind the scenes, the high-level RSS framework uses a lower-level framework for generating feeds’ XML. This framework lives in a single module: django/utils/feedgenerator.py. You use this framework on your own, for lower-level feed generation. You can also create custom feed generator subclasses for use with the feed_type Feed option.
SyndicationFeed classes – The feedgenerator module contains a base class:
- django.utils.feedgenerator.SyndicationFeed and several subclasses:
- django.utils.feedgenerator.RssUserland091Feed
- django.utils.feedgenerator.Rss201rev2Feed
- django.utils.feedgenerator.Atom1Feed
Each of these three classes knows how to render a certain type of feed as XML. They share this interface:
SyndicationFeed.init() – Initialize the feed with the given dictionary of metadata, which applies to the entire feed. Required keyword arguments are:
- title
- link
- description
There’s also a bunch of other optional keywords:
- language
- author_email
- author_name
- author_link
- subtitle
- categories
- feed_url
- feed_copyright
- feed_guid
- ttl
Any extra keyword arguments you pass to __init__ will be stored in self.feed for use with custom feed generators. All parameters should be Unicode objects, except categories, which should be a sequence of Unicode objects.
SyndicationFeed.add_item() – Add an item to the feed with the given parameters.
Required keyword arguments are:
- title
- link
- description
Optional keyword arguments are:
- author_email
- author_name
- author_link
- pubdate
- comments
- unique_id
- enclosure
- categories
- item_copyright
- ttl
- updateddate
Extra keyword arguments will be stored for custom feed generators. All parameters, if given, should be Unicode objects, except: - pubdate should be a Python datetime object.
- updateddate should be a Python datetime object.
- enclosure should be an instance of django.utils.feedgenerator.Enclosure.
- categories should be a sequence of Unicode objects.
SyndicationFeed.write() – Outputs the feed in the given encoding to outfile, which is a file-like object.
SyndicationFeed.writeString() – Returns the feed as a string in the given encoding. For example, to create an Atom 1.0 feed and print it to standard output:
>> from django.utils import feedgenerator
>>> from datetime import datetime
>>> f = feedgenerator.Atom1Feed(
… title=”My Weblog”,
… link=”http://www.example.com/”,
… description=”In which I write about what I ate today.”,
… language=”en”,
… author_name=”Myself”,
… feed_url=”http://example.com/atom.xml”)
>>> f.add_item(title=”Hot dog today”,
… link=”http://www.example.com/entries/1/”,
… pubdate=datetime.now(),
… description=”<p>Today I had a Vienna Beef hot dog. It was pink, plump and per\
fect.</p>”)
>>> print(f.writeString(‘UTF-8’))
<?xml version=”1.0″ encoding=”UTF-8″?>
<feed xmlns=”http://www.w3.org/2005/Atom” xml:lang=”en”>
…
</feed>
Custom feed generators
If you need to produce a custom feed format, you’ve got a couple of options. If the feed format is totally custom, you’ll want to subclass SyndicationFeed and completely replace the write() and writeString() methods. However, if the feed format is a spin-off of RSS or Atom (i.e. GeoRSS, Apple’s iTunes podcast format etc.), you’ve got a better choice.
These types of feeds typically add extra elements and/or attributes to the underlying format, and there are a set of methods that SyndicationFeed calls to get these extra attributes. Thus, you can subclass the appropriate feed generator class (Atom1Feed or Rss201rev2Feed) and extend these call-backs. They are:
SyndicationFeed.root_attributes(self, ) – Return a dict of attributes to add to the root feed element (feed/channel).
SyndicationFeed.add_root_elements(self, handler) – Callback to add elements inside the root feed element (feed/channel). handler is an XMLGenerator from Python’s built-in SAX library; you’ll call methods on it to add to the XML document in process.
SyndicationFeed.item_attributes(self, item) – Return a dict of attributes to add to each item (item/entry) element. The argument, item, is a dictionary of all the data passed to SyndicationFeed.add_item().
SyndicationFeed.add_item_elements(self, handler, item) – Callback to add elements to each item (item/entry) element. handler and item are as above.
For example, you might start implementing an iTunes RSS feed generator like so:
class iTunesFeed(Rss201rev2Feed):
def root_attributes(self):
attrs = super(iTunesFeed, self).root_attributes()
attrs[‘xmlns:itunes’] =
‘http://www.itunes.com/dtds/podcast-1.0.dtd’
return attrs
def add_root_elements(self, handler):
super(iTunesFeed, self).add_root_elements(handler)
handler.addQuickElement(‘itunes:explicit’, ‘clean’)
Obviously there’s a lot more work to be done for a complete custom feed class, but the above example should demonstrate the basic idea.