Django Syndication with Colddirt II

Published on June 3, 2007

Since I've already covered a really simple syndication example, I'll move onto something a little more complex. Let's say you want to offer syndication that is slightly more custom. The Django syndication docs give an example from Adrian's Chicagocrime.org syndication of beats. I had to ponder a minute to get "custom" syndication to work, so here's my example from start to finish.

First, as usual, feeds.py

feeds.py

class PerDirt(Feed):

    link = "/"
    copyright = 'Copyright (c) 2007, Blog Mozaic'
    
    def get_object(self, bits):
        from django.shortcuts import get_object_or_404
        if len(bits) != 1:
            raise ObjectDoesNotExist
        my_dirt = get_object_or_404(Dirt, slug__exact=bits[0])
        return my_dirt

    def title(self, obj):
        return obj.slug
    
    def description(self, obj):
        return obj.description
    
    def items(self, obj):
        from django.contrib.comments.models import FreeComment
        return FreeComment.objects.filter(object_id=obj.id).order_by('-submit_date')

You can see that this differs slightly from the simpler syndication example. I'll not a few things. But first, I need to show urls.py:

urls.py

feeds = {
    'mydirt': PerDirt,
}
    (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds}),

Let's pretend the dirt word (or if you were doing a blog, you could do this based on slug) is "nifty". So, the process is like this: a request comes in as /feeds/mydirt/nifty/ -- it is first looked up in the feed_dict (because of the mydirt part) and then sent to PerDict. Once in PerDict it hits the first def, get_object. One of the things that confused me at first is what the 'bits' part is. Simply put: it is the crap after you tell Django which feed to use. Similar to the beats example, I'm testing to make sure the length is only one -- so if the word doesn't exist or somebody just types in feeds/mydirt/nifty/yeehaaa/ -- they will get an error. Next the object is looked up, in this case the dirt word (in your case, maybe a blog entry).

The title and description are self-explanatory. The items are a query from the FreeComment database, ordered by date. What we need next is the correct templates.

templates/feeds/mydirt_title.html

Comment by {{ obj.person_name }}

Once again, the filename is important (mydirt_title). obj.person_name is the name from the comment.

templates/feeds/mydirt_description.html

{{ obj.comment }}

Posted by: {{ obj.person_name }}
Published: {{ obj.submit_date }}

And that is the comment.

templates/dirt_detail.html

If you are curious how to get that little orange icon next to your site's url, you do this:

<link rel="alternate" type="application/rss+xml" title="Your Dirt" href="/feeds/mydirt/{{ dirt_object.dirtword }}/" />

That's it. Hopefully I've explained how to create somewhat custom syndication feeds, in case you needed another example.



Tagged as: django | xml

Comments are currently closed for this entry.

About this page

This entry is from my tech blog and was written on June 3, 2007. It's been tagged with django and xml. There have been 0 comments so far.

Via Twitter

Twitter contact import script seems to be broken and spamming my requests - apologies if I sent you 10 emails; I'm stubborn like that. (about 1 week, 1 day ago)

Related Readings

Book Book Book Book