How To Create Django Dynamic And Static Sitemap For Your Website

How to create a dynamic sitemaps in django

have you asked yourself how To Create Django Dynamic And Static Sitemap For Your Website? Have you tried to index your site on search engine but you dont know how to do with it? Creating a website from scratch is not easy so am going to work you through on creating your own site maps both dynamic and static pages.

What is sitemap
A site map is a list of pages of a web site within a domain. There are three primary kinds of site map: Site maps used during the planning of a Web site by its designers. Human-visible listings, typically hierarchical, of the pages on a site. Structured listings intended for web crawlers such as search engines. you can check ongooglefor more info.


In this Tutorial I will be walking you through how to create a django sitemap which will include your login page, Sign up page, privacy, terms etc;
Steps involve in creating a django sitemap

  • Open your settings.py
  • go to installed app

Add ‘django.contrib.sitemaps’ to your INSTALLED_APPS setting.
Make sure your TEMPLATES setting contains a DjangoTemplates
backend whose APP_DIRS options is set to True. It’s in there by default,
so you’ll only need to change this if you’ve changed that setting.
Make sure you’ve installed the sites framework.

specify a siteid e.g SITE_ID=1 in settings.py.

open your website then go to admin panel and locate sites then instead of example.com type in your domain name without www or https/htpp, save it.

sitemap
Now open the folder containing your web app then create a file named sitemaps.py

<strong>from</strong> django.contrib.sitemaps <strong>import</strong> Sitemap
<strong>from</strong> django.urls <strong>import</strong> reverse

<strong>class</strong> <strong>BlogSitemap</strong>(<strong>Sitemap</strong>):
    changefreq = 'weekly'
    priority=0.9
    <strong>def</strong> <strong>items</strong>(self):
        <strong>return</strong> Blog.objects.filter()
    <strong>def</strong> <strong>lastmod</strong>(self,obj):
        <strong>return</strong> obj.date_added

Make sure you did all the import as above, the reverse import will be for our static pages.

Now go to urls.py in the folder that contains settings.py then edit it as below.

<strong>from</strong> django.contrib.sitemaps.views <strong>import</strong> sitemap
<strong>from</strong> posts.sitemaps <strong>import</strong> BlogSitemap
sitemaps = {'posts':BlogSitemap,
}
urlpatterns = [
path('sitemap.xml',sitemap,{'sitemaps':sitemaps},name='django.contrib.site.views.sitemaps'),

Now you have to know that BlogSitemap, ‘Blog’ is the name of the model we created in models.py

boom! we have to domigrations to ensure that we apply the installed app migrations.

./manage.py makemigrations
./manage.py migrate

now check your site map by going to the homepage and add sitemap.xml

e.g tutorial/sitemap.xml ; tutorial is the domain name then sitemap.xml.

Next i will show you how to configure static pages and also add other models that you have created, Thanks for reading.


But this is not the end , if you want to have an advance sitemaps in such a way that you will have separate sitemaps for each models and static sitemaps then you can check this article how to create multiple and separate sitemaps for your django website or blog

You may like How to develop django website using termux and andriod

Leave a Reply

Your email address will not be published. Required fields are marked *