Django - 站点地图 - 手动设置绝对网址

问题描述

我正在生成站点地图,但此站点地图所针对的网站具有不同的 URL 路由和不同的域。

我认为重写 location 方法会起作用,但问题是 Django 会在每个 url 之前自动添加 Site url。

http://example.comhttps://thewebsite.com...


<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url><loc>http://example.comhttps://thewebsite.com/article/123/slug</loc><lastmod>2021-05-10</lastmod>
         <changefreq>hourly</changefreq><priority>0.5</priority>
    </url>
</urlset>
class WebsiteSitemap(Sitemap):
    changefreq = "hourly"
    priority = 0.5

    def items(self) -> typing.List:
        items = []
        items.extend(Article.objects.home())
        return items

    def location(self,obj: typing.Union[Article]):
        return obj.website_full_url

    def lastmod(self,obj: typing.Union[Article]) -> datetime:
        return obj.modified

有没有办法告诉 Django 不要自动构建 URL?

解决方法

我通过构建自定义模板标签解决了这个问题。我使用 tag 替换站点地图模板中的 URL