tags:

views:

39

answers:

2

I've created a Feed subclass to export a simple feed of news

#urls.py
from django.conf.urls.defaults import *

from litenewz.feeds import NewsFeed

feeds = {
    'news': NewsFeed,
}

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

#feeds.py
from django.contrib.syndication.feeds import Feed

from litenewz.models import News

class NewsFeed(Feed):
    title = 'example news'
    link = 'http://example.net'
    description = 'Latest Newz from example.'

    item_author_name = '...'
    item_author_email = '...'
    item_author_link = 'http://example.net'

    def items(self):
        return News.objects.order_by('-time')[:15]

#models.py
from django.db import models
from django.utils.translation import ugettext as _

from datetime import datetime

class News(models.Model):
    class Meta:
        ordering = ('-time', )
        verbose_name = _('News')
        verbose_name_plural = _('News')

    title = models.CharField(
        _('Title'),
        max_length=512)
    time = models.DateTimeField(
        _('Date and time'),
        default=datetime.now
        )
    content = models.TextField(
        _('Content'))

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        return ('home', (), {})

As you can see the Feed subclass' items() method returns the first 15 objects in News.objects.order_by('-time'):

def items(self):
    return News.objects.order_by('-time')[:15]

Nevertheless, only one item is exported in the feed: hxxp://www.sshguard.net/litenewz/feeds/news/

Unfortunately, there are two objects of the News model:

>>> from litenewz.models import *
>>> News.objects.all()
[<News: Version 1.5 and timing>, <News: SSHGuard news>]

Any help?

I prefer not to switch to Django 1.2, unless this is strictly necessary to solve the issue described.

Update: the RSS returned indeed contains both the objects, but the RSS is not valid and thus readers such as Safari's are fooled:

A: 

The validation error appears to be from the <guid> element in each item of your feed. As far as I can tell, this is auto-generated from the get_absolute_url() method of the model, in your case the News model. Have you defined that method? If so, is it actually returning a distinct URL for each item?

Daniel Roseman
+1  A: 

Looks like it's not valid because you've not generated your guid correctly:

The validator says:

This feed does not validate.
line 25, column 83: guid values must not be duplicated within a feed:
... )</author><guid>http://www.sshguard.net/&lt;/guid&gt;&lt;/item&gt;&lt;/channel&gt;&lt;/rss&gt;

The reason is that your get_absolute_url method on your News model returns the same thing for each News instance - do you have unique URLs for each News item? If so you should use that rather than:

def get_absolute_url(self):
    return ('home', (), {})
Dominic Rodger
I do not have a distinct URL for each item. However, if this solves the issues I could just create unique dummy URLs.
phretor