Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join wagtail and django sitemaps?

I'm using wagtail app in my Django project. Is it possible to join django sitemaps (https://docs.djangoproject.com/en/1.11/ref/contrib/sitemaps/) with wagtail sitemaps (wagtail.contrib.wagtailsitemaps)? Tried using django sitemap indexes, but it divide only django sitemap, how I can include wagtail sitemap?

like image 468
vyckiuz Avatar asked Jun 21 '17 16:06

vyckiuz


1 Answers

Wagtail uses the Django sitemap framework since version 1.10. This should allow you to easily combine regular Django sitemaps with Wagtail sitemaps.

There is a small catch however; because wagtail supports multiple sites the sitemap should know for which site the sitemap is generated. For this reason wagtail provides it's own sitemap views (index and sitemap). These views extend the Django sitemap views in order to propagate the site object.

So instead of importing the sitemap views from django:

from django.contrib.sitemaps import views as sitemap_views

Use the wagtail versions:

from wagtail.contrib.wagtailsitemaps import views as sitemaps_views

And then use the Django approach to map the urls to the views:

from wagtail.contrib.wagtailsitemaps import Sitemap
urlpatterns = [
    # ...
    url(r'^sitemap\.xml$', sitemaps_views.index, {
        'sitemaps': {
            'pages': Sitemap
        },
        'sitemap_url_name': 'sitemap',
    }),
    url(r'^sitemap-(?P<section>.+)\.xml$', sitemaps_views.sitemap,
        name='sitemap'),
    # ...
]

For a complete example you can see the code in the tests:

https://github.com/wagtail/wagtail/blob/911009473bc51e30ff751fda0ea5d2fa1d2b450f/wagtail/tests/urls.py#L36

like image 143
mvantellingen Avatar answered Oct 19 '22 09:10

mvantellingen