In previous articles, we learned how to create a sitemap for the Django website. A valid sitemap increases your website's search engine ranking. Hence good for search engine optimization.
Similarly, adding a robot.txt file is good for your website. It tells crawlers, which page to crawl and which page not to crawl for indexing.
In this article, we will see how to generate RSS feed on your Django website.
The RSS feed help to keep up readers with their favorite blogs, news sites, and other websites. RSS allows the content and new updates to come to the reader. Generally, you use RSS to syndicate or subscribe to the feed of a website, blog or almost any media content that is updated online
Create a file in your app directory, parallel to urls.py file and name it feeds.py
. Paste the below code in it.
In the below sample example, we are fetching posts/articles from the database of pythoncircle.com. We have implemented four methods, item
, item_title
, item_description
and item_link
. The code has been updated with comments.
class LatestEntriesFeed(Feed):
title = "PythonCircle.com: New article for Python programmers every week"
link = "/feed/"
description = "Updates on changes and additions to python articles on pythoncircle.com."
# return 10 recently created/updated posts
def items(self):
return get_recent_updated_posts(number_of_posts=10)
def item_title(self, item):
return item.title
# return a short description of article
def item_description(self, item):
return item.description
# create and return the article URL
def item_link(self, item):
return reverse('appname:index', args=(item.post_id,))
Now in your project's urls.py
file (not in any app's urls.py file) add below code.
from appname.feeds import LatestEntriesFeed() # add feeds path urlpatterns += [
path(r'feed/', LatestEntriesFeed()),
]
Restart/Reload your Django app and go to pythoncircle.com/feed/ or localhost:8000/feed/.
You can validate if RSS feed generated is valid or not.