【问题标题】:Priority issue in Sitemaps站点地图中的优先问题
【发布时间】:2010-10-20 07:03:01
【问题描述】:

我正在尝试使用 Django 站点地图。

class BlogSiteMap(Sitemap):
    """A simple class to get sitemaps for blog"""

    changefreq = 'hourly'
    priority = 0.5

    def items(self):
        return Blog.objects.order_by('-pubDate')

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

我的问题是..我想将前 3 个博客对象的优先级设置为 1.0,其余的 作为 0.5 优先级。

我阅读了documentation,但无法摆脱它。

任何帮助都将不胜感激。提前致谢。

【问题讨论】:

    标签: python django sitemap


    【解决方案1】:

    我认为您可以更改每个对象的优先级。比如这样:

    def items(self):
        for i, obj in enumerate(Blog.objects.order_by('-pubDate')):
           obj.priority = i < 3 and 1 or 0.5
           yield obj
    
    def priority(self, obj):
        return obj.priority
    

    【讨论】:

    【解决方案2】:

    类似的方法可能有效:

    def priority(self, obj):
        if obj.id in list(Blog.objects.all()[:3].values_list('id'))
            return 1.0
        else:
            return 0.5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-17
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多