Create Static Sitemaps In Django
A sitemap is a xml file which tells search engines about the pages available in your website which can be crawled. Django has inbuilt functionality for generating sitemap.xml file. It automates the creation of sitemaps based on your models.
To use django sitemaps first of all you have to add it as installed app in django project settings. This will look like as follows:
Now we will create a file sitemaps.py, in which we will write sitemaps views. Sitemaps classes are must be inherited from django.contrib.sitemaps it can be placed anywhere in your codebase. To create a sitemaps.py and add the following code to it.
After creating sitemap view in sitemaps.py file now we have to add sitemap views to urls.py, you can add in app level urls.py or in project level urls.py here i am adding it in app level urls.py.
After that run django using python manage.py runserver and navigate to url localhost:8000/sitemap.xml, it will automatically generate sitemap for our privacy static page as you can see in below gist code.