我正在努力让RSS与django一起使用
我有一个社交书签应用。
当我尝试访问localhost:8000 / feeds / recent /
的rss页面时我收到以下错误:
Could not import django.contrib.syndication.views.feed. View does not exist in module django.contrib.syndication.views.
我正在使用python 2.7.3和django 1.5.1
我只会展示我认为相关的代码。
我在feeds.py中有以下代码
from django.contrib.syndication.views import Feed
from bookmarks.models import Bookmark
class RecentBookmarks(Feed):
title = 'Django Bookmarks | Recent Bookmarks'
link = '/feeds/recent/'
description = 'Recent bookmarks posted to Django Bookmarks'
def items(self):
return Bookmark.objects.order_by('id')[:10]
urls.py有以下代码我遗漏了不相关的网址。
import os.path
from django.conf.urls.defaults import *
from bookmarks.views import *
from bookmarks.feeds import *
from django.views.generic import TemplateView
from bookmarks.models import Link, Bookmark, Tag, SharedBookmark
site_media = os.path.join(
os.path.dirname(__file__), 'site_media'
)
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
admin.site.register(Link)
class BookmarkAdmin(admin.ModelAdmin):
list_display = ('title', 'link', 'user')
list_filter = ('user',)
ordering = ('title',)
search_fields = ('title',)
admin.site.register(Bookmark, BookmarkAdmin)
admin.site.register(Tag)
admin.site.register(SharedBookmark)
feeds = {
'recent': RecentBookmarks
}
urlpatterns = patterns('',
# Feeds
(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
{'feed_dict': feeds }),
)
models.py如下所示:
from django.db import models
from django.contrib.auth.models import User
class Link(models.Model):
url = models.URLField(unique=True)
def __str__(self):
return self.url
class Bookmark(models.Model):
title = models.CharField(max_length=200)
user = models.ForeignKey(User)
link = models.ForeignKey(Link)
def __str__(self):
return '%s %s' % (self.user.username, self.link.url)
def get_absolute_url(self):
return self.link.url
答案 0 :(得分:0)
我一直在学习Django的书已经很老了。
我从Django文档中发现,所需的url模式现在可以直接进入RecentBookmarks。
我先看了here
并将其与this
进行比较通过这种比较,我发现我需要做的是将网址格式更改为以下内容。
(r'^feeds/(?P<url>.*)/$', RecentBookmarks()),
我还发现,除非我安装了RSS Subscription extension
,否则RSS无法与Google Chrome浏览器一起使用进行这些更改后,它现在可以正常工作。