gpt4 book ai didi

django - 如何将标签网址添加到 sitemap.xml?

转载 作者:行者123 更新时间:2023-12-02 03:21:26 24 4
gpt4 key购买 nike

我正在为 Django + Wagtail 项目生成 sitemap.xml

我通过重写 get_sitemap_urls 方法实现了文章的 xml 生成。但问题是 Wagtail 站点地图生成器 无法“看到”博客标签网址(不会将它们添加到站点地图中)。

...
from taggit.models import TaggedItemBase


class BlogPageTag(TaggedItemBase):
content_object = ParentalKey(
'BlogInnerPage',
related_name='tagged_items',
on_delete=models.CASCADE,
)

class BlogInnerPage(Page):
icon = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=False,
on_delete=models.SET_NULL,
related_name='+'
)
...
post_date = models.DateTimeField(auto_now_add=True, null=True)
tags = ClusterTaggableManager(through=BlogPageTag, blank=False)

@property
def sidebar_tags(self):
blogs = BlogInnerPage.objects.live().all()
tags = {}
for post in blogs:
for tag in post.tags.all():
if tag.slug in tags:
tags[tag.slug]['count'] += 1
else:
tags[tag.slug] = {
'name': tag.name,
'count': 1
}
return sorted(tags.items())

...
def get_sitemap_urls(self):
return [
{
'location': self.full_url,
'lastmod': self.last_published_at,
'changefreq': 'weekly',
'priority': .8
}
]

我期望看到以下标签结果:

<url>
<loc>https://example.com/?tag=design</loc>
<lastmod>2019-01-31T12:24:01+00:00</lastmod>
<priority>0.80</priority>
</url>

以下是我的博客文章:

<url>
<loc>
http://example.com/trends-booming-todays-it-industry/
</loc>
<lastmod>2018-10-04</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>

最佳答案

使用get_sitemap_urls,您的方向是正确的。默认实现仅返回页面本身的条目,因为它不可能知道您可能过滤的所有查询参数。因此,您可以将这些条目添加到列表中。

假设您有 2 个页面类,即 HomePageBlogInnerPage,您需要保留 BlogInnerPage 实现。并更新HomePage以返回其自己的站点地图条目并添加标签条目。

from urllib.parse import urlencode

from django.db.models import Max
from taggit.models import Tag
from wagtail.core.models import Page


class HomePage(Page):
# Note that the method signature should accept an optional request parameter as of Wagtail 2.2
def get_sitemap_urls(self, request=None):
urls = super().get_sitemap_urls(request)

# Get the page's URL, we will use that later.
base_url = self.get_full_url(request)

# Get the IDs of all the tags used on your `BlogPage`s.
tag_ids = BlogPageTag.objects.values_list('tag_id', flat=True).distinct()

# 1. Filter all the tags with the IDs fetched above.
# 2. Annotate the query with the latest `last_published_at` of the associated pages.
# Note the `home_` part in the string, this needs to be replaced by the name of the Django app your `BlogPageTag` model lives in.
# 3. Only fetch the slug and lastmod (we don't need the full object).
tags = Tag.objects\
.filter(pk__in=tag_ids)\
.annotate(lastmod=Max('home_blogpagetag_items__content_object__last_published_at'))\
.values('slug', 'lastmod')

# Add sitemap entries for each tag.
for tag in tags:
urls.append({
'location': '{}?{}'.format(base_url, urlencode({'tag': tag.slug})),
'lastmod': tag.lastmod,
})

# Return the results.
return urls

关于django - 如何将标签网址添加到 sitemap.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54644965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com