A sitemap is an XML file that is primarily intended for search engines. A sitemap informs search engines about the pages on a website that are available for crawling. It does not only provide information on the sites' URL list and other metadata, but it also tells when these pages were last updated, how frequently they change, and how important they are in relation to other pages on the site.
Generally, Django has a built-in sitemap framework to create sitemap XML files. This sitemap is located in sitemap.xml. For example anythingpython.com/sitemap.xml
Let's see how we can create a sitemap in Django.
Installation:
To add the sitemap functionality to your Django application, we have to install it in our setting.py file. we have to add 'django.contrib.sitemaps'
to our INSTALLED_APPS
setting.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites', # New
'django.contrib.sitemaps', # New
]
SITE_ID = 1 #New
Make sure the site framework is also installed. The site framework is used to define the name of the site in your sitemap framework. The SITE_ID is an integer that specifies the database ID of the site associated with the setting.py file.
We have to update our database by running the following migration
(env) C:\Users\uobod\Desktop\django\djangoblog>python manage.py migrate
The migration command adds the site table to the database.
Create superuser:
We have to create a superuser to be able to access the admin section of our site and control the sitemap from the admin panel. The following command creates a superuser.
(env) C:\Users\uobod\Desktop\django\djangoblog>python manage.py createsuperuser
Create a sitemap.py file:
from django.contrib.sitemaps import Sitemap
from blog.models import Post
from django.urls import reverse
from django.contrib import sitemaps
class PostSiteMap(Sitemap):
changefreq = "weekly"
priority = 0.8
def items(self):
return Post.objects.filter(roll_out=True)
def lastmod(self, obj):
return obj.updated
class StaticViewSitemap(sitemaps.Sitemap):
priority = 0.5
changefreq = 'daily'
def items(self):
return ['blog:aboutme', 'blog:contact']
def location(self, item):
return reverse(item)
Creating a sitemap is similar to creating a Django model. The sitemap class represents a section of entries in your sitemap. The example above represents all the posts in your blog.
After creating the sitemap.py file, we made certain imports.
The "Sitemap" module is imported from django.contrib.sitemaps
Then the "sitemaps" is imported from "django.contrib". The "Post" model and "reverse" are imported from "blog.models" and "django.urls". respectively.
The PostSiteMap class extends Sitemap.
The changefreq is how often your content on your page changes. Possible values for changefreq includes the following;
- "always"
- "hourly"
- "daily"
- "weekly"
- "monthly"
- "yearly"
- "never"
The priority represents the importance of one page compared to another on our website. The values are either string or floats. The default priority of a page is 0.5
A function is declared in the class block "PostSiteMap" named items. This method is required and it usually returns QuerySet
of objects.
The lastmod represents the last-modified date/time for every object returned by items().
The other class "StaticViewSitemap" handles our Static pages. The items() method returned the static pages. The location() method returns the URL to the complete static Django sitemap function.
Add the sitemap Url:
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.sitemaps.views import sitemap #sitemap
from blog.sitemap import PostSiteMap, StaticViewSitemap #sitemap
sitemaps = {
"posts": PostSiteMap,
'static': StaticViewSitemap,
}
urlpatterns = [
path('admin/', admin.site.urls),
path('tinymce/', include('tinymce.urls')),
path('', include('blog.urls', namespace ='blog')),
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap')
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Finally, we have to add the Django sitemap to our URL. Then import sitemap, PostSiteMap, and StaticViewSitemap. A dictionary(sitemaps) is created that maps to the sitemap classes PostSiteMap and StaticViewSitemap.
Now, to access our sitemap, we have to enter the URL into our browser:http://127.0.0.1:8000/sitemap.xml/. It will look similar to the picture below:
