站点地图中的优先问题

2024-06-28 19:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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个blog对象的优先级设置为1.0,其余的设置为1.0 作为0.5优先级。在

我读了documentation,但没有办法摆脱它。在

任何帮助都是值得赞赏的。提前谢谢。在


Tags: todjangoselfobjgetreturn站点def
2条回答

类似的方法可能会奏效:

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

我想你可以改变每个对象的优先级。比如说:

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

相关问题 更多 >