Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Django
  • Views
  • Model
  • Template
  • Forms
  • Jinja
  • Python SQLite
  • Flask
  • Json
  • Postman
  • Interview Ques
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJS
  • Vue.js
Open In App
Next Article:
How To Add Unit Testing to Django Project
Next article icon

How to add RSS Feed and Sitemap to Django project?

Last Updated : 10 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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
                      
                       

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

# 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"),
.....
]
                      
                       

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

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',
 
]
                      
                       

Create Sitemap – 

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

Python3

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

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

# 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)})
                      
                       

Routing for sitemap –

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

Python3

# 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"),
.....
]
                      
                       

Now you can see RSS feed and Sitemaps at assigned urls

Sample Sitemap

Sample Sitemap



Next Article
How To Add Unit Testing to Django Project
author
kushwanthreddy
Improve
Article Tags :
  • Python
  • Technical Scripter
  • Django-Projects
  • Python Django
  • Technical Scripter 2020
Practice Tags :
  • python

Similar Reads

  • How to add AMP to Django Project?
    A blog mostly needs content but that doesn't mean, your blog will be on top of Google search. For this you will need Speed, Security, user base and first of all search engines need to know that your blog exists. We will add AMP for speed.  This article is in continuation of Blog CMS Project in Djang
    4 min read
  • How To Add Unit Testing to Django Project
    Unit testing is the practice of testing individual components or units of code in isolation to ensure they function correctly. In the context of Django, units typically refer to functions, methods, or classes within your project. Unit tests are crucial for detecting and preventing bugs early in deve
    4 min read
  • How to add Site Header, Site Title, Index Title in a Django Project?
    Automatic admin interface one of the most powerful parts of Django. Metadata is read from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization’s internal management tool. It’s not intend
    1 min read
  • How to Import a JSON File to a Django Model?
    In many web development projects, it's common to encounter the need to import data from external sources into a Django application's database. This could be data obtained from APIs, CSV files, or in this case, JSON files. JSON (JavaScript Object Notation) is a popular format for structuring data, an
    4 min read
  • How to create a Django project?
    Dive into the world of web development with Python by exploring the versatile Django framework. Django is a go-to for many developers due to its popularity, open-source license, and robust security features. It enables fast and efficient project development. In this tutorial, we will guide you throu
    5 min read
  • How to Add URL Parameters to Django Template URL Tag?
    When building a Django application, it's common to construct URLs dynamically, especially when dealing with views that require specific parameters. The Django template language provides the {% url %} template tag to create URLs by reversing the URL patterns defined in the urls.py file. This tag beco
    4 min read
  • How to add Google reCAPTCHA to Django forms ?
    This tutorial explains to integrate Google's reCaptcha system to your Django site. To create a form in Django you can check out – How to create a form using Django Forms ?  Getting Started Adding reCaptcha to any HTML form involves the following steps: Register your site domain to reCaptcha Admin Co
    4 min read
  • How to Push your Django Project to GitHub
    In this article, we have explained how to push a Django project on GitHub using Git and direct upload. Git is a version control system that allows us to track our changes and GitHub is a cloud-based platform where we can store, share, and manage your code or projects. With Git and GitHub we can coll
    6 min read
  • How to Add Data from Queryset into Templates in Django
    In this article, we will read about how to add data from Queryset into Templates in Django Python. Data presentation logic is separated in Django MVT(Model View Templates) architecture. Django makes it easy to build web applications with dynamic content. One of the powerful features of Django is fet
    3 min read
  • Adding WYSIWYG editor to Django Project
    Often, to manage content efficiently we use WYSIWYG (What You See Is What You Get) editor which stores our content in html and is also helpful to upload images, creating links, lists and works almost like Wordpress editor. This article is in continuation of Blog CMS Project in Django. Check this out
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences