Search code examples
djangoxml-sitemap

How to have different change frequency and priority for a list of items in django sitemap?


I have built a static sitemap in my django project as below:

class StaticViewSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.9
    protocol = 'http' if DEBUG else 'https'

    def items(self):
        return ['home', 'contact_us', 'blog', ]

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

What should I do if I want to set different priorities and change frequency for different urls?

I have seen this question but I still ddo not know what to do: Priority issue in Sitemaps


Solution

  • In a similar way, you can use:

    class StaticViewSitemap(Sitemap):
        changefreq = "weekly"
        # Remove the priority from here
        protocol = 'http' if DEBUG else 'https'
    
        def items(self):
            return ['home', 'contact_us', 'blog', ]
    
        def location(self, item):
            return reverse(item)
    
        def priority(self, item):
            return {'home': 1.0, 'contact_us': 1.0, 'blog': 0.5}[item]