Related Articles

Related Articles

How to add RSS Feed and Sitemap to Django project?
  • Last Updated : 01 Nov, 2020

This article is in continuation of Blog CMS Project in Django. Check this out here – Building Blog CMS (Content Management System) with Django

RSS (Really Simple Syndication) Feed

RSS (Really Simple Syndication) is a web feed that allows users and applications to access updates to websites in a standardized, computer-readable format. These feeds can, for example, allow a user to keep track of many different websites in a single news aggregator. Django comes with an library to create atom feed for our blog. 

Creating views for RSS Feed – 

Go to blog app directory and create a file feeds.py and paste the below code.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.contrib.syndication.views import Feed
from django.template.defaultfilters import truncatewords
from .models import posts
from django.urls import reverse
from django.utils.feedgenerator import Atom1Feed
  
class blogFeed(Feed):
    title = "geeksforgeeks"
    link = "/posts/"
    description = "RSS feed of GeeksForGeeks"
  
    def items(self):
        return posts.objects.filter(status = 1)
  
    def item_title(self, item):
        return item.title
        
    def item_description(self, item):
        return item.metades
  
    def item_link(self, item):
       return reverse('post_detail', args =[item.slug])
  
class atomFeed(Feed):
    feed_type = Atom1Feed

chevron_right


Create Routes for RSS Feed – 

To route the RSS feed go to urls.py file of the app you are using for generating feed and add the route



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing django routing libraries
from . import views
from django.urls import path, include
from .views import * from .feeds import blogFeed
  
urlpatterns = [
.....
    # RSS route  
    path("posts / feed", blogFeed(), name ="feed"),
.....
]

chevron_right


Sample Feed

Sample Feed

Sitemap – 

Sitemap protocol allows a webmaster to inform search engines about URLs on a website that are available for crawling. A Sitemap is an XML file that lists the URLs for a site. It allows webmasters to include additional information about each URL: when it was last updated, how often it changes. This allows search engines to crawl the site more efficiently and to find URLs that may be isolated from the rest of the site’s content.

Add sitemaps to INSTALLED_APPS –

Django also comes with a sitemaps creator go to the blog app directory and add sitemaps to installed apps in settings file

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
    # adding in installed apps
    'django.contrib.sitemaps',
  
]

chevron_right


Create Sitemap – 

Create a file sitemaps.py and paste the below code.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.contrib.sitemaps import Sitemap
from .models import posts
  
# siemap class
class blogSitemap(Sitemap):
# change fequency and priority
    changefreq = "daily"
    priority = 1.0
  
    def items(self):
        return posts.objects.filter(status = 1)
  
    def lastmod(self, obj):
        return obj.updated_on

chevron_right


Add absolute URL to model – 

Sitemap generated should have urls for our posts so we need to add a simple function to our model so that we sitemap library can generate posts urls

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# add it in your model for which you want to generate sitemap
def get_absolute_url(self):
        from django.urls import reverse
        return reverse("post_detail", kwargs ={"slug": str(self.slug)})

chevron_right


Routing for sitemap –

Now to generate the sitemap url, Go to urls.py file of the and add the route

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# adding sitemap libraries
from django.contrib.sitemaps.views import sitemap
from blog.sitemaps import blogSitemap
  
blogsitemap = {
"blog": blogSitemap, }
  
urlpatterns = [
.....
    # urls handling site maps
    path("sitemap.xml", sitemap, {"sitemaps": blogsitemap}, name ="sitemap"),
.....
]

chevron_right


Now you can see RSS feed and Sitemaps at assigned urls

Sample Sitemap

Sample Sitemap


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :