如何为 django 站点地图中的项目列表设置不同的更改频率和优先级?

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

我在我的 django 项目中构建了一个静态站点地图,如下所示:

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)

如果我想为不同的url设置不同的优先级和更改频率怎么办?

我看到了这个问题,但我仍然ddo不知道该怎么做: Priority issue in Sitemaps

以类似的方式,您可以使用:

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]

我实际上是这样做的:

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]