How to create a sitemap for a website based on Django ?

Published: April 15, 2019

DMCA.com Protection Status

An example of how to create a sitemap for a website based on Django

Let's assume a Django website with an application called "notebooks" with the following files:

database.db
manage.py
myproject / settings.py
            urls.py
notebooks / views.py
            models.py
            urls.py

with models.py

class Article(models.Model): 
    full_path = models.TextField()
    date_created = models.DateTimeField(default=datetime.now) 
    date_modified = models.DateTimeField(default=datetime.now) 
    public = models.BooleanField(default=True)
    content = models.TextField()
    content_formatted = models.TextField()

where full_path is the full article url path (for example: /Articles/My-First-Article/)

and the (notebooks / urls.py ) file:

from django.conf.urls import url

from . import views

urlpatterns = [

    url(r'^About/$', views.about_view, name='about_view'),
    url(r'^Contact/$', views.contact_view, name='contact_view'),
    url(r'^Articles/$', views.home_view, name='home_view'),

]

Edit the settings.py file

First step: add the line "django.contrib.sitemaps" into INSTALLED_APPS, in the settings.py file:

INSTALLED_APPS = [
    'notebooks.apps.NotebooksConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sitemaps',
]

Create a file named sitemaps.py

Create an empty file sitemaps.py:

notebooks / views.py
           models.py
           urls.py
           sitemaps.py

Add website statics pages

Edit the sitemaps.py file to include the statics pages (/About/, /Contact/) in the sitemap:

from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse

class Static_Sitemap(Sitemap):

    priority = 1.0
    changefreq = 'yearly'

    def items(self):
        return ['about_view', 'contact_view']

    def location(self, item):
        return reverse(item)

Add website dynamic pages

Edit the sitemaps.py file to include the dynamic pages (like articles) in the sitemap:

from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse

from .models import Article

class Static_Sitemap(Sitemap):

    priority = 1.0
    changefreq = 'daily'


    def items(self):
        return ['about_view', 'contact_view']

    def location(self, item):
        return reverse(item)


class Article_Sitemap(Sitemap):
    changefreq = "daily"
    priority = 0.7

    def items(self):
        return Article.objects.all()

    def location(self, obj):
        return obj.note_full_path

    def lastmod(self, obj): 
        return obj.date_modified

Edit the urls.py file

Last step: edit the myproject / urls.py file:

from django.conf.urls import include, url
from django.contrib import admin

from django.contrib.sitemaps.views import sitemap

from notebooks.sitemaps import Static_Sitemap
from notebooks.sitemaps import Article_Sitemap

sitemaps = {
    'article': Article_Sitemap(),
    'static': Static_Sitemap(),
}

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('notebooks.urls')),
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
]

Check the file sitemaps.xml

And we are done ! We can now go to the url http://127.0.0.1:8000/sitemap.xml and check the result (which should look similar to the image below)

Comment créer un sitemap avec le web-framework Django ?
Comment créer un sitemap avec le web-framework Django ?

References

Image

of