2012-04-11 27 views
0

我是一個noob,所以請原諒,如果這是一個愚蠢的要求。我正在嘗試爲網站的每個類別創建自定義RSS訂閱源,但不知何故,我無法通過參數(類別段落)來正確構建請求的訂閱源。 RSS的應設在這樣一個地址:http://www.website.com/category-name/feed自定義Django RSS不起作用

這是我有:

在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 _()得到了一個意外的關鍵字參數'category_slug'」,但回溯沒有幫助,它只顯示一些ba se python的東西。 謝謝。

回答

1

從DOC: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()), 
) 
+0

好了,但如果我這樣做,然後我得到: 「__init __()到底需要3個參數(1給出)」 – 2012-04-11 14:45:11

+0

啊,你還使用過時的飼料類。使用'from django.contrib.syndication.views import feed' not'from django.contrib.syndication.feeds import Feed' – 2012-04-11 14:52:33

+0

非常感謝,這對我們有很大的幫助! – 2012-04-11 15:56:52