[Fixed]-How do you create a sitemap index in Django?

1👍

If you included the app with a namespace (e.g. include(‘myapp.urls’, namespace=’myapp’), then you need to include the namespace when reversing, e.g. {% url ‘myapp:my_url_name’ %} or reverse(‘myapp:my_url_name’).

url(r'^sitemap-(?P<section>.+)\.xml$', cache_page(86400)(views.sitemap), {'sitemaps': sitemaps}, name='sitemapsname'),
url(r'^sitemap\.xml$', cache_page(86400)(views.index), {'sitemaps': sitemaps, 'sitemap_url_name': 'posts:sitemapsname'}),

note: posts:sitemapsname is my app

see source code of Django: https://github.com/django/django/blob/master/django/contrib/sitemaps/views.py

0👍

The “sections” are what you define in a dictionary, something like this:

urls.py

from .views import StaticSitemap, BlogSitemap, NewsSitemap

SITEMAPS = {
    'blog': BlogSitemap,
    'main': StaticSitemap,
    'news': NewsSitemap,
} 

These will show up as sitemap-blog.xml, sitemap-main.xml and sitemap-news.xml in your sitemap.xml index.

These are laid out in your views.py, for example:

class BlogSitemap(Sitemap):
    changefreq = "never"
    priority = 0.5

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

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

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

You don’t need to access the index file, as there is a default in Django that will be used. It should just work automatically.

👤kbdev

Leave a comment