DEV Community

Cover image for Generate Your Dynamic Django Sitemap
Ordinary Coders
Ordinary Coders

Posted on • Originally published at ordinarycoders.com

Generate Your Dynamic Django Sitemap

1) Install Django sites app to the settings.py

INSTALLED_APPS = [
    'main.apps.MainConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',    #add sites to installed apps
]
SITE_ID = 1   #define the site id
Enter fullscreen mode Exit fullscreen mode

2) Make migrations

(env)C:\Users\Owner\desktop\env\mysite> py manage.py migrate
Enter fullscreen mode Exit fullscreen mode

3) Create a superuser

(env) C:\Users\Owner\Desktop\Code\env\mysite>py manage.py createsuperuser
Username (leave blank to use 'owner'): admin
Email address: 
Password: *****
Password (again): *****
Superuser created successfully.
(env) C:\Users\Owner\Desktop\Code\env\mysite>py manage.py runserver
Enter fullscreen mode Exit fullscreen mode

4) Log in to the Django Admin console, click Sites. Change the example.com domain to your localhost 127.0.0.1:8000. For production, change the names to your actual domain name.

5) Install Django sitemap app in settings.py

INSTALLED_APPS = [
    'main.apps.MainConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sitemaps',   #add Django sitemaps to installed apps
]
Enter fullscreen mode Exit fullscreen mode

6) Create a Django sitemap file main > (New File) sitemaps.py

from django.contrib.sitemaps import Sitemap
from .models import Article


class ArticleSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.8
    protocol = 'http'
    def items(self):
        return Article.objects.all()
    def lastmod(self, obj):
        return obj.article_published

    def location(self,obj):
        return '/blog/%s' % (obj.article_slug)
Enter fullscreen mode Exit fullscreen mode

7) Add the Django sitemap URL to the main > urls.py

from django.urls import path
from . import views
from django.contrib.sitemaps.views import sitemap
from .sitemaps import ArticleSitemap
app_name = "main"
sitemaps = {
    'blog':ArticleSitemap
}
urlpatterns = [
    path("", views.homepage, name="homepage"),
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
]
Enter fullscreen mode Exit fullscreen mode

8) Add static pages to the dynamic Django sitemap

from django.urls import reverse
class StaticSitemap(Sitemap):
    changefreq = "yearly"
    priority = 0.8
    protocol = 'https'
    def items(self):
        return ['main:homepage_view', 'main:contact_view']
    def location(self, item)
            return reverse(item)
Enter fullscreen mode Exit fullscreen mode

9) Update the main > urls.py with the static sitemap

from django.urls import path
from . import views
from django.contrib.sitemaps.views import sitemap
from .sitemaps import ArticleSitemap, StaticSitemap #import StaticSitemap
app_name = "main"
sitemaps = {
    'blog':ArticleSitemap,
    'static':StaticSitemap #add StaticSitemap to the dictionary
}
Enter fullscreen mode Exit fullscreen mode

10) View the Django sitemap at http://127.0.0.1:8000/sitemap.xml/

In-depth tutorial: How to Create a Dynamic Django Sitemap

Top comments (0)