开发者

Django 1.1.1's Feed only displays the latest item, while there are many

开发者 https://www.devze.com 2023-01-10 18:13 出处:网络
I\'ve created a Feed subclass to export a simple feed of news #urls.py from django.conf.urls.defaults import *

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:

  • http://validator.w3.org/feed/check.cgi?url=http://www.sshguard.net/litenewz/feeds/news/


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/</guid></item></channel></rss>

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', (), {})


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号