Django站点地图更改基本URL

bed*_*dre 6 sitemap django

我正在使用https://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/?from=olddocs.

我有一个从api.mydomain.me为域生成的站点地图:mydomain.com.

我可以用django指定一个基本网址吗?

现在用location()方法返回:

api.mydomain.me/page/3123而不是mydomain.com/page/3123

这可能吗?谢谢.

bed*_*dre 10

解决了,我重新定义了自己的get_urls.有用:

class MySitemap(Sitemap):
    changefreq = "never"
    priority = 0.5
    location = ""

    def get_urls(self, site=None, **kwargs):
        site = Site(domain='mydomain.com', name='mydomain.com')
        return super(MySitemap, self).get_urls(site=site, **kwargs)

    def items(self):
        return MyObj.objects.all().order_by('pk')[:1000]

    def lastmod(self, obj):
        return obj.timestamp
Run Code Online (Sandbox Code Playgroud)

  • 效果很好。我不知道您使用的是哪个版本的 Django,但如果您使用的是 Django 1.5.1,那么只有在 get_urls 函数中添加“协议”参数时,这才有效。https://github.com/django/django/blob/1.5.1/django/contrib/sitemaps/__init__.py (2认同)