Sitemaps are how web crawlers such as search engines find your pages! Rather than having to grab your links and parse your html, they’re all already there.
INSTALLED_APPS = [
...
"wagtail.contrib.sitemaps",
"django.contrib.sitemaps",
now to the core urls.py!!!
from wagtail.contrib.sitemaps.views import Sitemap
urlpatterns = [
...
We are going to want to make some routable pages exist in this sitemap!
In our blog models:
def get_context(self, request, *args, **kwargs):
"""Adding Custom stuff to our context."""
context = super().get_context(request, *args, **kwargs)
context["posts"] = BlogDetailPage.objects.live().public()
return context
@route(r'^latest/?$', name="latest_posts")
def latest_blog_posts(self, request, *args, **kwargs):
context = self.get_context(request, *args, **kwargs)
context["latest_posts"] = BlogDetailPage.objects.live().public()[:1]
return render(request, 'blog/latest_posts.html', context)
def get_sitemap_urls(self, request):
# uncomment to havae no sitemap for this page
# return []
sitemap = super().get_sitemap_urls(request)
sitemap.append(
{
"location": self.full_url + self.reverse_subpage("latest_posts"),
"lastmod": (self.last_published_at or self.latest_revision_created_at),
"priority": 0.9
}
)
return sitemap
If you want the routed page to not enter the xml, just return the function with an empty list!