wagtail の
参考
- Sitemap generator — Wagtail 1.7 documentation
- Model Reference — Wagtail 1.7 documentation
- django - Custom Wagtail Sitemap - Stack Overflow
wagtail.contrib.wagtailsitemaps の 設定
wagtail では、
この
INSTALLED_APPS に 追加
INSTALLED_APPS
にwagtail.contrib.wagtailsitemaps
を
django.contrib.sitemaps
のdjango.contrib.sitemaps
も
INSTALLED_APPS = [ 'wagtail.contrib.wagtailsitemaps', 'django.contrib.sitemaps', ]
django.contrib.sitemaps
をTemplateDoesNotExist
が
urls.py に sitemap.xml を 追加
urls.py に
from wagtail.contrib.wagtailsitemaps.views import sitemap urlpatterns = [ ... url(r'^sitemap\.xml$', sitemap), ]
- 補足 で
Django 2.1.8 wagtail 2.3
動作確認を した ところ、 以下のような sitemap.xml の 出力と なりました。 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>https://mutter.monotalk.xyz/</loc> <lastmod>2018年3月21日17:25</lastmod> <changefreq/> <priority/> </url> <url> <loc>https://mutter.monotalk.xyz/about</loc> <lastmod>2018年3月21日21:32</lastmod> <changefreq/> <priority/> </url> <url> <loc>https://mutter.monotalk.xyz/posts</loc> <lastmod>2018年3月21日16:59</lastmod> <changefreq/> <priority/> </url>
lastmod
に日本語が 設定されるのは、 sitemap.xml の 形式と して 不正に なります。
以下、templateと して 別途xmlを 準備して、 urls.pyを 書きかえた ところ 正常に 出力されました。 from wagtail.contrib.wagtailsitemaps.views import sitemap urlpatterns = [ ... url(r'^sitemap\.xml$', sitemap, {'template_name': 'custom_sitemap.html' }), ]
<?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>
http://127.0.0.1:8000/sitemap.xml
に アクセスする
ローカルサーバーを
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url><loc>http://localhost/</loc><lastmod>2018-03-10</lastmod></url> <url><loc>http://localhost/posts/</loc><lastmod>2018-03-10</lastmod></url> <url><loc>http://localhost/posts/3a211ddd211948452de1613f82543eed/</loc></url> <url><loc>http://localhost/posts/a516f27b3fd13e5a24ff36327142a613/</loc></url> <url><loc>http://localhost/posts/6c287eab5aa45415bf5c50df1e61a47f/</loc></url> <url><loc>http://localhost/about/</loc><lastmod>2018-03-10</lastmod></url> </urlset>
ページごとに sitemap.xml の 出力内容を 変更する
Page クラスに、get_sitemap_urls
ファンクションを
def get_sitemap_urls(self): return [ { 'location': self.full_url, 'lastmod': self.latest_revision_created_at, 'changefreq': 'monthly', 'priority': .5 } ]
以上です。
コメント