Generate Your Dynamic Django Sitemap
Tags: django, tutorial
Published: 2021-03-11 20:32:03

1) Install Django sites app to the settings.py
```python
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
```

2) Make migrations
```
(env)C:\Users\Owner\desktop\env\mysite> py manage.py migrate
```

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
```

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
```python
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
]
```

6) Create a Django sitemap file main > (New File) sitemaps.py
```python
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)
```

7) Add the Django sitemap URL to the main > urls.py
```python
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'),
]
```

8) Add static pages to the dynamic Django sitemap
```python
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)
```

9) Update the main > urls.py with the static sitemap
```python
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
}
```

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

In-depth tutorial: [How to Create a Dynamic Django Sitemap](https://www.ordinarycoders.com/blog/article/django-sitemap)