如何解决Django管理中“无法导入django.contrib.syndication.views.feed”错误?

5 投票
2 回答
6024 浏览
提问于 2025-04-16 21:12

我把我的Django版本更新到了最新的夜间版本,现在在管理后台遇到了以下错误:

Could not import django.contrib.syndication.views.feed.  
View does not exist in module django.contrib.syndication.views.

我在几个视图中也遇到了这个错误,因为实际上,django.contrib.syndication.views.feed已经被弃用了,并且被移除了。
我只需要添加一个

from django.contrib.syndication.views import Feed

带有

from django.contrib.syndication.feeds import Feed

问题是我在任何地方都找不到关于django.contrib.syndication.views.feed的引用,甚至在Django的源代码中也没有,所以我不明白这个错误是从哪里来的,也不知道该怎么解决。

错误的直接来源是

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in get_callable, line 100

但我在那儿也找不到任何信息。

希望有人能帮忙!

2 个回答

7

可能你的代码有些问题,我做了一些测试,发现这个高级的生成信息的框架运行得很好。你只需要使用Feed类就可以了。

django.contrib.syndication.views.Feed

这是一个简单的例子:在你的模型里


# -*- coding: utf8 -*-
from django.utils.translation import ugettext as _
from django.contrib.syndication.views import Feed
from django.db import models

class Concept(models.Model):
    concept = models.IntegerField(unique=True, primary_key=True, verbose_name=_('Concepto'))
    description = models.CharField(max_length=255, verbose_name=_('Descripcion'))

    def __unicode__(self):
        return "%s" % ( self.description or self.concept )

    class Meta:
        verbose_name = _('Concepto')
        verbose_name_plural = _('Conceptos')
        ordering = ['concept']

class LatestEntriesFeed(Feed):
    title = "My site news"
    link = "/sitenews/"
    description = "Updates on changes and additions."

    def items(self):
        return Concept.objects.all()

    def item_code(self, item):
        return item.code

    def item_description(self, item):
        return item.description

然后在你的网址里:


from models import LatestEntriesFeed

urlpatterns = patterns('',
    (r'^latest/feed/$', LatestEntriesFeed()),    
)

结果:

我的网站新闻http://example.com/sitenews/关于更改和新增的更新.es-es2011年7月12日 星期二 08:18:49 -0000

希望这能帮到你。

11

user643511 提到,错误可能出在我自己的代码上,而不是 Django。不过她没有指出具体的问题(我理解,因为我没有提供足够的信息)。经过几天的深入研究,我发现我在

url(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds}),

urls.py 文件中写错了。实际上,我应该使用

url(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.Feed', {'feed_dict': feeds}),

注意 views.Feed 中的 F 是大写的。

所以如果有其他人遇到类似的问题,可以检查一下 urls.py 文件。

撰写回答