DEV Community

Cover image for Add Sitemap to Your Django Site
Shoukrey Tom
Shoukrey Tom

Posted on

2 1

Add Sitemap to Your Django Site

you can jump to step 4 if you want to test it with your existing project

Index

1- Intorduction
2- Environment Setup
3- Project Setup
4- Add Sitemap

Introduction

A sitemap tells search engines which pages and files you think are important in your site and also provides valuable information about these files.
it helps search engines to crawl your website quickly.

Environment Setup

  • create your virtual environment or use an existing one: python -m venv env
  • activate your virtual environment source env/bin/activate
  • install Django pip install django

Project Setup

  • create new folder mkdir django-add-sitemap && cd django-add-sitemap
  • create new project django-admin startproject config .
  • create new app python manage.py startapp blog and add it to INSTALLED_APPS
  • edit blog/models.py and paste these code in it:
from django.db import models
from django.urls import reverse

class Post(models.Model):
    STATUS_CHOICES = [('published', 'Published'), ('draft', 'Draft')]
    title = models.CharField(max_length=250)
    content = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=12, choices=STATUS_CHOICES, default='draft')

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'slug': self.slug})

@receiver(post_save, sender=Post)
def save_slug(sender, instance=None, created=False, **kwargs):
    if created:
        instance.slug = slugify(instance.title)
        instance.save()
Enter fullscreen mode Exit fullscreen mode
  • run python manage.py makemigrations then python manage.py migrate and don't forget to create a superuser python manage.py createsuperuser
  • add Post to admin.py and add some data for testing later.

Add Sitemap

  • first add django.contrib.sitemaps to INSTALLED_APPS
  • create blog/sitemaps.py then paste this code to it:
from django.contrib import sitemaps
from .models import Post


class PostSitemap(sitemaps.Sitemap):
    changefreq = "weakly"
    priority = 0.8

    def items(self):
        return Post.objects.filter(status='published')

    def lastmod(self, obj):
        return obj.updated
Enter fullscreen mode Exit fullscreen mode

changefreq this attribute is used to tell how frequently your site is changing. other values are ["always", "hourly", "daily", "monthly", "yearly", "never"].
you can override default location method if have you don't have get_absolute_url in your model.

  • finally, map the URL for the sitemaps in the config/urls.py or blog/urls.py file.
# config/urls.py
from django.contrib.sitemaps.views import sitemap
from blog.sitemaps import PostSitemap

sitemaps = {
    "posts": PostSitemap,
}

urlpatterns = [
    ..........
    path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, name="sitemap"),
    .........
]
Enter fullscreen mode Exit fullscreen mode

here is how it looks like in a browser:
Result

source code is upload to github: https://github.com/abdulshak1999/Python/tree/main/django/website_sitemap

for more information check this: https://docs.djangoproject.com/en/3.0/ref/contrib/sitemaps/

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay