Django 如何将标记URL添加到sitemap.xml?

Django 如何将标记URL添加到sitemap.xml?,django,web-applications,sitemap,wagtail,django-taggit,Django,Web Applications,Sitemap,Wagtail,Django Taggit,我正在为Django+Wagtail项目生成sitemap.xml 我通过重写get\u sitemap\u url方法实现了文章的xml生成。但是问题是Wagtail站点地图生成器没有看到博客标签URL,也没有将它们添加到站点地图中 ... from taggit.models import TaggedItemBase class BlogPageTag(TaggedItemBase): content_object = ParentalKey( 'BlogInn

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

我通过重写get\u sitemap\u url方法实现了文章的xml生成。但是问题是Wagtail站点地图生成器没有看到博客标签URL,也没有将它们添加到站点地图中

...
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>
以下是我的博客文章:


使用get_sitemap_URL,您的思路是正确的。默认实现只返回页面本身的条目,因为它不可能知道您可能要筛选的所有查询参数。因此,您可以将这些条目添加到列表中

假设您有两个页面类,HomePage和BlogInnerPage,您将希望保留BlogInnerPage实现,并更新主页以返回其自己的站点地图条目和添加标记条目

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

伟大的谢谢!
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