Reputation: 604
I want to generate a feed of latest entries of a blog post under a particular tag. I used django-tagging. How can i do this? Here is how i defined my LatestEntriesFeed
from django.core.exceptions import ObjectDoesNotExist
from django.utils.feedgenerator import Atom1Feed
from django.contrib.sites.models import Site
from django.contrib.syndication.feeds import Feed
from articles.models import Entry
current_site = Site.objects.get_current()
class LatestEntriesFeed(Feed):
title = 'Latest Entries for %s' % current_site
link = '/feeds/latest/'
description = 'Latest entries posted.'
def items(self):
return Entry.live.all()[:100]
def item_pubdate(self, item):
return item.pub_date
def item_guid(self, item):
return "tag:%s,%s:%s" % (current_site.domain,
item.pub_date.strftime('%Y-%m-%d'),
item.get_absolute_url())
Upvotes: 1
Views: 235
Reputation: 604
After realizing how get_object() works i finally make it work. I added some imports:
from django.core.exceptions import ObjectDoesNotExist
from tagging.models import Tag, TaggedItem
class TagFeed(LatestEntriesFeed):
def get_object(self, bits):
if len(bits) != 1:
raise ObjectDoesNotExist
return Tag.objects.get(name__exact=bits[0])
def title(self, obj):
return "%s: Latest entries under the tag '%s'" % (current_site.name, obj.name)
def description(self, obj):
return "%s: Latest entries under the tag '%s'" % (current_site.name, obj.name)
def items(self, obj):
return TaggedItem.objects.get_by_model(Entry, obj.name)
Lets say I access /feeds/tag/thetagnamehere/ then get_object will fetch tag object with name "thetagnamehere". Method items() will then fetch Entries under the tag "thetagnamehere". I also created feeds/tag_title.html and feeds/tag_description.html in my templates directory. In my project urls.py:
feeds = {
'latest': LatestEntriesFeed,
'tag': TagFeed,
}
(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
{'feed_dict': feeds}, ),
That's it. Im now able to generate a feed for a particular tag in my sidebar. I hope that helps.
Upvotes: 2
Reputation: 5344
Change your items
method to the folowing:
from tagging.models import Tag, TaggedItem
def items(self):
tag = Tag.objects.get(name='you tag name')
return TaggedItem.objects.get_by_model(Entry, tag)
Upvotes: 1