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 feeds.py. 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/urls.py 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 https://127.0.0.1:8000/blog/feed/ in your browser. An RSS feed of the last five posts should appear.

<?xml version="1.0" encoding="utf-8"?
<rss version="2.0"
    xmlns:atom="https://www.w3.org/2005/Atom">
    <channel>
      <title>My blog</title>
      <link>https://localhost:8000/blog/</link>
      <description>New posts of my blog.</description>
      <atom:link href="https://localhost:8000/blog/feed/" rel="self"></atom:link>
      <language>en-us</language>
      <lastBuildDate>Sun, 02 Feb 2020 14:59:07 +0000</lastBuildDate>
      <item>
        <title>Markdown post</title>
        <link>https://localhost:8000/blog/2020/2/2/markdown-post/</link>
        <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 ...
        </description>
        <guid>https://localhost:8000/blog/2020/2/2/markdown-post/</guid>
      </item>
      <item>
        <title>The AI Community Needs to Take Responsibility for Its Technology and Its Actions</title>
        <link>https://localhost:8000/blog/2019/12/14/ai-community-needs-take-responsibility-its-technology-and-its-actions/</link>
        <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.
        </description>
        <guid>https://localhost:8000/blog/2019/12/14/ai-community-needs-take-responsibility-its-technology-and-its-actions/</guid>
      </item>
    </channel>
</rss>

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 https://127.0.0.1:8000/blog/ 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

LEAVE A COMMENT