Django has a built-in syndication feed framework that you can use to dynamically generate RSS or Atom feeds in a similar manner to creating sitemaps using the site's framework. A web feed is a data format (usually XML) that provides users with frequently updated content. Users will be able to subscribe to your feed using a feed aggregator, a software that is used to read feeds and get new content notifications.
Create a new file in your blog application directory and name it feeds.py. Add the following lines to it:
# blog/feeds.py
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 = 'Latest posts of my blog.'
def items(self):
return Post.published.all()[:3]
def item_title(self, item):
return item.title
def item_description(self, item):
return truncatewords(item.body, 30)
First, we subclass the Feed class of the syndication framework. The title, link, and description attributes correspond to the <title>, <link>, and <description> RSS elements, respectively.
The items() method retrieves the objects to be included in the feed. We are retrieving only the last three published posts for this feed. The item_title() and item_description() methods receive each object returned by items() and return the title and description for each item. We use the truncatewords built-in template filter to build the description of the blog post with the first 30 words.
Now, edit the blog/urls.py file, import LatestPostsFeed you just created, and instantiate the feed in a new URL pattern:
# blog/urls.py
from .feeds import LatestPostsFeed
urlpatterns = [
# ...
path('feed/rss/', LatestPostsFeed(), name='post_feed'),
]
Navigate to http://localhost:8000/blog/feed/rss/ in your browser. You should now see the RSS feed, including the last three blog posts:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>My cool blog</title>
<link>http://localhost:8000/blog/</link>
<description>Latest posts of my cool blog.</description>
<atom:link href="http://localhost:8000/blog/feed/rss/" rel="self"></atom:link>
<language>en-us</language>
<lastBuildDate>Mon, 23 Mar 2020 11:39:56 +0000</lastBuildDate>
<item>
<title>python is my favourite programming language</title>
<link>http://localhost:8000/blog/2020/1/11/python-my-favourite-programming-language/</link>
<description>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum …</description>
<guid>http://localhost:8000/blog/2020/1/11/python-my-favourite-programming-language/</guid></item>
<item>
<title>django is the best python web framework</title>
<link>http://localhost:8000/blog/2020/1/11/django-best-python-web-framework/</link>
<description>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum …</description>
<guid>http://localhost:8000/blog/2020/1/11/django-best-python-web-framework/</guid></item>
<item>
<title>the gods of our ancestors</title>
<link>http://localhost:8000/blog/2020/1/6/the-gods-of-our-ancestors/</link>
<description>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took …</description>
<guid>http://localhost:8000/blog/2020/1/6/the-gods-of-our-ancestors/</guid></item>
</channel>
</rss>
If you open the same URL in an RSS client, you will be able to see your feed with a user-friendly interface.
The final step is to add a feed subscription link to the blog's sidebar. Open the blog/base.html template and add the following line under this is my blog inside the sidebar div:
<!-- base.html -->
<p><a href="{% url "blog:post_feed" %}" title="Subscribe to my RSS feed">RSS feed</a></p>
Now, open http://localhost:8000/blog/ in your browser and take a look at the sidebar. The new link should take you to your blog's feed:
that's it for now, stay connected for more useful features.
If you like my content, please consider buying me a coffee.
Thank you for your support!