3 horizontal lines, burger
3 horizontal lines, burger
3 horizontal lines, burger
3 horizontal lines, burger

3 horizontal lines, burger
Remove all
LOADING ...

Content



    How to add sitemap into website via django sitemap generator

    Clock
    17.04.2025
    /
    Clock
    01.10.2025
    /
    Clock
    3 minutes
    An eye
    851
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0

    Introduction

    A sitemap is an important part of any website. Even though today there are instant indexing technologies such as IndexNow, a sitemap helps search engines learn about the structure of your website and the pages that would be nice to index.
    You have two options for how to add a map to your website. The first is to write it manually (I don't recommend it). The second is to generate it using Django's built-in utilities. The second method, unlike the first, assumes that once you write it, you will never remember it again. Here.

    Develop a sitemap

    The first thing to do is to create a sitemap.py file in any suitable place. In my case, I usually create it in the Backend application. I think it makes sense. From now on, we will work only in the sitemap.py file. As usual, I will dump all the code at once; the explanation will be after.
    from django.urls import reverse from django.core import paginator from django.contrib.sitemaps import Sitemap from .models import Article, LanguageType from Frontend.views import HomeView class ArticleSitemap(Sitemap): def items(self): articles = Article.objects.filter(is_published=True).order_by('-time_published') return articles def lastmod(self, obj): return obj.time_updated def location(self, obj): return f"/{obj.lang_type}/{obj.slug}" class ArticlePaginationSitemap(Sitemap): def items(self): items = [] types = [] for member in dir(LanguageType): if member.startswith('LANG_TYPE_'): types.append(member) for type in types: articles = Article.objects.filter(is_published=True).filter(lang_type=LanguageType[type]).order_by('-time_published') pagi = paginator.Paginator(articles, per_page=HomeView.page_size) for page in pagi.page_range: items.append({ "page": page, "lang": LanguageType[type] }) return items def location(self, item): if item["page"] == 1: return f"/{item["lang"]}/" else: return f"/{item["lang"]}/?page={item["page"]}" class StaticSitemap(Sitemap): i18n = True def items(self): return ["o-proekte"] def location(self, item): return reverse(item)
    Here are 3 cases of using sitemaps. The first, ArticleSitemap, collects all the articles into the sitemap. The second, ArticlePaginationSitemap, works with pagination pages.
    Of course, not all SEO specialists agree that such pages should be put into the search index at all, but in my opinion they should, and that's why some of them will be in the sitemap.
    And the third class, StaticSitemap, works with static pages. That is, pages that do not change and are not generated in hundreds. I'm talking about contact pages, author and home pages, and, in general, single and unique pages.
    The Sitemap class has 3 main (but there are others), in my opinion, methods that can be useful to everyone:
    1. items - determines what will be returned and how many elements will be returned in the list.
    2. location - returns which path to return
    3. lastmod - returns the date of the last update of content, article, category.
    Furthermore, for StaticSitemap, I use a modifier such as i18n=True. I do this because I have these pages in several translation options. Therefore, for example, it will return not http://website.com/article1, but http://website.com/ru/article1 + http://website.com/en/article1.

    Connect a sitemap

    After you have written your Sitemap classes, you will need to connect them in the main urls.py, where the application URLs are connected. And, of course, to connect a default django app for rendering sitemaps in settings.py. In the urls.py file:
    from django.contrib.sitemaps.views import sitemap from Backend.sitemaps import ArticleSitemap, ArticlePaginationSitemap, StaticSitemap sitemaps = { "articles": ArticleSitemap, "static": StaticSitemap, "pagination": ArticlePaginationSitemap } urlpatterns = [ path('admin/', admin.site.urls), path('captcha/', include('captcha.urls')), path( "sitemap.xml", sitemap, {"sitemaps": sitemaps}, name="django.contrib.sitemaps.views.sitemap", ), ]
    In the settings.py file:
    INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sitemaps', ]
    We import the previously created Sitemap classes, plus a view for our sitemaps. Afterward, we create a dictionary for all the created classes and connect them to urlpatterns. Yes, we connect them not in urlpatterns = + i18n_patterns, because according to the precepts of search engines, sitemaps should be in the root directory.
    Don't forget to add a link to the sitemap in the robots.txt file:
    User-agent: * Disallow: /some_admin/* Host: https://website.com Sitemap: https://website.com/sitemap.xml

    Conclusion

    I would like to add that the possibilities of generating sitemaps are not limited to this. You can generate several at once and combine them into an index sitemap. Or you can generate robots.txt in django and combine it with the sitemap without manual intervention. In any case, for a more in-depth acquaintance with the topic, I recommend reading the official documentation.


    Do not forget to share, like and leave a comment :)

    Comments

    (0)

    captcha
    Send
    LOADING ...
    It's empty now. Be the first (o゚v゚)ノ

    Other

    Similar articles


    How to implement the feedback form in Django and HTMx

    Clock
    11.04.2025
    /
    Clock
    01.10.2025
    An eye
    1030
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    In this article, I will describe the way to add on your Django website feedback form using only HTMx and a little bit of DaisyUI as a UI library. Everything …

    How to make 404 and 500 error pages in Django

    Clock
    12.04.2025
    /
    Clock
    01.10.2025
    An eye
    1022
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    In this article, I will describe how to make custom errors pages such as 404 and 500. I will show two main ways to do this and how you can …

    Used termins