Django 联合框架:如何使用新的基于类的提要视图?
Django 1.2 在联合框架中带来了一些变化。根据 this,我现在应该能够做类似的事情:
from django.conf.urls.defaults import *
from myproject.feeds import LatestEntries, LatestEntriesByCategory
urlpatterns = patterns('',
# ...
(r'^feeds/latest/$', LatestEntries()),
(r'^feeds/categories/(?P<category_id>\d+)/$', LatestEntriesByCategory()),
)
但是当我尝试按照这些思路做任何事情,我收到错误:
init() 恰好需要 3 个参数(给定 1 个)
任何人都可以给我一个有效的示例吗?或者也许有人理解这个错误与什么相关?
编辑 #1
上面的示例实际上来自 Django Advent 链接。我尝试了多种方法,但都出现相同的错误。但一个简单的无效示例是:
urls.py
urlpatterns = patterns('',
url(r'^feeds/comments/$', LatestCommentsFeed()),
)
feeds.py
class LatestCommentsFeed(Feed):
description = "Latest comments left at %s" % current_site.name
feed_type = Atom1Feed
link = "/feeds/comments/"
title = "%s: Latest comments" % current_site.name
def items(self):
return Comment.objects.filter(is_public=True).order_by('-submit_date')[:50]
def item_pubdate(self,item):
return item.submit_date
def item_guid(self,item):
return "tag:%s,%s:%s" % (current_site.domain,
item.submit_date.strftime('%Y-%m-%d'),
item.get_absolute_url())
Django 1.2 has brought in some changes in the syndication framework. According to this, I should now be able to do something like:
from django.conf.urls.defaults import *
from myproject.feeds import LatestEntries, LatestEntriesByCategory
urlpatterns = patterns('',
# ...
(r'^feeds/latest/
But when I try to do anything along those lines, I get an error:
init() takes exactly 3 arguments (1 given)
Can anyone give me a working example? Or perhaps someone understands what this error relates to?
Edit #1
The example above is actually from the Django Advent link. I've tried a variety of things and all of them spout the same error. But a simple non-working example would be:
urls.py
urlpatterns = patterns('',
url(r'^feeds/comments/
feeds.py
class LatestCommentsFeed(Feed):
description = "Latest comments left at %s" % current_site.name
feed_type = Atom1Feed
link = "/feeds/comments/"
title = "%s: Latest comments" % current_site.name
def items(self):
return Comment.objects.filter(is_public=True).order_by('-submit_date')[:50]
def item_pubdate(self,item):
return item.submit_date
def item_guid(self,item):
return "tag:%s,%s:%s" % (current_site.domain,
item.submit_date.strftime('%Y-%m-%d'),
item.get_absolute_url())
, LatestEntries()),
(r'^feeds/categories/(?P<category_id>\d+)/
But when I try to do anything along those lines, I get an error:
init() takes exactly 3 arguments (1 given)
Can anyone give me a working example? Or perhaps someone understands what this error relates to?
Edit #1
The example above is actually from the Django Advent link. I've tried a variety of things and all of them spout the same error. But a simple non-working example would be:
urls.py
feeds.py
, LatestEntriesByCategory()),
)
But when I try to do anything along those lines, I get an error:
init() takes exactly 3 arguments (1 given)
Can anyone give me a working example? Or perhaps someone understands what this error relates to?
Edit #1
The example above is actually from the Django Advent link. I've tried a variety of things and all of them spout the same error. But a simple non-working example would be:
urls.py
feeds.py
, LatestCommentsFeed()),
)
feeds.py
, LatestEntries()), (r'^feeds/categories/(?P<category_id>\d+)/But when I try to do anything along those lines, I get an error:
init() takes exactly 3 arguments (1 given)
Can anyone give me a working example? Or perhaps someone understands what this error relates to?
Edit #1
The example above is actually from the Django Advent link. I've tried a variety of things and all of them spout the same error. But a simple non-working example would be:
urls.py
feeds.py
, LatestEntriesByCategory()), )But when I try to do anything along those lines, I get an error:
init() takes exactly 3 arguments (1 given)
Can anyone give me a working example? Or perhaps someone understands what this error relates to?
Edit #1
The example above is actually from the Django Advent link. I've tried a variety of things and all of them spout the same error. But a simple non-working example would be:
urls.py
feeds.py
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,罪魁祸首找到了! :)
在我的 feeds.py 中,我有:
我应该有:
django.contrib.syndicate.feeds 模块显然只是为了向后兼容。
Ok, found the culprit! :)
In my feeds.py I had:
And I should have had:
The django.contrib.syndication.feeds module is there only for backwards compatibility apparently.