How to join wagtail and django sitemaps?

安稳与你 提交于 2019-12-23 15:28:42

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/44681046/how-to-join-wagtail-and-django-sitemaps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!