如何加入wagtail和django网站地图? [英] How to join wagtail and django sitemaps?

查看:54
本文介绍了如何加入wagtail和django网站地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django项目中使用了wagtail应用.是否可以加入django网站地图( https://docs.djangoproject.com/zh/1.11/ref/contrib/sitemaps/)与w尾形站点地图(wagtail.contrib.wagtailsitemaps)?尝试过使用django站点地图索引,但只划分了django站点地图,如何包含include实站点地图?

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?

推荐答案

Wagtail从1.10版开始使用Django站点地图框架.这样一来,您就可以轻松地将常规Django网站地图与Wagtail网站地图结合起来.

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

但是有一个小问题;因为w支持多个站点,所以站点地图应知道该站点是针对哪个站点生成的.因此,wagtail提供了自己的站点地图视图(索引和站点地图).这些视图扩展了Django站点地图视图,以便传播站点对象.

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.

因此,与其从django中导入站点地图视图,不如:

So instead of importing the sitemap views from django:

from django.contrib.sitemaps import views as sitemap_views

使用the版本:

from wagtail.contrib.wagtailsitemaps import views as sitemaps_views

然后使用Django方法将网址映射到视图:

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

这篇关于如何加入wagtail和django网站地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆