Adding a per-post comments feed with Django 1.0
I've added an Atom feed for comments on a single post on this blog (per a request in the comments). Here are my notes. I am using Django 1.0. (Note: the Django feeds framework has changed for 1.2. See the Django 1.2 release notes for more information.)
Added to /srv/SaltyCrane/iwiwdsmi/feeds.py
:
from django.contrib.comments.models import Comment
from django.contrib.syndication.feeds import Feed, FeedDoesNotExist
from django.utils.feedgenerator import Atom1Feed
from django.core.exceptions import ObjectDoesNotExist
from iwiwdsmi.myblogapp.models import Post
from iwiwdsmi.settings import SITE_NAME
class CommentsByPost(Feed):
title_template = "feeds/comments_title.html"
description_template = "feeds/comments_description.html"
feed_type = Atom1Feed
def get_object(self, bits):
if len(bits) != 1:
raise ObjectDoesNotExist
return Post.objects.get(id=bits[0])
def title(self, obj):
return '%s' % obj
def description(self, obj):
return 'Comments on "%s": %s' % (obj, SITE_NAME)
def link(self, obj):
if not obj:
raise FeedDoesNotExist
return obj.get_absolute_url() + "#comments"
def items(self, obj):
return Comment.objects.filter(object_pk=str(obj.id),
is_public=True,
is_removed=False,
).order_by('-submit_date')
def item_pubdate(self, item):
return item.submit_date
Added to /srv/SaltyCrane/iwiwdsmi/urls.py
:
from iwiwdsmi.feeds import CommentsByPost
feeds = {
'comments': CommentsByPost,
}
urlpatterns = patterns(
'',
(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
{'feed_dict': feeds}),
)
Added /srv/SaltyCrane/iwiwdsmi/templates/feeds/comments_title.html
:
Comment by {{ obj.name|escape }}
Added /srv/SaltyCrane/iwiwdsmi/templates/feeds/comments_description.html
:
{% load markup %}
{{ obj.comment|markdown:"safe" }}
Added to /srv/SaltyCrane/iwiwdsmi/templates/myblogapp/singlepost.html
:
<a href="/feeds/comments/{{ post.id }}/">
<img src="http://saltycrane.s3.amazonaws.com/image/icon_feed_orange_14x14_1.png" style="border: 0pt none ; vertical-align: middle;" alt="feed icon">
Comments feed for this post</a>