Only show posts tagged with: metablogging, english, sotramont, francais, linux, ubuntu, geeky, web, python, django, screwtheman, spam sucks, vélo, akoha, hiring, chicago, pycon, cloud, consulting, quacks
Older posts:
Adding RSS feeds was super easy, with Django's syndication framework in the contrib applications. I wanted to be able to have RSS feeds that would only follow entries tagged with something, so that you could only follow my entries written in english or that are about django. It turns out i got that done with a 7 line change.
Go check out the syndication framework documentation on how to create feeds. It's simple. In my case all i did was create a blog.feeds.WeblogEntryFeed class, which inherits from django.contrib.syndication.feeds. Feed and overrides the items method which returns a queryset with the entries to put in the feed. I then defined a global /rss/app url. The app part of that url refers to a dict entry, where i do the mapping from /rss/blog to the WeblogEntryFeed class. The view is from the syndication framework directly - i didn't do any overrides -, and it just does the rest.
To be able to get a feed for blog entries tagged with django, at the url /rss/blog/django, all i had to do was:
--- a/blog/feeds.py
+++ b/blog/feeds.py
@@ -12,7 +12,14 @@ class WeblogEntryFeed(Feed):
description = "Rien à déclarer."
def items(self):
- return Entry.objects.filter(pub_date__lte=datetime.datetime.now())[:10]
+ q = Entry.objects.filter(pub_date__lte=datetime.datetime.now())
+ if self.tag:
+ q = q.filter(tags=self.tag)
+ return q[:10]
+
+ def get_feed(self, tag=None):
+ self.tag = tag
+ return super(WeblogEntryFeed, self).get_feed()
et voilà! That's it. The syndication framework's feed view looks for anything after the application name in the url, and feeds that into the feed object's get_feed method as a parameter. For a blog feed, i just save that, knowing it's a tag, and then call the inherited get_feed, which eventually calls items(), which will now filter down the entries queryset with the tag.
The only thing left to do was to publicize this RSS url at the top of the page if you click on a tag in the blog (which shows you a list of entries for that tag) so people can use it.
by wiswaud
on 15 April 2009
Tags:
django, english, geeky, metablogging, python, web