Django 简明教程

Django - RSS

Django 带有一个联合提要生成框架。利用此框架,您可以通过对 django.contrib.syndication.views.Feed class 进行子类化来创建 RSS 或 Atom 提要。

Django comes with a syndication feed generating framework. With it you can create RSS or Atom feeds just by subclassing django.contrib.syndication.views.Feed class.

让我们创建一个提要,以了解应用程序上进行的最新评论(另请参阅 Django - 注释框架章节)。为此,我们创建一个 myapp/feeds.py 并定义我们的提要(您可以在代码结构中的任何位置放置您的提要类)。

Let’s create a feed for the latest comments done on the app (Also see Django - Comments Framework chapter). For this, let’s create a myapp/feeds.py and define our feed (You can put your feeds classes anywhere you want in your code structure).

from django.contrib.syndication.views import Feed
from django.contrib.comments import Comment
from django.core.urlresolvers import reverse

class DreamrealCommentsFeed(Feed):
   title = "Dreamreal's comments"
   link = "/drcomments/"
   description = "Updates on new comments on Dreamreal entry."

   def items(self):
      return Comment.objects.all().order_by("-submit_date")[:5]

   def item_title(self, item):
      return item.user_name

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

   def item_link(self, item):
      return reverse('comment', kwargs = {'object_pk':item.pk})
  1. In our feed class, title, link, and description attributes correspond to the standard RSS <title>, <link> and <description> elements.

  2. The items method, return the elements that should go in the feed as item element. In our case the last five comments.

  3. The item_title method, will get what will go as title for our feed item. In our case the title, will be the user name.

  4. The item_description method, will get what will go as description for our feed item. In our case the comment itself.

  5. The item_link method will build the link to the full item. In our case it will get you to the comment.

既然我们有了提要,让我们在 views.py 中添加一个评论视图以显示我们的评论 -

Now that we have our feed, let’s add a comment view in views.py to display our comment −

from django.contrib.comments import Comment

def comment(request, object_pk):
   mycomment = Comment.objects.get(object_pk = object_pk)
   text = '<strong>User :</strong> %s <p>'%mycomment.user_name</p>
   text += '<strong>Comment :</strong> %s <p>'%mycomment.comment</p>
   return HttpResponse(text)

我们还需要在 myapp urls.py 中添加一些 URL 以进行映射 -

We also need some URLs in our myapp urls.py for mapping −

from myapp.feeds import DreamrealCommentsFeed
from django.conf.urls import patterns, url

urlpatterns += patterns('',
   url(r'^latest/comments/', DreamrealCommentsFeed()),
   url(r'^comment/(?P\w+)/', 'comment', name = 'comment'),
)

访问 /myapp/latest/comments/,您将获得我们的提要 -

When accessing /myapp/latest/comments/ you will get our feed −

django rss example

然后,单击其中一个用户名将带您访问:/myapp/comment/comment_id (如我们在之前的注释视图中定义的那样),您将获得 -

Then clicking on one of the usernames will get you to: /myapp/comment/comment_id as defined in our comment view before and you will get −

django rss redirected page

因此,定义 RSS 提要只是子类化 Feed 类并确保定义 URL(一个用于访问提要,一个用于访问提要元素)的问题。就像注释一样,这可以附加到应用程序中的任何模型。

Thus, defining a RSS feed is just a matter of sub-classing the Feed class and making sure the URLs (one for accessing the feed and one for accessing the feed elements) are defined. Just as comment, this can be attached to any model in your app.