I'm trying to get RSS to work with django
I have a social bookmarking app.
when I try to access the rss page at localhost:8000/feeds/recent/
I get the following error:
Could not import django.contrib.syndication.views.feed. View does not exist in module django.contrib.syndication.views.
I am using python 2.7.3 and django 1.5.1
I am only going to show the code that I think is relevant.
I have the following code in 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]
The urls.py has the following code I have left out the urls that are not relevant.
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 }),
)
The models.py looks like the following:
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
The book I have been learning Django from is quite old.
I discovered from looking at the Django Documentation that the url pattern that's required can now go straight to RecentBookmarks.
I first looked here
and compared it with this
From this comparison I discovered that what I need to do is change the url pattern to the following.
(r'^feeds/(?P<url>.*)/$', RecentBookmarks()),
I also found that in RSS does not work with google chrome browser unless I installed the RSS Subscription extension
After making these changes it now works correctly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.