Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Django RSS feed example
from django.shortcuts import get_object_or_404
from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Rss201rev2Feed
from myproject.myapp.models import Model, Broadcast, \
class OfficialFeed(Rss201rev2Feed):
def add_root_elements(self, handler):
Rss201rev2Feed.add_root_elements(self, handler)
handler.startElement(u'image', {})
handler.addQuickElement(u"url", OFFICIAL_LOGO_URL)
handler.addQuickElement(u"title", u'MyProject')
handler.addQuickElement(u"link", OFFICIAL_URL)
class BSAFeed(Feed):
feed_type = OfficialFeed
def get_object(self, request, *args, **kwargs):
user_slug = request.META['PATH_INFO'].split('/')[2]
user = get_object_or_404(Model, slug=user_slug)
return user
def link(self, obj):
return '/user/{}/broadcasts/feed/'.format(obj.slug)
def title(self, obj):
return "%s Broadcasts Feed" %
def description(self, obj):
return "Updates on %s social media activity" %
def items(self, obj):
return BroadcastSocialAccount.objects.filter(broadcast__user=obj,
def item_title(self, item):
return "{} posted on {}".format(,
def item_description(self, item):
return item.broadcast.message
# item_link is only needed if NewsItem has no get_absolute_url method.
def item_link(self, item):
return item.get_broadcast_abs_url()
def item_pubdate(self, item):
return item.sent_at
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment