Skip to content Skip to sidebar Skip to footer

How Do You Create A Sitemap Index In Django?

The Django documentation is very minimal and I can't seem to get this to work. Currently I have 3 individual sitemaps, and I would like to create a sitemap index for them: (r'^sit

Solution 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

Solution 2:

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

urls.py

from .views import StaticSitemap, BlogSitemap, NewsSitemapSITEMAPS= {
    '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:

classBlogSitemap(Sitemap):
    changefreq = "never"
    priority = 0.5defitems(self):
        return BlogPage.objects.all()

    deflastmod(self, obj):
        return obj.date

    deflocation(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.

Post a Comment for "How Do You Create A Sitemap Index In Django?"