5

「changefreq」と「priority」を含むカスタム Wagtail サイトマップを作成しようとしています。デフォルトは、'lastmod' と 'url' だけです。

Wagtail のドキュメント ( http://docs.wagtail.io/en/latest/reference/contrib/sitemaps.html ) によると、/wagtailsitemaps/sitemap.xml にサイトマップを作成することで、デフォルトのテンプレートを上書きできます。

私はこれをしました。サイトマップ テンプレートは次のようになります。

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% spaceless %}
{% for url in urlset %}
  <url>
    <loc>{{ url.location }}</loc>
    {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}   </lastmod>{% endif %}
    {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
    {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
   </url>
{% endfor %}
{% endspaceless %}
</urlset>

設定のインストール済みアプリに「wagtail.contrib.wagtailsitemaps」を追加しました。上書きしようとして、Page クラスを変更して get_sitemap_urls 関数を含めました。

class BlockPage(Page):
    author = models.CharField(max_length=255)
    date = models.DateField("Post date")
    body = StreamField([
        ('heading', blocks.CharBlock(classname='full title')),
        ('paragraph', blocks.RichTextBlock()),
        ('html', blocks.RawHTMLBlock()),
        ('image', ImageChooserBlock()),
    ])

    search_fields = Page.search_fields + (
        index.SearchField('heading', partial_match=True),
        index.SearchField('paragraph', partial_match=True),
    )

    content_panels = Page.content_panels + [
        FieldPanel('author'),
        FieldPanel('date'),
        StreamFieldPanel('body'),
    ]

    def get_sitemap_urls(self):
        return [
            {
                'location': self.full_url,
                'lastmod': self.latest_revision_created_at,
                'changefreq': 'monthly',
                'priority': .5
            }
        ]

まだ機能していません。他に何か不足していますか?Wagtail のドキュメントはこれ以上の情報を提供しておらず、Wagtail に関する Web 上の他のドキュメントは非常に軽量です。どんな助けでも大歓迎です。

4

1 に答える 1

7

私はそれを考え出した。私は間違ったクラスで関数を持っていました。サイトマップに表示するには、一般的な BlockPage クラスではなく、特定の Page クラスごとに移動する必要がありました。これにより、必要に応じて各ページに異なる優先度を設定することもできました。

解決:

class HomePage(Page):
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname='full')
    ]

    def get_sitemap_urls(self):
        return [
            {
                'location': self.full_url,
                'lastmod': self.latest_revision_created_at,
                'changefreq': 'monthly',
                'priority': 1
            }
        ]

class AboutPage(Page):
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname='full')
    ]

    def get_sitemap_urls(self):
        return [
            {
                'location': self.full_url,
                'lastmod': self.latest_revision_created_at,
                'changefreq': 'monthly',
                'priority': .5
            }
        ]
于 2016-06-17T19:06:34.800 に答える