Search code examples
djangofeed

Django Feed setting number of items to return in URL parameter


I am working with the Django 1.4 Feed class to return RSS feeds for my database entries. I want to let the user specify the number of feeds to return in the URL, I tried something but it isn't working for some reason. Here is what I have so far:

urls.py

urlpatterns = patterns('',
    url(r'^latest/feed/(?P<count>[0-9]+)/$',LatestPostsFeed(),name="feed"),
)

feeds.py

from django.contrib.syndication.views import Feed
from django.core.urlresolvers import reverse
from blog.models import Post
from django.utils import text, html

class LatestPostsFeed(Feed):
    title="Latest Posts"
    link="feeds"
    description="Latest posts"

    def items(self,count):
        print count
        return Post.objects.order_by('-created')[:count]

    def item_title(self,item):
        return item.title

    def item_description(self,item):
        return text.truncate_html_words(item.content,50)

    def item_link(self,item):
        return item.get_absolute_url()

When I try to get the value of count via print it returns None on the server. Where should I add my count parameter so that its value gets recognized by the class?

Thanks for the help.


Solution

  • It looks like get_object takes the kwargs you need, and is supposed to return an object describing the feed. In your case, you just need the count:

    def get_object(self, request, *args, **kwargs):
        return int(kwargs['count'])
    

    Then your items method will work.