Django Blog #31: Creating a Blog RSS Feed

by Alex
Django Blog #31: Creating a Blog RSS Feed
Django has a built-in feed syndication framework that can be used to dynamically generate RSS or Atom feeds based on a sitemap. Web feeds are a data format (usually XML) that offers users frequently updated content. They can subscribe to a feed using an aggregator, the software used to read feeds and receive notification of new content. Create a new file in the blog application folder and name it Add the following lines:

from django.contrib.syndication.views import Feed  
from django.template.defaultfilters import truncatewords  
from .models import Post  
class LatestPostsFeed(Feed) 
    title = 'My blog' 
  link = '/blog/' 
  description = 'New posts of my blog.' 
 def items(self) 
       return Post.published.all()[:5] 
   def item_title(self, item) 
       return item.title 
   def item_description(self, item) 
       return truncatewords(item.body, 30)

First, you need a subclass of the Feed class of the syndication framework. The title, link and description attributes are associated with the corresponding RSS elements: <title>, <link>, <description>. Method items() gets the objects which should be included in the feed. There will be 5 of them. The item_title() and item_description() methods get each object which returns items() and return title and description respectively. The built-in truncatewords filter is used to create a description from the first 30 words. Now edit the blog/ file, import the created LatestPostsFeed and create a feed instance in the URL template:

from .feeds import LatestPostsFeed 

urlpatterns = [ 
   # .. 
    path('feed/', LatestPostsFeed(), name='post_feed'), 

Open in your browser. An RSS feed of the last five posts should appear.

<?xml version="1.0" encoding="utf-8"?
<rss version="2.0"
      <title>My blog</title>
      <description>New posts of my blog.</description>
      <atom:link href="https://localhost:8000/blog/feed/" rel="self"></atom:link>
      <lastBuildDate>Sun, 02 Feb 2020 14:59:07 +0000</lastBuildDate>
        <title>Markdown post</title>
        <description>This is a post formatted with markdown 
                     *This is emphasized* and **this is more emphasized** 
                     Here is a list: * One * Two * Three And a [link to ...
        <title>The AI Community Needs to Take Responsibility for Its Technology and Its Actions</title>
        <description>At the opening keynote of a prominent AI research conference,
                     Celeste Kidd, a cognitive psychologist, challenged the audience 
                     to think critically about the future they want to build.

If you open the same link in an RSS client, the feed will appear in human-readable form with an interface. The final step is to add a link to subscribe to the feed in the sidebar of the blog. Open the blog/base.html template and add the following line below the total number of posts in the sidebar div block:

<p><a href="{% url "blog:post_feed" %}">Subscribe to my RSS feed</a></p>

Now open in your browser and look at the sidebar. The link should lead to the blog feed:Django Blog #31: Creating a Blog RSS Feed

Related Posts