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>
2
Comments
—
Comments feed for this post
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(5)
-
aws
(9)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(13)
-
conkeror
(2)
-
core
(2)
-
cygwin
(17)
-
datastructures
(14)
-
datetime
(4)
-
decorators
(4)
-
django
(40)
-
emacs
(22)
-
files_directories
(11)
-
git
(5)
-
hardware
(5)
-
install_setup
(8)
-
javascript
(3)
-
keyboard
(9)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
persistence
(5)
-
preferences
(7)
-
processes
(4)
-
pyqt
(18)
-
python
(144)
-
ratpoison
(3)
-
regexes
(6)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(14)
-
ssh
(10)
-
subversion
(6)
-
twisted
(7)
-
ubuntu
(65)
-
urxvt
(5)
-
vxworks
(25)
-
webdev
(5)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Marty Alchin
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan
#1 PATX commented on 2010-04-21:
This is very cool, way to go! Thanks!