11

What is the "django-way" of specifying channel image in rss feed? I can do it manually by rolling my own xml, but was looking for a proper way of doing it.

Edit dobrych's solution is not quite applicable here because I was asking specifically about RSS not Atom feeds

0

3 Answers 3

11

Found the right way of doing it. As the documentation describes, I needed to create a custom feed generator by subclassing from Rss201rev2Feed and overriding method

add_root_elements()

like this:

class RssFooFeedGenerator(Rss201rev2Feed):
    def add_root_elements(self, handler):
        super(RssFooFeedGenerator, self).add_root_elements(handler)
        handler.addQuickElement(u"image", '',
            {
                 'url': u"http://www.example.com/images/logo.jpg",
                 'title': u"Some title",
                 'link': u"http://www.example.com/", 
             })     

class RssFooFeed(Feed):
    feed_type = RssFooFeedGenerator
    title = u"Foo items"
    link = u"http://www.example.com/"
    description = u"Some description"
3
  • Similar approach will also work with Atom feeds by subclassing generator from Atom1Feed Mar 29, 2009 at 21:58
  • Adding url/title/link as attributes is not correct and does not work. They must be XML elements.
    – AndiDog
    Jun 17, 2012 at 9:36
  • It is displayed in chrome (mime-type=application/xml), but how to render the new element in firefox or other application (which applies some style to RSS Feed)? Oct 1, 2014 at 12:07
8

For valid RSS 2.0 you shoud use this:

class ImageRssFeedGenerator(Rss201rev2Feed):
    def add_root_elements(self, handler):
        super(ImageRssFeedGenerator, self).add_root_elements(handler)
        handler.startElement(u'image', {})
        handler.addQuickElement(u"url", self.feed['image_url'])
        handler.addQuickElement(u"title", self.feed['title'])
        handler.addQuickElement(u"link", self.feed['link'])
        handler.endElement(u'image')

class LastPublishedPromiseFeed(Feed):
    link = 'http://www.example.com'
    feed_type = ImageRssFeedGenerator

    def feed_extra_kwargs(self, obj):
        return {'image_url': self.link + '/image.jpg'}
4

I suggesting to use django-atompub for Atom feed generation. It has very nice Class abstraction with lots of options, so no any XML hacking, high-level Python code only.

Example:

# Define feed class
class StreamFeed(Feed):
    ... [snipped]
    def item_links(self, item):
        return [{'rel': 'enclosure', 'href': item.file.url, 'length': item.file.size, 'type': item.mime.name},
        {'rel': 'alternate', 'href': full_url(item.get_absolute_url())}]

I used it in my open source photoblog django app. You can see examples via bitbucket repo.

Complete feed generation code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.