0

我是菜鸟,所以如果这是一个愚蠢的要求,请原谅。我正在尝试为网站的每个类别创建自定义 RSS 提要,但不知何故我无法传递参数(类别 slug)以正确构建请求的提要。RSS 应位于如下地址:http ://www.website.com/category-name/feed

这是我所拥有的:

在 urls.py 中:

from project.feeds import FeedForCategory
urlpatterns = patterns('category.views',
 #...
 url(r'^(?P<category_slug>[a-zA-Z0-9\-]+)/feed/?$', FeedForCategory),
)

在 feeds.py 中:

from django.contrib.syndication.feeds import Feed

class FeedForCategory(Feed):

  def get_object(self, request, category_slug):
    return get_object_or_404(Category, slug_name=category_slug)

  def title(self, obj):
    return "website.com - latest stuff"

  def link(self, obj):
    return "/articles/"

  def description(self, obj):
    return "The latest stuff from Website.com"

  def get_absolute_url(self, obj):
    return settings.SITE_ADDRESS + "/articles/%s/" % obj.slug_name

  def items(self, obj):
    return Article.objects.filter(category=category_slug)[:10]

我得到的错误是:“_ init _() got an unexpected keyword argument 'category_slug'”,但回溯没有帮助,它只显示了一些基本的 python 内容。谢谢你。

4

1 回答 1

1

来自文档:https ://docs.djangoproject.com/en/dev/ref/contrib/syndication/

您需要将提要对象的实例传递给您的 url 模式。所以在 urls.py 中这样做:

from project.feeds import FeedForCategory
urlpatterns = patterns('category.views',
 #...
url(r'^(?P<category_slug>[a-zA-Z0-9\-]+)/feed/?$', FeedForCategory()),
)
于 2012-04-11T14:24:09.797 回答