Django RSS | How To Generate Feeds Using Django- W3School

Django offers you a feature of creating an RSS with the help of the syndication feed generating framework. With the help of this framework, you can easily create RSS feed or Atom feeds. For this, you just need to create a subclass of the django.contrib.syndication.views.Feed class.

In here, we will be using the previously created comments done on the web application (you can refer to Django Comments chapter). You need to begin by creating a myapp/ file. After that, define your feed under this file. You can keep your feed class anywhere in the structure as jumbling the code won’t have any effects on the final output.

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’})

The above provided feed class has the following elements which are explained to you over here:

  • title, link, and description attributes – These are the simplest attributes as they directly correspond to the standard RSS <title>, <link>, and <description> elements of the code.
  • items() method – This method returns all the elements which are passed in the feed as an item element. According to the above code, the method will return the last 5 comments.
  • item_title() method – The method will create a title name for your feed item. The above code will set the title for your feed item as the username.
  • item_description() method – The description for your feed’s item will go under this method. In this case, the description for your feed item will be the comment itself.
  • item_link() – This method ensures that a link will be established to the full item. In the above case, it will return the comment.

Once you are done with the feed creation, it’s time that you add a comment view in the file (under the myapp directory of your project). This change in the file will make sure that the comment will get displayed to the user.

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)

Now, you need to map the URL to your myapp/ file. For this, follow the given code –

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’),

Type in the given URL: in the web browser and you will land on the following display screen –

By the time you click on any of the usernames displayed on the screen, you will get redirected to the /myapp/comment/comment_id. The screen will appear as the following –

So, in Django framework, defining an RSS feed class is very easy as you just need to create a subclass of the Feed class. But make sure that all the URLs are defined properly. In short, you need to define two URLs:

  1. for accessing the feed
  2. for accessing the feed elements of your feed

As done in the case of comments, you need to attach this to any of the models in your web application.