Search Here

Create Dynamic Sitemap in Django Using Multiple Models Slug

 In this article, you will learn how to create a dynamic sitemap in Django using slug of multiple models. For example, if we want to show URLs in a sitemap like this http://example.com/slug-field-from-model-1/slug-field-from-model2 then just sit back and follow this great tutorial.

Create Dynamic Sitemap in Django Using Multiple Models Slug

What is a sitemap?

A sitemap is the blueprint of your website. It is an XML file that has all of your web page links its priority and the last modified date. This information will help the search engine crawler to crawl all of your web pages in a go. In this article, I am going to show how to add a dynamic sitemap in the Django app using the below Django sitemap frameworks.

Enjoy Free SEO Tools: Free SEO Optimized Xml Sitemap Generator Online

Use Digital Marketing Tools Free

  1. django.contrib.sites
  2. django.contrib.sitemaps

Create Sitemap Automatically in Django

Below is the step-by-step guide to creating automatic sitemaps in the Django application.

Enjoy Free SEO Tools: Analyze And Test Robots Txt Files On Large Scale

First of all, add the above apps provided by Django in INSTALLED_APPS located in the settings.py file and add SITE_ID = 1 below the INSTALLED_APPS list. 

INSTALLED_APPS = [

    # your other apps goes here

    'django.contrib.sites',

    'django.contrib.sitemaps',

]

SITE_ID = 1

Make sure that in your settings.py file under TEMPLATES you have DjangoTemplates listed (generally you don't need to do this because it should be here by default), and 'APP_DIRS': True set to true.

TEMPLATES = [

    {

        'BACKEND': 'django.template.backends.django.DjangoTemplates',

        'APP_DIRS': True,

    },

]


Run migrations to load your changes.

  • py manage.py makemigrations
  • py manage.py migrate

Logged in as administrator http://127.0.0.1/admin and go to Sites and change Domain Name and Display Name as 127.0.0.1:8000 if you are in development mode, if you are working on production environment branch then change the above as per your domain name.

Enjoy Free SEO Tools: Meta Description Tag Length Checker For All Pages

Create a new python file sitemap.py where your models.py and views.py files are located and follow below code snippet.


In the above code we have imported the Sitemap class from django.contrib.sitemaps and import our models. Next, we have created a MySitemap class that extends the Sitemap class. Below are the description of MySitemap class data members and member functions.

Name Description
changefreq Shows how frequently the content of the page changes, its value should be one of them (always, hourly, daily, weekly, monthly, yearly, never)
priority Priority shows how much the web pages are important than other pages. You can choose the value between 0.0 upto 1.0
protocol Protocol eighter http or https, in development mode it would be http and in production it would be https
item Its a class member function and return all of the objects from model which we want to show as a sitemap in our django app
lastmod Returns the date when the last time your model instance or object was modified. It should be a date field created in your model.
location This will return the PATH or URL of your web pages, in simple words it can be a slug field. By default django framework calls the get_absolute_url() function to return the path.

Add your sitemap in urls.py


Now it's time to add our sitemap class in urls.py file. Follow the below code snippet to add this.


from django.contrib.sitemaps.views import sitemap
from some_dir.sitemap import Your_Sitemap_Class

sitemaps = {
    'some_key':Your_Sitemap_Class
}

urlpatterns = [
    path('admin/', admin.site.urls),
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps},name='django.contrib.sitemaps.views.sitemap'),
]

You have created successfully dynamic sitemap in your django app.

Post a Comment

0 Comments