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

729 views Asked by At

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

2

There are 2 answers

0
Jonathan Harel On BEST ANSWER

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]
0
Amin Ba On

I actually did this to achieve it:

class StaticViewSitemap(Sitemap):

    protocol = 'http' if DEBUG else 'https'
    static_url_list = [
        {'url': 'home', 'priority': 0.8, 'changefreq': "monthly"},
        {'url': 'contact_us', 'priority': 0.6, 'changefreq': "weekly"},
        {'url': 'blog', 'priority': 0.4, 'changefreq': "weekly"}
    ]

    def items(self):
        return [item['url'] for item in self.static_url_list]

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

    def priority(self, item):
        return {element['url']: element['priority'] for element in self.static_url_list}[item]

    def changefreq(self, item):
        return {element['url']: element['changefreq'] for element in self.static_url_list}[item]