Django 联合框架:如何使用新的基于类的提要视图?

发布于 2024-09-08 00:06:20 字数 1485 浏览 13 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2024-09-15 00:06:20

好的,罪魁祸首找到了! :)
在我的 feeds.py 中,我有:

from django.contrib.syndication.feeds import Feed

我应该有:

from django.contrib.syndication.views import Feed

django.contrib.syndicate.feeds 模块显然只是为了向后兼容。

Ok, found the culprit! :)
In my feeds.py I had:

from django.contrib.syndication.feeds import Feed

And I should have had:

from django.contrib.syndication.views import Feed

The django.contrib.syndication.feeds module is there only for backwards compatibility apparently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文